WinForm应用程序:在另一个表单关闭后,按钮禁用了吗? [英] WinForm app: button disable after another form closed?
问题描述
您好WinForm App专家,
你能告诉我这是否可行:
1. Form1 Button1_Click-> Form2 Show(),Form1 Button1禁用
//上面的第一个事件显然没有问题!
2. Form2已关闭-> Form1 Button1启用
//直到现在我都无法做到这一点
为什么我要这样做?因为我必须使用Show()方法而不是ShowDialog(),所以form1和form2可以并行操作.但是问题是,我希望仅在关闭form2时才启用form1中的button1.
是否可以以注释形式控制控件?我看到了有关c#的文章,但是当我在C ++/CLI中翻译"它们时,它似乎不起作用.
因此,我使用了varible来启用button1.同样在关闭Form2时,请执行a = 1;
我想在form1的事件中获得a = 1,但找不到合适的那个.
当我的想法不可能的时候.您能给我您的建议吗?
提前谢谢!
大家好,
现在它可以与Marcus的c#代码很好地配合使用.但我也想将其写入C ++/CLI,但我不太了解(o,arg)=>"是什么.是.是所谓的"lambda表达式"吗?应该如何用C ++/CLI编写?
我至少知道"var form2Obj = new Form2();"在C ++/CLI中应该这样:
Hello WinForm App experts,
Could you tell me if this is possible:
1. Form1 Button1_Click -> Form2 Show(), Form1 Button1 disable
// The first event above is clearly no prolem!
2. Form2 closed -> Form1 Button1 enable
// I can''t make this till now
Why I want to do this? Because I must use Show() method instead of ShowDialog(), so that the form1 and form2 can be operated parallelly. But the problem is that, I want the button1 in form1 only to be enabled while form2 is closed.
Is controlling a control in anoter form possible? I saw articles for c#, but when I "translate" them in C++/CLI, it doesn''t seem to work.
So I used varible to enable the button1. Also when Form2 Closing, do a=1;
I want get the a=1 in an event for form1, but I can''t find a suitable one.
When my idea is impossible. Could you give me your advices?
Thank you in advance!
Hi mates,
now it works well with Marcus'' c# code. But I also want to write it into C++/CLI, and I don''t quite understand what "(o, arg)=>" is. Is it the so-called "lambda expression"? How should it be written in C++/CLI?
I know at least that the "var form2Obj = new Form2();" should like this in C++/CLI:
auto form2Obj = gcnew Form2();
感谢您的帮助!
Thank you for helping!
推荐答案
使用基于事件的通信.这样,您的用户界面就符合正确的编码规范.
Use event based communication. That way your UI conforms to proper coding etiquette.
// In Form1 code
private void OnOpenForm2Click(object sender, EventArgs e)
{
var form2Obj = new Form2();
form2Obj.Shown += (o, args) => { btnOpenForm2.Enabled = false; };
form2Obj.FormClosed += (o, args) => { btnOpenForm2.Enabled = true; };
form2Obj.Show();
}
马库斯的答案会更好.
[/编辑]
这可以通过将按钮控件从Form1传递到Form2来完成.
示例:
Marcus''s Answer would be better.
[/Edit]
This can be done by passing the button control from Form1 into Form2.
Example:
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
// pass in the button into Form2
Form2 f = new Form2(ref button1);
f.Show();
}
然后,在Form2关闭期间,再次使按钮可见.
示例:
Then, during Form2 is closing, make the button visible again.
Example:
public partial class Form2 : Form
{
Button btForm1 = null;
// Receive button1 control from Form1
public Form2(ref Button bt)
{
btForm1 = bt;
InitializeComponent();
FormClosing += new FormClosingEventHandler(Form2_FormClosing);
}
void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
// make the button visible again
btForm1.Visible = true;
}
}
这篇关于WinForm应用程序:在另一个表单关闭后,按钮禁用了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!