主要表格控制和限制 [英] main Form control and restriction
问题描述
C#,Visual Studio
我有一个表格A,其中显示了计数器的值和打开表格B的选项.
当用户单击表单B时,表单B出现两个按钮,
按钮1:增加值,以A形式增加计数器值
按钮2:减少值,以A形式减少计数器值
问题是,当用户创建多个表格B并单击增加或减少时,仅B的第一个实例应增加或减少值,并限制其他形式的表格B来更新计数器.
你们可以在这方面给我链接有用的文章还是帮我吗?
C#,Visual Studio
i have a form A which shows value of counter and option to open form B.
when user clicks on form B, form B appears with two buttons,
button 1 : increase value, increases counter value in form A
button 2 : decrease value, decrease counter value in form A
the problem is that when user creates multiple Form B and click on increase or decreases, only first instance of B should increase or decrease value and restricting other instance of form B to update counter.
Can u guys link me useful articles or help me in this context
推荐答案
在FormB中创建Changed事件:
Create a Changed event in FormB:
public event EventHandler Changed;
protected virtual void OnChanged(EventArgs e)
{
EventHandler eh = Changed;
if (eh != null)
{
eh(this, e);
}
}
在FormA中创建一个类级别的变量:
Create a class level variable in FormA:
private FormB formB = null;
当要求您打开FormB时:
When you are asked to open FormB:
if (formB == null)
{
formB = new FormB();
formB.FormClosed += new FormClosedEventHandler(formB_FormClosed);
formB.Changed += new EventHandler(formB_Changed);
formB.Show();
}
else
{
formB.Focus();
}
事件处理程序很简单:
The event handlers are simple:
void formB_Changed(object sender, EventArgs e)
{
// ... update counters
}
void formB_FormClosed(object sender, FormClosedEventArgs e)
{
formB = null;
}
这篇关于主要表格控制和限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!