C # learning notes-data transmission (public variables) and Dictionary,

Source: Internet
Author: User

C # learning notes-data transmission (public variables) and Dictionary,

The more code you read, the more code you write, the more you enjoy these characters, and finally gradually understand the sense of accomplishment in the rumor. In particular, you can learn it step by step, one code and one code, even if only a small function is completed, it is also very proud! This kind of pride does not need to be told to others, and you are especially satisfied!

The most beautiful part of the code is that all possibilities are impossible ......

 

 

Display function:

1. Select different buttons to transfer the corresponding data to the corresponding window;

1 class PublicName2 {3 public static string ChoicedName; 4 // key and value, the key must be unique, and the value does not need a unique 5 public static Dictionary <int, string> NameType = new Dictionary <int, string> (); 6}

Then, enter the software selection window.

(This can be optimized. You don't need to add a language to each display, but you are too lazy to modify it now)

1 public partial class ChoiceName: Form 2 {3 Form1 form1 = new Form1 (); 4 5 public ChoiceName () 6 {7 InitializeComponent (); 8} 9 10 private void button#click (object sender, EventArgs e) 11 {12 PublicName. nameType. add (1, "Chinese"); 13 PublicName. choicedName = ""; 14 form1.Show (); 15 this. visible = false; 16} 17 18 private void button2_Click (object sender, EventArgs e) 19 {20 PublicName. nameType. add (2, "English"); 21 PublicName. choicedName = "English"; 22 form1.Show (); 23 this. visible = false; 24} 25 26 private void button3_Click (object sender, EventArgs e) 27 {28 PublicName. nameType. add (3, "Korean"); 29 PublicName. choicedName = "Korean"; 30 form1.Show (); 31 this. visible = false; 32} 33 34 private void button4_Click (object sender, EventArgs e) 35 {36 PublicName. nameType. add (4, "French"); 37 PublicName. choicedName = "French"; 38 form1.Show (); 39 this. visible = false; 40} 41 42 private void button5_Click (object sender, EventArgs e) 43 {44 OtherName oN = new OtherName (); 45 oN. show (); 46 this. visible = false; 47} 48}

If you select "others", you need to enter the input window interface.

 1     public partial class OtherName : Form 2     { 3         public OtherName() 4         { 5             InitializeComponent(); 6         } 7  8         private void button1_Click(object sender, EventArgs e) 9         {10             PublicName.ChoicedName = textBox1.Text;11             PublicName.NameType.Add(5, PublicName.ChoicedName);12             Form1 ending = new Form1();13             ending.Show();14             this.Close();15         }16     }

Finally, both "Chinese" and "Others" need to transmit data to Form1, which reflects the importance of public variables. All the data just now is ChoicedName. Therefore, form1 just look for ChoicedName!

 1   public partial class Form1 : Form 2     { 3         public Form1() 4         { 5             InitializeComponent(); 6         } 7  8         private void Form1_Load(object sender, EventArgs e) 9         {10             comboBox1.Text = PublicName.ChoicedName;11         }12 13         private void button1_Click(object sender, EventArgs e)14         {15             Application.Exit();16         }17     }

 

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.