通过Button打开一个对话框,选择一个文件,在TextBox中显示文件的绝对路径
private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { txtPath.Text = ofd.FileName; }
}
窗体中通过代码添加按钮的背景颜色
public Form1() { InitializeComponent(); SetColButton.BackColor = Color.Green;//指在窗体的构造函数添加,其中SetColButton是Button的name名称 }
C#实现读取文本文件,用一个richTextBox组件显示出来。要读取文本文件
private void btnOpen_Click(object sender, EventArgs e) { FileStream fs = new FileStream("F:\\file.txt", FileMode.Open, FileAccess.Read); StreamReader streamreader = new StreamReader(fs); streamreader.BaseStream.Seek(0,SeekOrigin.Begin); this.rtb.Text = ""; string strLine = streamreader.ReadLine(); while(strLine!=null) { this.rtb.Text += strLine + "\n"; strLine = streamreader.ReadLine(); } streamreader.Close(); }