سؤال

private void textBoxColor_KeyDown(object sender, KeyEventArgs e) 
{ 
//do something 
} 
private void btnSaveSet_Click(object sender, EventArgs e) 
{ 
//how can i invoke the KeyDown event 
}

In my test WinForm, i have a TextBox named textBoxColor and a Button named btnSaveSet. I add KeyDown event to the textBox and Click event to the Button.

هل كانت مفيدة؟

المحلول

Generally you shouldn't try to call event handlers within other event handlers. If you want to share a method, then you should put it in another method and call that from each of the events.

For example:

private void textBoxColor_KeyDown(object sender, KeyEventArgs e) 
{ 
    SomeMethod();
} 

private void btnSaveSet_Click(object sender, EventArgs e) 
{ 
    SomeMethod();
}

private void SomeMethod()
{
    // Put your shared event code here.
}

You can also pass the event arguments into this method if you'd like, by adding these as parameters to SomeMethod.

نصائح أخرى

Very simple, call method textBoxColor_KeyDown from btnSaveSet_Click

private void textBoxColor_KeyDown(object sender, KeyEventArgs e) 
{ 
//do something 
} 
private void btnSaveSet_Click(object sender, EventArgs e) 
{ 
    textBoxColor_KeyDown(sender,null);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top