简体   繁体   中英

Getting information from button1_click() to button2_click()

I am fairly new to c#. I have one winform with 2 buttons on it. button1_click() generates some data say data1, data2, data3, data4. Now I want to use this data in button2_click():

    private void button1_click(object sender, EventArgs e)
    {
         //generate data1, data2, data3, data4..
    }



    private void button2_click(object sender, EventArgs e)
    {
        //do processing using data1, data2, data3, data4..
    }

I assume this should be relatively simple to do in c# without using files and such. I understand I can pass arguments using a custom class derived from EvenArgs, but I need to get hold of the data first before i can pass it.

Create private fields for data1 etc. and set them in button1_click once they are set they will be available via the current instance in button2_click .

FIelds are part of an object instance's shared state . This means that any instance fields (fields that are not marked as static and are declared within the body of the current type) are available to all instance methods (methods that are not marked as static and are declared within the body of the current type). Since both of your button click event handlers are instance methods they can both access the fields.

Try something like this:

class Foo
{
    // These are the fields
    Object data1;
    Object data2;
    Object data3;
    Object data4;

    void button1_click(object sender, EventArgs e)
    {
         this.data1 = generateData1();
         this.data2 = generateData2();
         this.data3 = generateData3();
         this.data4 = generateData4();
    }

    void button2_click(object sender, EventArgs e)
    {
        // In this method you can access this.data1 etc. since
        // they are instance fields
    }
}

You could use class fields:

private string data1 = "";
private string data2 = "";
private string data3 = "";

private void button1_click(object sender, EventArgs e)
{
    data1 = "some data for field1";
    data2 = "some data for field2";
    data3 = "some data for field3";
}

private void button2_click(object sender, EventArgs e)
{
    // use data1, data2, ... here:
    MessageBox.Show(data1 + data2 + data3);
}

Example:

public class MyForm : Form {

    private string _data1;

    private void button1_click(object sender, EventArgs e) {
        _data1 = "hello";
    }

    private void button2_click(object sender, EventArgs e) {
       MessageBox.Show(_data1);
    }
}

The easies way to do it is to add some state to your Form that is containing these two buttons (I assume they are on the same form).

So in the same class in which you have the posted methods you need to add members:

class MyForm : Form
{
    MyType data1;
    MyType data2;


    private void button1_click(object sender, EventArgs e)
    {
         //generate data1, data2, data3, data4.. <-- here you just set the state of the Form
    }



    private void button2_click(object sender, EventArgs e)
    {
        //do processing using data1, data2, data3, data4.. <-- here you use the state set by button 1
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM