Winform instance (1)-simple notepad, winform instance notepad

Source: Internet
Author: User

Winform instance (1)-simple notepad, winform instance notepad
Using the time spent learning C #, we made some simple winform forms.

First, let's take one.

Required controls:

1. Two button controls ------ enable and save functions.

2. The corresponding "dialog box" -- openFileDialog and SaveFileDialog controls.

3. A textbox Control is the text content.

3. A timer control prompts the time.

4. A label control with prompt information.

Code implementation: Enable the function:
1 private void btn_open_Click (object sender, EventArgs e) 2 {3 if (openFileDialog1.ShowDialog () = DialogResult. OK) 4 {5 FileName = openFileDialog1.FileName; 6 7 try 8 {9 OriginalContent = File. readAllText (FileName); 10 textBox1.Text = OriginalContent; 11} 12 catch 13 {14 15 lbl_notice.Text = "the file cannot be opened! "; 16} 17} 18}

 

Save:

You have written a method to save and disable it.

1 private void save () 2 {3 // when the content has changed, this flag is True 4 bool ShouldSvae = false; 5 // if the file name is not empty, indicates that the content in the current text box is from file 6 if (FileName! = "") 7 {8 // if the content changes 9 if (textBox1.Text! = OriginalContent & MessageBox. Show ("the file has been modified. Are you sure you want to save it? "," Save file ", MessageBoxButtons. yesNo) = DialogResult. yes) 10 {11 ShouldSvae = true; 12} 13} 14 else15 {16 // if the user inputs the content, a file name 17 if (textBox1.Text! = "" & SaveFileDialog1.ShowDialog () = DialogResult. OK) 18 {19 FileName = saveFileDialog1.FileName; 20 ShouldSvae = true; 21} 22} 23 if (ShouldSvae) 24 {25 try26 {27 File. writeAllText (FileName, textBox1.Text); 28 OriginalContent = textBox1.Text; 29 lbl_notice.Text = "saved files"; 30} 31 catch (Exception) 32 {33 lbl_notice.Text = "failed to save the file !! "; 34 throw; 35} 36} 37}

 

Close the window and save:

The FormClosing event of the form is used.

1 // when the window is closed, save the file 2 private void form=formclosing (object sender, FormClosingEventArgs e) 3 {4 save (); 5}

 

Textbox Control details:

(1) set the multiline attribute;

(2) Side Pull box: Scrollbars attributes;

Timer control:
1 private void timer1_Tick(object sender, EventArgs e)2         {3             lblTimer.Text = DateTime.Now.ToString();4         }
Window title:

Display the file name in the window title.

1 // form initial value 2 private void Form1_Load (object sender, EventArgs e) 3 {4 lblTimer. text = ""; 5 lbl_notice.Text = ""; 6 Text = "untitled-my notepad"; 7 8} 9 // The Form title displays the file name 10 private string OriginalContent; 11 12 private string _ FileName = ""; 13 14 public string FileName15 {16 get {return _ FileName ;} 17 18 set 19 {20 // update the Form title 21 _ FileName = value; 22 // use System. IO reference 23 Text = Path. getFileName (value) + "-My Notepad"; 24 25} 26}

 

All code:
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. IO; 7 using System. linq; 8 using System. text; 9 using System. threading. tasks; 10 using System. windows. forms; 11 12 namespace Notepad 13 {14 public partial class Form1: Form 15 {16 public Form1 () 17 {18 InitializeComponent (); 19 20} 21 22 23 private void Timereffectick (object sender, EventArgs e) 24 {25 lblTimer. text = DateTime. now. toString (); 26} 27 28 // initial form value 29 private void Form1_Load (object sender, EventArgs e) 30 {31 lblTimer. text = ""; 32 lbl_notice.Text = ""; 33 Text = "untitled-my notepad"; 34 35} 36 // format title display file name 37 private string OriginalContent; 38 39 private string _ FileName = ""; 40 41 public string FileName 42 {43 get {return _ FileName;} 44 45 set 46 {47 // Update Form title 48 _ FileName = value; 49 // use System. IO references 50 Text = Path. getFileName (value) + "-My Notepad"; 51 52} 53} 54 55 private void btn_open_Click (object sender, EventArgs e) 56 {57 if (openFileDialog1.ShowDialog () = DialogResult. OK) 58 {59 FileName = openFileDialog1.FileName; 60 61 try 62 {63 OriginalContent = File. readAllText (FileName); 64 textBox1.Text = OriginalContent; 65} 6 6 catch 67 {68 69 lbl_notice.Text = "the file cannot be opened! "; 70} 71} 72} 73 74 private void btn_save_Click (object sender, EventArgs e) 75 {76 save (); 77} 78 79 private void save () 80 {81 // when the content has changed, this flag is True 82 bool ShouldSvae = false; 83 // if the file name is not empty, indicates that the content in the current text box is from the file 84 if (FileName! = "") 85 {86 // if the content is changed 87 if (textBox1.Text! = OriginalContent & MessageBox. Show ("the file has been modified. Are you sure you want to save it? "," Save file ", MessageBoxButtons. yesNo) = DialogResult. yes) 88 {89 ShouldSvae = true; 90} 91} 92 else 93 {94 // if the user inputs the content, a file named 95 if (textBox1.Text! = "" & SaveFileDialog1.ShowDialog () = DialogResult. OK) 96 {97 FileName = saveFileDialog1.FileName; 98 ShouldSvae = true; 99} 100} 101 if (ShouldSvae) 102 {103 try104 {105 File. writeAllText (FileName, textBox1.Text); 106 OriginalContent = textBox1.Text; 107 lbl_notice.Text = "saved file"; 108} 109 catch (Exception) 110 {111 lbl_notice.Text = "failed to save the file !! "; 112 throw; 113} 114} 115} 116 // when the window is closed, save the file 117 private void form=formclosing (object sender, FormClosingEventArgs e) 118 {119 save (); 120} 121 122} 123}
View Code

 

 

Finally:

Through the exercises of the winform instance, you can customize the functions you want.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.