简体   繁体   中英

accessing different elements in form2 from form1 and vice versa

I have been finding some issues with form1 and form2, I want the result of pressing a button in form1 to be the change of the text in a button in form2, so mainly my problem is how to access this button in form2 from form1,I want to assign the result of int q into the text of button 2 shown in the picture

private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
        this.Hide();
       // int a=1;

        Random R = new Random();
        int start = R.Next(10, 999);


        if(start>99)
            {
                int x = start-1;
                int y = x%100;
                int z = start/y;
                int w = z+1;
                int q = start/w;
            }
        else
            {

                int y = start-1;
                int z = y/2;
                int w = start/z;
                int q = 1;
            }

    }

enter image description here

Create a public property in Form2 class

public class Form2 : Form
{
    public string Button1Text 
    {
        set { this.Button1.Text = Value; }
    }
    ....
}

Now in form1 click code set it

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show();
    .... your calculations
    f2.Button1Text = theResultOfYourCalculation.ToString()

Of course this could also be done making the property Modifiers of your buttons Public through the Forms designer. Giving access to the internal controls of your form (and all of their properties) is a bad idea and in the long term leads to a very bad designed application

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