更改按钮单击标签文本以不同的形式 [英] Change Label text on button click in a different form

查看:130
本文介绍了更改按钮单击标签文本以不同的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个按键,这种情况叫他们按钮1,2和3。然后,我有不同的形式在同一个解决方案,带有标签。我想拥有的是当按下每个按钮时,label.text需要改变其他形式。

I've 3 buttons, for this case call them button 1, 2 and 3. Then I have a different form in the same solution, with an label. What I want to have is when each button is pressed, the label.text needs to change on the other form.

像一个例子:当:
- 按钮1点:标签文本= 1
- 巴顿2点:标签文本= 2
- 巴顿3点:标签文本= 3

Like for an example: When: - Button 1 clicked: Label Text = 1 - Button 2 clicked: Label Text = 2 - Button 3 clicked: Label Text = 3

推荐答案

您可以使用的 Application.OpenForms 收集并添加一个简单的方法到每个表单要求变更

You can use the Application.OpenForms collection and add a simple method to each of your forms that requires the change

在点击按钮添加

foreach (Form f in Application.OpenForms)
{
    if(f Is Form1)
        (f as Form1).ChangeLabel("NewText 1");
    else if (f is Form2)
        (f as Form2).ChangeLabel("NewText 2");
    else if (f is Form3))
        (f as Form3).ChangeLabel("NewText 3");
}

在你想改变增加(例如为Form1)每个窗体

In each form that you want to change add (example for Form1)

.... other code inside your Form1,Form2,Form3 classes...
public void ChangeLabel(string newText)
{
    this.Label1.Text = newText;
}
.... other code inside your Form1,Form2,Form3 classes...


$ B内的其他代码$ b

(当然,您需要到Form1,Form2的,Form3更改为实现您的形式和LABEL1的标签来改变的确切名称类的真实姓名)

(Of course you need to change Form1,Form2,Form3 to the real names of the classes that implements your forms and the Label1 to the exact name of the label to change)

您也可以使您的标签公众和使用像

You can also make your labels public and use a syntax like

(f as Form1).Label1 = "New Text 1";



但我usally宁愿让来自外部的代码不可见的形式托管的控件。

but I usally prefer to leave the controls hosted by the form not visible from external code.

修改

纵观注释下我明白,你有三个按钮形式之一和一种形式与一种标签。而你要更改标签,当你点击这些按钮

Looking at the comment below I understand that you have one forms with three buttons and one form with one label. And you want to change the label when you click on these buttons

然后代码将是以下(使用设计的每个按钮的Click事件设置为相同的事件处理程序。在这个例子中我将其称之为ChangeLabel_Click

Then the code would be the following (Set each button Click event to the same event handler using the designer. In this example I will call it ChangeLabel_Click

protected void ChangeLabel_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    SecondFormType f = Application.OpenForms.OfType<SecondFormType>().FirstOrDefault();
    if(f != null)
    {
        if(btn.Name == "Button1")
           f.ChangeLabel("NewText 1");
        else if(btn.Name == "Button2")
           f.ChangeLabel("NewText 2");
        else if(btn.Name == "Button3")
           f.ChangeLabel("NewText 3");
    }
}

编辑2

代码上面取SecondType的第一种形式是这样,如果你有形式的多个实例打开SecondType你需要一个循环来改变每个表单实例

The code above takes the first form of the SecondType so, if you have more than one instance opened of the form SecondType you need a loop to change every form instance

protected void ChangeLabel_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    foreach(SecondFormType f in Application.OpenForms.OfType<SecondFormType>())
    {
        if(btn.Name == "Button1")
           f.ChangeLabel("NewText 1");
        else if(btn.Name == "Button2")
           f.ChangeLabel("NewText 2");
        else if(btn.Name == "Button3")
           f.ChangeLabel("NewText 3");
    }
}

这篇关于更改按钮单击标签文本以不同的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆