是否可以在CheckedChanged事件参数中声明关键事件参数 [英] Is it Possible to declare Key Event Args in CheckedChanged Event Args
本文介绍了是否可以在CheckedChanged事件参数中声明关键事件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好任何人都可以帮忙。
我有 CheckBox
和 TextBox
在Windows窗体中,我想如果我做( CheckBox.checked == false
)和 TextBox.Text
不是null,那么我想要 TextBox.clear()
然后我想要 TextBox Key.Enter
eventargs,然后 TextBox.Enable == false
。
这是代码
Hello can any one help.
I have CheckBox
and TextBox
in Windows Forms, I would like to have if i make (CheckBox.checked==false
) and TextBox.Text
is not null, then i would like to make TextBox.clear()
and next step I would like TextBox Key.Enter
eventargs, then TextBox.Enable==false
.
Here is the code
private void checkbox_CheckedChanged(object sender, EventArgs e)
{
if (checkbox.Checked == true)
{
this.textBox1.Enabled = true;
}
if (checkbox.Checked == false)
{
textBox1.Clear();
// here i want textBox1.KeyEnterEventArgs "Enter" key pressed ..........
this.textBox1.Enabled = false;
}
}
是否可以这样做,谢谢提前
Is it possible to do this please, thanks in advance
推荐答案
不要模拟按键。如果它会执行任何操作,那么无论如何都会附加一个事件处理程序。直接调用该方法而不是让事件调用它。
评论中有两个独立的错误:
1.e.KeyCode = Keys.Enter;
是不可能的,因为< a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode.aspx> e.KeyCode [ ^ ]是只读的。这样做你想要达到什么目的?
2.textBox_KeyDown(发件人,e);
不起作用,因为你的e
的类型是 EventArgs [ ^ ],但您调用的方法需要 KeyEventArgs [ ^ ]作为第二个参数。
这就是你可以这样做的方法:
Don''t simulate pressing of a key. If it will do anything, you have an event handler attached anyway. Call that method directly instead of making the event call it.
There are two independent errors in your comment:
1.e.KeyCode = Keys.Enter;
is not possible since e.KeyCode[^] is readonly. What do you want to achieve by doing this?
2.textBox_KeyDown(sender,e);
doesn''t work because here youre
is of type EventArgs[^], but the method you''re calling expects a KeyEventArgs[^] as second parameter.
This is how you could get that to work:
private void MethodNameDescribingTheActionToTake()
{
// Do whatever needs to be done.
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
MethodNameDescribingTheActionToTake();
}
private void checkbox_CheckedChanged(object sender, EventArgs e)
{
if (checkbox.Checked == true)
{
this.textBox1.Enabled = true;
}
if (checkbox.Checked == false)
{
textBox1.Clear();
MethodNameDescribingTheActionToTake();
this.textBox1.Enabled = false;
}
}
[/编辑]
这篇关于是否可以在CheckedChanged事件参数中声明关键事件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文