简体   繁体   English

从form1访问form2中的不同元素,反之亦然

[英]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 我一直在查找有关form1和form2的问题,我希望在form1中按一个按钮的结果是对form2中的按钮中的文本进行更改,所以主要是我的问题是如何从form1,I访问form2中的此按钮。想要将int q的结果分配给图片中所示的按钮2的文本

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 在Form2类中创建一个公共属性

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

Now in form1 click code set it 现在在form1中单击代码设置它

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. 当然,也可以通过Forms设计器将按钮的属性Modifiers公开。 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 允许访问表单的内部控件(及其所有属性)是一个坏主意,从长远来看,这将导致设计很糟糕的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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