Winform summary of method calls between different forms

Source: Internet
Author: User

Method One: (Form2 calls the method in Form1: The called name is set in the called form) 1, first set the main form in the main window can be called the name, set its own name of "S". This allows you to invoke the main form's method using "S" in other forms.        public static Form1 s = null;            Public Form1 () {InitializeComponent (); s = this;//define main form}

2. Write the method that can be called in the main window, and note that the property is set to public.

public void Setform1value () {//code required to implement the function ...}

3. For example, the Button1 button in the Form2 form needs to call Form1 's Setform1value () method, the code is as follows:

private void Button1_Click (object sender, EventArgs e) {form1.s.setform1value ();//Call Method of Form1}



Method Two: (Form2 call method in Form1: called Name setting in Form2)


1, Form2 in the code:

        private form1 f ;//Set called name         public form2 (form1 f)          {             InitializeComponent ();             this. f=f;        }       private  Void btn2_click (object sender, eventargs e)          {                              f.setform1value ();//Call method in Form1                       } 

2, Form1 in the code:

Public Form1 () {InitializeComponent (); }private void Btn1_click (object sender, EventArgs e) {new Form2 (this). Show (); Show Form2 Form}






This article is from the "book to Time Square hate" blog, please be sure to keep this source http://7798914.blog.51cto.com/7788914/1653012

Winform Summary of method calls between different forms

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.