如何以2种不同形式链接2个标签 [英] How to link 2 labels in 2 different forms

查看:76
本文介绍了如何以2种不同形式链接2个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在窗体1上运行的计时器,其中一个名为"timenumber"的标签显示了时间,我还有一个带有标签"timer"的第二个窗体.如何将它们链接在一起,使它们在同一时间具有相同的值.窗体1被用作控制器,而窗体2是在另一台监视器中显示的窗体.

I have a timer running on form 1 with a label called "timenumber" showing the time, i also have a second form that has a label "timer". How do i link these in a way that they both so the same value at the same time. Form 1 is used as a controller and the form 2 is the one that is displayed in another monitor.

推荐答案

选项1
使用其构造函数将对Form1中的倒数标签的引用传递给Form2,然后使用此引用来订阅标签的TextChanged事件:

Option 1
Pass a reference to the Count Down Label in Form1 to Form2 using its constructor, then use this reference to subscribe to the Label's TextChanged event:

Form1 中:

In Form1:

private int CountDown = 100;

private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2(this.[The Counter Label]);
    form2.Show();
    this.timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.[The Counter Label].Text = CountDown.ToString();
    if (CountDown == 0)
        this.timer1.Enabled = false;
    CountDown -= 1;
}

Form2 中:

In Form2:

public form2() : this(null) { }

public form2(Control timerCtl)
{
    InitializeComponent();
    if (timerCtl != null) {
        timerCtl.TextChanged += (s, evt) => { this.[Some Label].Text = timerCtl.Text; };
    }
}

选项2
使用可以设置为控件引用的Form2的公共属性.在创建Form2的新实例之后,立即在Form1中设置此属性.
但是,这意味着Form1需要了解有关Form2中的此属性:

Option 2
Use a Public Property of Form2 that can be set to a Control reference. Set this property in Form1 right after a new instance of Form2 has beed created.
This, however, implies that Form1 needs to know about this property in Form2:

Form1 中:

In Form1:

private int CountDown = 100;

private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.Show();
    form2.CountDownControl = this.[The Counter Label];
    this.timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.[The Counter Label].Text = CountDown.ToString();
    if (CountDown == 0)
        this.timer1.Enabled = false;
    CountDown -= 1;
}

Form2 中:

In Form2:

private Control timerCtl = null;

public Control CountDownControl {
    set { this.timerCtl = value;
        if (value != null) {
            this.timerCtl.TextChanged += (s, evt) => { this.[Some Label].Text = timerCtl.Text; };
        }
    }
}

还有许多其他选项.

您还可以在Form2中使用公共属性",并直接从Timer.Tick事件中设置此属性.与第二个示例一样,公共属性可以将其控件之一的Text属性设置为该属性的值.
我不太喜欢这个选项,尽管它是可用的.

You could also use a Public Property in Form2 and set this property directly from the Timer.Tick event. The public property, as in the second example, could set the Text property of one of its controls to the value of the property.
I don't like this option very much, nonetheless it's available.

Form1 中:

In Form1:

private int CountDown = 100;

private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.Show();
    this.timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.[The Counter Label].Text = CountDown.ToString();
    form2?.CountDownValue = CountDown;
    if (CountDown == 0)
        this.timer1.Enabled = false;
    CountDown -= 1;
}

Form2 中:

In Form2:

public int CountDownValue {
    set { this.[Some Label].Text = value.ToString(); }
    }
}

您还可以在Form1中具有一个自定义事件,该事件可以由Form2订阅或实现 INotifyPropertyChange .但这是与Option1相同的东西.

You could also have a custom event in Form1 that Form2 could subscribe, or implement INotifyPropertyChange. But this is, give or take, the same thing as Option1.

这篇关于如何以2种不同形式链接2个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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