簡體   English   中英

將類值從form1的列表框傳遞到form2的文本框

[英]Passing a class value from listbox in form1 to textbox in form2

我已經知道了,但是我做的不對。 我正在嘗試將值從form1傳遞到form2。 在form2上,我設置了一個屬性,以允許訪問其中的一個文本框。 在form1上,我已將其設置為打開form2的實例,並將值從列表框中的對象傳遞到form2的文本框中。 似乎我已經正確設置了所有東西,因為我通過將對象值發布到messagebox.show中進行了測試,並且它按照我的計划顯示了不同的對象值。 由於某種原因,雖然我實際運行時會打開form2,但不會設置我在表單中傳遞給文本框的值,但這只是一個空白表單。 我沒有任何錯誤,但我認為這與數據沒有直接傳遞到我的新Form2實例有關。 我希望我解釋得足夠好。 任何幫助表示贊賞。

表格1

private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmProperties editProperties = new frmProperties();
    editProperties.ShowDialog();

    Employee person = (Employee)lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
    editProperties.TextFirstName = person.EmployeeFirstName;
}

表格2

public string TextFirstName
{
    get { return txtFirstName.Text; }
    set { txtFirstName.Text = value; }
}
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmProperties editProperties = new frmProperties();
    editProperties.ShowDialog();

    Employee person = new   Employee ();
person.EmployeeFirstName = lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
    editProperties.TextFirstName = person.EmployeeFirstName;

}

在顯示對話框之前,您必須設置文本框。

private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmProperties editProperties = new frmProperties();
    Employee person =   (Employee)lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
    editProperties.TextFirstName = person.EmployeeFirstName;
    editProperties.ShowDialog();    
}

暫無
暫無

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

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