I have a combobox, if the "Enter" key is pressed will do something. but i want to call this function manually but how i send "ENTER" key as parameter?
private void carga_todos(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
do something
}
im trying this:
carga_todos(null, ??????)
Don't call events manually like that.
Move your logic into a separate method, which you can call whenever you like:
private void carga_todos(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
DoSomething();
}
}
private void AnotherFunctionThatNeedsToDoSomethingToo()
{
DoSomething();
}
private void DoSomething()
{
// stuff to do
}
I agree with Grant Winney. However, if your requirements are to call the handler directly then the following should work:
KeyPressEventArgs kpea = new KeyPressEventArgs((char)Keys.Enter);
carga_todos(null, kpea);
Good luck
J
Related
I would like to call btnSubmit if certain conditions in axTws1_tickPrice are true. How do i do this?
private void btnSubmit_Click(object sender, EventArgs e)
{
//code here
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
Call butSubmit (how do i do this)
}
}
You're better off having a common method that both of your control handlers call, rather than trying to call one handler from another. That way your code is more extensible and testable, and you don't have to worry about the event arguments or senders.
For example:
private void btnSubmit_Click(object sender, EventArgs e)
{
DoStuff();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
DoStuff();
}
}
private void DoStuff()
{
// code to do stuff common to both handlers
}
Multiple options.
Option 1 :
Preferred approach, move common logic to another method.
private void btnSubmit_Click(object sender, EventArgs e)
{
CommonLogic();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
CommonLogic();
}
}
private void CommonLogic()
{
// code for common logic
}
Option 2:
Executing PerformClick() method which generates a Click event for a button.
btnSubmit.PerformClick();
Option 3:
Invoke the event method like any other normal method.
btnSubmit_Click(sender, new EventArgs());
just call it with current parameters.
if (Condition)
{
butSubmit(sender, null)
}
Unbelievable, but
btnSubmit_Click(null,null);
Or other arguments if needed.
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
button1_Click(sender, EventArgs.Empty);
}
}
button1_Click is similar to a normal method which accepts two inputs of type object and EventArgs So you can call them by giving the same parameters. if you are not going to use these arguments inside the method then you can call them by passing null,null don't use null if you want to use e or sender inside the method. in such situation call them as like i suggested above.
Thanks Steve and Hari - this worked well
private void btnSubmit_Click(object sender, EventArgs e)
{
DoStuff();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
DoStuff();
}
}
private void DoStuff()
{
// code to do stuff common to both handlers
}
My original intention in to make enter event for text box to run btnOK_Click event, but after several try I can't make it happen, so I tried another way and try KeyPress for any key but still didn't work, so I made these two simple code, but it still didn't work either;
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//enter key is down
//btnOK_Click(this, e);
System.Windows.Forms.MessageBox.Show("My message here");
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
//enter key is down
//btnOK_Click(this, e);
System.Windows.Forms.MessageBox.Show(((char)Keys.Return).ToString());
}
}
Any suggestion? I read some similar questions and they said to set the IsInputKey property to true but I can't find it anywhere. I use Visual Studio 2008
Two Options :
1) Use Key Up Event As
public void txt_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnOK_Click(sender, e); // or btn.PerformClick();
return;
}
}
2) Make BtnOK the AcceptButton of the form. (Note : this will be for all textboxes in your form)
this.AcceptButton = btnOK;
It seems to me that you are looking to something like this
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)//should be replaced with enter
{
button1.PerformClick();
}
}
NOTE: the code above is on the KeyDown instead of the KeyPress
This code should work, assuming you are using winforms
use Escape key instead of return key:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnOK_Click(sender, e);
MessageBox.Show("My message here");
}
else if (e.KeyCode == Keys.Escape)
{
btnOK_Click(sender, e);
MessageBox.Show(((char)Keys.Escape).ToString());
}
}
private void btnOK_Click(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
}
}
also you can check both keys in KeyDown event.
you can also use
btnOK.PerformClick();
instead of
btnOK_Click(sender, e);
I have such piece of code:
private void button1_Click(object sender, EventArgs e) {
// Do something...
}
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.A) {
// Call button1_Click.
}
}
How do I manage to call the Click event? What should I write instead of
// Call button1_Click.
Thank you!
Events are meant to be triggered not called. So you can trigger the event in your Form1_KeyDown event by performing the button click. I mean
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.A) {
button1.PerformClick();
}
}
Since the button click event handler is just a method, you could just do:
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.A) {
button1_Click(this, e);
}
}
However, it's often a better idea to move the logic into a separate method, and call it from both locations. By refactoring the logic within button1_Click into it's own method, you make it clear that you're calling that logic from either set of event handlers.
private void btnBrowserGo_Click(object sender, EventArgs e)
{
browser.Navigate(txtBrowserURL.Text);
}
The code above directs the browser to the URL address in the textbox. I would like this event to also occur if the user presses the ether key when typing the URL. I have this code (below) but don't know how I would call the above code
private void txtBrowserURL_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
}
}
You could just call the event directly using:
btnBrowserGo_Click(null, null);
However, it would be better if you didn't handle the navigation in the event itself and just called a method. That way, the method could be directly called from anywhere else in the class. This is especially useful if you have more logic in the method.
void NavigateBrowser()
{
browser.Navigate(txtBrowserURL.Text);
}
Then, from any event, you can just call the method.
private void txtBrowserURL_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
NavigateBrowser();
}
}
or
private void btnBrowserGo_Click(object sender, EventArgs e)
{
NavigateBrowser();
}
Try
btnBrowserGo_Click(null, null);
since you don't use last two parameters.
However, it might be good to extract 'logic' into some other method, and use that method from BOTH event handlers.
Your logic here is one-liner, but it might be more...
private void txtBrowserURL_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
btnBrowserGo_Click(btnBrowserGo,EventArgs.Empty);
}
}
I need to have an event fired whenever Enter is pressed inside of the combobox. This is a WPF C# 4.0 control, and I am unable to find a specific event handler to do this. I think I am missing something, as this seems like something that would be included. Is there preexisting code to accomplish this task?
I have also tried:
private void comboBox1_SelectionChanged(
object sender,
SelectionChangedEventArgs e)
{
if (e.Equals(Key.Enter))
{
// Do Something
}
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// do stuff
}
else
{
// do stuff
}
}
private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// do stuff
}
}
or
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// do stuff
}
}
The difference being that KeyUp is when the key is released, KeyDown is when it is first pressed.