簡體   English   中英

從form1訪問form2中的不同元素,反之亦然

[英]accessing different elements in form2 from form1 and vice versa

我一直在查找有關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;
            }

    }

在此處輸入圖片說明

在Form2類中創建一個公共屬性

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

現在在form1中單擊代碼設置它

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

當然,也可以通過Forms設計器將按鈕的屬性Modifiers公開。 允許訪問表單的內部控件(及其所有屬性)是一個壞主意,從長遠來看,這將導致設計很糟糕的應用程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM