簡體   English   中英

我如何從C#中同一項目下的不同Windows窗體訪問不同的組件?

[英]How can i access the different component from different windows form which are under same project in c#?

例如,在表單4中,我有一個數據網格視圖,當我從數據網格視圖中選擇一個數據並單擊更新時。表單5彈出帶有各種空文本字段。 現在,我要確保這些文本字段填充了我在數據網格視圖中選擇的數據。 我怎樣才能做到這一點?

說我在數據網格視圖中選擇了第四行

id = 4
name = name
address = address

在表格5上,我有3個文本框,即在加載表格時如何使那些文本框自動填充在表格4的數據網格視圖中選擇的數據。

最好的方法可能是為表單編寫一個自定義構造函數,然后將數據傳遞給它。 像這樣:

 public DisplayForm(int id, string name, string address)
{
    InitializeComponent();
    //Take the data that you passed to the constructor, and use it to update the text boxes:
    txtID.Text = id.ToString();
    txtName.Text = name;
    txtAddress.Text = address;
}

然后,使用剛剛編寫的構造函數調用它:

DisplayForm display = new DisplayForm(4, name, address);

您甚至可以創建一個以DataGridViewRow作為參數的對象:

 public DisplayForm(DataGridViewRow row)
{
    InitializeComponent();
    //Take the data that you passed to the constructor, and use it to update the text boxes:
    txtID.Text = row.Cells[0].Value.ToString();
    txtName.Text = row.Cells[1].Value.ToString();
    txtAddress.Text = row.Cells[2].Value.ToString();
}

暫無
暫無

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

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