简体   繁体   English

单击按钮后,以另一种形式刷新标签文本

[英]refresh label text in another form after button click C#

i have 2 project in 1 solution and each project have 1 form. 我在1个解决方案中有2个项目,每个项目都有1个表格。 In form 1 i have label and button, and i want when i click on button it will show in form 2 labeltext in form 1. 在表格1中,我有标签和按钮,我想当我单击按钮时它将在表格1中以表格2 labeltext显示。

my code in form 1: 我的代码形式1:

WindowsFormsApplication1.Form1 layarForm1 = new WindowsFormsApplication1.Form1();
layarForm1.Show();
layarForm1.LabelText = no_antrian.Text;
//layarForm1.LabelText = this.Refresh();

form2(in other project but in same solutions, and i have reference to form 1): form2(在其他项目中但在相同的解决方案中,我已参考表1):

public string LabelText
    {
        get
        {
            return this.ruang_1.Text;
        }
        set
        {
            this.ruang_1.Text = value;
        }
    }

my code work for first time but when i click button again it will show new form. 我的代码第一次工作,但是当我再次单击按钮时,它将显示新表格。 is there any ways that label text in form 2 will refresh after button click. 单击按钮后,是否有任何方法可以刷新表格2中的标签文本。 And i dont want to use dispose form because in form 2 i have video played that will start again if i use dispose() and show() 而且我不想使用处置表单,因为在表单2中,如果我使用dispose()和show(),我将播放重新播放的视频

Two projects = 2 executable you need to use shared memory or named pipes to send the data across from one exe to another, or use winapi such as SendMessage once you get the window handle of the other window and the find the child window handle via EnumChildWindows. 两个项目= 2个可执行文件,您需要使用共享内存或命名管道将数据从一个exe发送到另一个exe,或者一旦获取另一个窗口的窗口句柄并通过EnumChildWindows查找子窗口句柄,就使用winapi(例如SendMessage) 。

Why not use the two forms in the same project? 为什么不在同一个项目中使用两种形式?

The problem is every time the button is clicked, an instance of the form is created. 问题是每次单击按钮时,都会创建表单的实例。 You need to check whether the form is already open and if not create an instance, show the form and update the label. 您需要检查表单是否已经打开,如果尚未创建实例,请显示该表单并更新标签。

If the form is already open, all you need to do is to update the label and set focus on the form. 如果表单已经打开,您要做的就是更新标签并将焦点放在表单上。

You can use Application.OpenForms to iterate through all one forms and check whether the form is in that collection. 您可以使用Application.OpenForms遍历所有一种表单,并检查该表单是否在该集合中。

Something like 就像是

foreach (Form frm in WindowsFormsApplication1.OpenForms)  
{  
    if (frm.Name == "MY_FORM_NAME") then
       frm.LabelText = no_antrian.Text;
    else
    {
        WindowsFormsApplication1.Form1 layarForm1 = new       
        WindowsFormsApplication1.Form1();
        layarForm1.Show();
        layarForm1.LabelText = no_antrian.Text;
    }
}

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

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