简体   繁体   English

从一种形式显示数据到另一种形式的标签

[英]Displaying data from one form to a label in another form

I'm currently having problem in displaying my transferred String data from the my 1st form to the second one. 我目前在显示从第一种形式到第二种形式的转移String数据时遇到问题。 I tried putting a button in my 2nd form to check if the data is transferred, and I confirmed that the data was transferred by using MessageBox.Show() command. 我尝试在第二个表单中放置一个按钮,以检查是否已传输数据,并且确认使用MessageBox.Show()命令已传输了数据。 But when I try to assign it to my Label the Label text does not change. 但是,当我尝试将其分配给我的LabelLabel文本不会更改。 Here is my Sample Code: 这是我的示例代码:

First Form: Data transfer is being triggered by clicking a cell in my dataGridView 第一种形式:通过单击我的dataGridView一个单元格来触发数据传输

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        PopupSummary f = new PopupSummary();
        f.searchText = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        this.Hide();
        f.ShowDialog();
        this.Show();
    }

Second Form: 第二种形式:

     public string searchText = "";
private void PopupSummary_Load(object sender, EventArgs e)
        {
            label1.Text = searchText;
        }

Note: 注意:

label1 is inside a tableLayoutPanel. label1在tableLayoutPanel内部。

My main problem is that the label.Text does not change the text of my current label even if the data was successfully transferred. 我的主要问题是,即使数据已成功传输,label.Text也不会更改当前标签的文本。 Thank You. 谢谢。

Your problem is that you're assigning your new text to searchText , not to label1.Text . 您的问题是您要将新文本分配给searchText而不是 label1.Text The only time you assign anything to label1.Text is on form load, in PopupSummary_Load , when you set it to the value of searchText . 你分配什么的唯一时间label1.Text是形式的负载,在PopupSummary_Load ,当你将它设置为价值searchText Simply changing the value of searchText after that won't cause the label text to be updated. 简单地改变的值searchText ,不会导致标签的文本进行更新。

You can do any number of things now that you know what is going on, but here is a simple one: 现在您知道发生了什么,您可以执行许多操作,但这是一个简单的操作:

f.label1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

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

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