更改时自动更新全局变量的标签
这是我的程序。
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static int count = 0;
public Form1(){InitializeComponent();}
private void button1_Click(object sender, EventArgs e)
{
count += 1;
label1.Text = Convert.ToString(count);
}
private void button2_Click(object sender, EventArgs e)
{
count -= 1;
label1.Text = Convert.ToString(count);
}
}
}
现在...我有一个程序,每当我们按下两个按钮之一时,数字就会加或减 1,并将值保存到全局变量 count 中,然后将其显示在 label1 中。
假设我想更改另一个标签(label2)的值,其中我还想在每次 label1 更改时显示计数变量值的内容。
所以有一种方法,使用按钮中的事件,可以这样完成:
private void button1_Click(object sender, EventArgs e)
{
count += 1;
label1.Text = Convert.ToString(count);
label2.Text = Convert.ToString(count);
}
private void button2_Click(object sender, EventArgs e)
{
count -= 1;
label1.Text = Convert.ToString(count);
label2.Text = Convert.ToString(count);
}
所以这是问题......
但是假设我想更新 label2 的值,而不是通过按钮事件,而是以某种方式更新该值每当计数变量发生变化时,都会自动从计数变量中获取。
请帮忙。
So here is my program.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static int count = 0;
public Form1(){InitializeComponent();}
private void button1_Click(object sender, EventArgs e)
{
count += 1;
label1.Text = Convert.ToString(count);
}
private void button2_Click(object sender, EventArgs e)
{
count -= 1;
label1.Text = Convert.ToString(count);
}
}
}
Now... I have a program which add or subtract number by 1 whenever we press one of two buttons and save the value to global variable count and then displaying it in label1.
Lets say I want to change value of another label (label2) in which I want to also display the content of count variable value every time the label1 changes.
So there is one way, use event from the button which can be done like this:
private void button1_Click(object sender, EventArgs e)
{
count += 1;
label1.Text = Convert.ToString(count);
label2.Text = Convert.ToString(count);
}
private void button2_Click(object sender, EventArgs e)
{
count -= 1;
label1.Text = Convert.ToString(count);
label2.Text = Convert.ToString(count);
}
So here is the question...
But lets say I want to update the value of label2 not from event of buttons but somehow so it would update the value automatically from count variable whenever it changes.
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下使用属性
当您想更改计数时,只需使用属性即可
Use property for this situation
And when you want to change count, just use property instead
您无法将数据绑定到
static
成员,也无法将数据绑定到字段(除非您使用自定义绑定模型);一种选择是拥有一些您也可以进行数据绑定的上下文实例,例如:然后可以将
Counters
实例传递到您的表单,并且数据 -绑定:您当然可以有一个
Counters
的static
实例,但同样:这不是一个很好的设计;static
通常意味着您正在走一条古怪的捷径。You can't data-bind to a
static
member, and you can't data-bind to a field (unless you use a custom binding-model); one option would be to have some contextual instance that you can data-bind too, for example:Then make it possible to pass a
Counters
instance to your form, and data-bind:You could of course have a
static
instance of theCounters
, but again : that isn't a great design;static
often means you're taking a hacky short cut.使用房产怎么样?那么它不能再是静态的了,这是一个要求吗?
[编辑:如果有要求,您可以再次将 _count 设置为静态。]
每当
count
被修改时,这将始终触发事件CountChanged
,因此您可以附加自定义事件每个控件的处理程序。How about using a property? It can't be static anymore then, that's a requirement?
[Edit: You could make _count static again in case that IS a requirement.]
This will always trigger the event
CountChanged
whenevercount
is modified, so you can attach your custom event handlers for each control.