1.我們先創(chuàng)建一個(gè).NET Framework桌面應(yīng)用程序栗涂。如下圖:
把程序命名為記事本
2. 放一個(gè)菜單欄寫(xiě)成下圖:
3.放一個(gè)RichTextBox曹货,把滾動(dòng)條設(shè)為有豎向滾動(dòng)條。
4髓霞、放一個(gè)Panel半火,里面放一個(gè)ListBox和Button塞赂。
5.給打開(kāi)注冊(cè)一個(gè)單擊事件邑闺,代碼如下:
List<string> paths = new List<string>();
private void 打開(kāi)ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? OpenFileDialog ofd = new OpenFileDialog();
? ? ? ? ? ? ofd.Title = "請(qǐng)選擇要打開(kāi)的文件";
? ? ? ? ? ? ofd.Filter = "文本文件(*.txt)|*.txt|rtf文件(*.rtf)|*.rtf|身份證文件(*.sfz)|*.sfz";
? ? ? ? ? ? ofd.Multiselect = true;
? ? ? ? ? ? ofd.ShowDialog();
? ? ? ? ? ? if(ofd.FileNames.Length==0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? for (int i = 0; i < ofd.FileNames.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? richTextBox1.Clear();
? ? ? ? ? ? ? ? using (FileStream fs = new FileStream(ofd.FileNames[i],FileMode.Open,FileAccess.Read))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? using (StreamReader sr=new StreamReader(fs,Encoding.Default))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? while (!sr.EndOfStream)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? richTextBox1.AppendText(sr.ReadLine() + "\r\n");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? this.Text=Path.GetFileNameWithoutExtension(ofd.FileNames[i])+" - 記事本";
? ??????????????????????listBox1.Items.Add(Path.GetFileNameWithoutExtension(ofd.FileNames[i]));
????????????????????????paths.Add(ofd.FileNames[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
6.給保存注冊(cè)一個(gè)單擊事件跌前,代碼如下:
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? SaveFileDialog sfd = new SaveFileDialog();
? ? ? ? ? ? sfd.Title = "請(qǐng)選擇要保存的文件";
? ? ? ? ? ? sfd.Filter = "文本文件(*.txt)|*.txt|rtf文件(*.rtf)|*.rtf|身份證文件(*.sfz)|*.sfz";
? ? ? ? ? ? sfd.ShowDialog();
? ? ? ? ? ? if(string.IsNullOrEmpty(sfd.FileName))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create,FileAccess.Write))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? fs.Write(Encoding.Default.GetBytes(richTextBox1.Text), 0, Encoding.Default.GetBytes(richTextBox1.Text).Length);
? ? ? ? ? ? }
? ? ? ? ? ? MessageBox.Show("保存成功!","?");
? ? ? ? }
7.給窗體注冊(cè)一個(gè)加載事件陡舅,代碼如下:
private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? richTextBox1.WordWrap = false;
? ? ? ? ? ? panel1.Visible = false;
? ? ? ? }
8.給☆自動(dòng)換行注冊(cè)一個(gè)單擊事件抵乓,代碼如下:
private void 自動(dòng)換行ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if(自動(dòng)換行ToolStripMenuItem.Text=="☆自動(dòng)換行")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? 自動(dòng)換行ToolStripMenuItem.Text = "★取消自動(dòng)換行";
? ? ? ? ? ? ? ? richTextBox1.WordWrap = true;
? ? ? ? ? ? }
? ? ? ? ? ? else if(自動(dòng)換行ToolStripMenuItem.Text=="★取消自動(dòng)換行")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? 自動(dòng)換行ToolStripMenuItem.Text = "☆自動(dòng)換行";
? ? ? ? ? ? ? ? richTextBox1.WordWrap = false;
? ? ? ? ? ? }
? ? ? ? }
9.給圖片注冊(cè)一個(gè)單擊事件,代碼如下:
private void 圖片ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? OpenFileDialog ofd = new OpenFileDialog();
? ? ? ? ? ? ofd.Filter = "圖片文件(*.png;*.jpg;*.jfif;*.bmp;*.jpeg;*.jpe)|*.png;*.jpg;*.jfif;*.bmp;*.jpeg;*.jpe";
? ? ? ? ? ? ofd.ShowDialog();
? ? ? ? ? ? Clipboard.SetDataObject(Image.FromFile(ofd.FileName), false);
? ? ? ? ? ? richTextBox1.Paste();
? ? ? ? }
10.給字體注冊(cè)一個(gè)單擊事件靶衍,代碼如下:
private void 字體ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? FontDialog fd = new FontDialog();
? ? ? ? ? ? fd.ShowDialog();
? ? ? ? ? ? richTextBox1.Font = fd.Font;
? ? ? ? }
11.給顏色注冊(cè)一個(gè)單擊事件灾炭,代碼如下:
private void 顏色ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? ColorDialog cd = new ColorDialog();
? ? ? ? ? ? cd.ShowDialog();
? ? ? ? ? ? richTextBox1.ForeColor = cd.Color;
? ? ? ? }
12.給顯示注冊(cè)一個(gè)單擊事件,代碼如下:
private void 顯示ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? panel1.Visible = true;
? ? ? ? }
13.給隱藏注冊(cè)一個(gè)單擊事件颅眶,代碼如下:
private void 隱藏ToolStripMenuItem_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? panel1.Visible = false;
? ? ? ? }
14.給listBox注冊(cè)一個(gè)雙擊事件咆贬,代碼如下:
private void ListBox1_DoubleClick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? richTextBox1.Clear();
? ? ? ? ? ? using (FileStream fs=new FileStream(paths[listBox1.SelectedIndex],FileMode.Open,FileAccess.Read))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? using (StreamReader sr =new StreamReader(fs,Encoding.Default))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? while(!sr.EndOfStream)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? richTextBox1.AppendText(sr.ReadLine() + "\r\n");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? this.Text = Path.GetFileNameWithoutExtension(paths[listBox1.SelectedIndex]) + " - 記事本";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
15.給Button注冊(cè)一個(gè)單擊事件,代碼如下:
private void Button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? panel1.Visible = false;
? ? ? ? }
本章總結(jié):對(duì)于MenuStrip帚呼、RichTextBox、Panel皱蹦、ListBox煤杀、Button進(jìn)行了鞏固。
------------------------------------------------------------end------------------------------------------------------------------------------------