I have a form with a textbox called 'tbWO.' This field is used to enter a Purchase Order Number. I also have a button control called 'btnFill.' When btnFill is clicked, it fills a dataset with a parameter from 'tbWO.'
I would like to be able to press 'ENTER' in the 'tbWO' textbox (after a Purchase Order # is entered) and have it fire the btnFill_Click event I mentioned above.
I tried with this bit of errant, badly written code - but, it's just not working properly, i.e., at all, or how I think it should work. Anyway, the code is below; in all it's glory.
private void txtWO_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnFill.Click += new EventHandler(btnFill_Click);
}
}
I will admit confusion on using 'new EvenHandler( ?? ). Fairly new to C# (as is probably blantantly obvious.)
Any help, links, suggestions - all are greatly appreciated.
Thanks.
Jasoomian
you could do this...
private void txtWO_KeyUp(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
btnFill_Click();
}
}
As a rule, I abhor mapping one event handler to another. Instead, write a separate function, and have both event handlers invoke that separate function. Something like this:
private void txtWO_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
AcceptInput();
}
}
private void btnFill_Click(object sender, EventArgs e)
{
AcceptInput();
}
private void AcceptInput()
{
// Do clever stuff here when the user presses enter
// in the field, or clicks the button.
}
Granted, you may feel differently, but it accomplishes the same thing, but with (IMO) far more readable code. But it's been my experience that criss-crossing event handlers is very sloppy and leads to maintenance headaches out the wazoo.
Related
The if statement is being checked until I hit enter then it goes straight to another method. My guess is there is something else on the form that is getting triggered when I hit enter but I can't find it despite my search.
I want to not have to put a button on the form to call this function, the button I had worked but I just want to be able to hit enter from my textbox input.
Here is my code below:
private void textBox1firstName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
searchAD();
}
}
private void textBox2lastName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
searchAD();
}
}
searchAD() is not getting called despite hitting enter. Any suggestions? Thank you!
The form has a KeyDown event, but also has a "AcceptButton" property, which hooks the [Enter] keypress and can call an event handler. Check if there are event-handlers attached to either of those on the form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
MessageBox.Show("called btn 0 click..");
KeyPressEventArgs e0 = new KeyPressEventArgs('0');
textBox1_KeyPress(sender, e0);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("called txtbox_keypress event...");
}
}
Am sorry if this is a silly question,I have just started to learn windows forms, I still find material on the internet confusing.I want to implement calculator. So when number button is pressed it should be filled in textbox. So I thought calling textBox1_keypress() event from button click event would work??? but its not working,
I can manually write the logic in button click event to fill text in text box but if i do so, i have to do the same thing in button1_KeyPress event too. so it would be duplication of code right??...so i thought solution was to call textBox1_KeyPress() event from both button click event and button key press event...but its not working .So what should i do???..is there any other approach which should i follow.
so it would be duplication of code right??
Yes, it would be. So you can do
private void btn0_Click(object sender, EventArgs e)
{
CommonMethod(e);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
CommonMethod(e);
}
private void CommonMethod(EventArgs e)
{
//Your logic here.
}
The TextBox KeyPress event handler (textBox1_KeyPress) is called after the user presses a key. The KeyPressEventArgs parameter includes information such as what key was pressed. So calling it from your btn0_Click method isn't going to set the text for the TextBox.
Rather, you want to (probably) append whatever number the user pressed to the text already present in the TextBox. Something like
private void btn0_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
might be closer to what you're trying to accomplish.
You could put the logic in an extra function like so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
NumberLogic(0),
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// I don't know right now if e contains the key pressed. If not replace by the correct argument
NumberLogic(Convert.ToInt32(e));
}
void NumberLogic(int numberPressed){
MessageBox.Show("Button " + numberPressed.ToString() + " pressed.");
}
}
You don't want to tie the events together like that.
A key-press is one thing, handled in one way.
A button click is something totally different and should be handled as such.
The basic reason is this,
The button doesn't know what number it is, you need to tell it that.
A key-press on the other hand, knows what number was pressed.
If you REALLY want to, for some reason, you could use SendKeys to trigger your key-press event in a round-about way, from the button.
SendKeys.SendWait("0");
I can suggest to you to use an Tag Property of the Buttons. Put in it the value of each button in Design mode or in Constructor, create one button event handler for all buttons and use Tag value:
Constructor:
button1.Tag = 1;
button2.Tag = 2;
button1.Click += buttons_Click;
button2.Click += buttons_Click;
Event hadler:
private void buttons_Click(object sender, EventArgs e)
{
textBox1.Text = ((Button)sender).Tag.ToString();
}
I know this has been asked hundred of times, but I haven't been able to find a solution that helps me. I'm using a barcode scanner and I want to be able to get the keys that are typed using just the keydown events. For some reason, I can't use both keydown and keypress events (my keypress events won't run).
I need to be able to get the characters, including hyphens, uppercase letters and dots and also need to detect the enter key.
These are my listeners:
form.KeyDown += new KeyEventHandler(Input_KeyDown);
form.KeyPress += new KeyPressEventHandler(Input_KeyPress);
And these are my methods:
private void TimedOut(object sender, EventArgs e)
{
_barcode = "";
}
private void Input_KeyDown(object sender, KeyEventArgs e)
{
_timer.Stop();
_timer.Start();
if (e.KeyData == Keys.Enter)
{
if (!_barcode.Equals(""))
{
this.BarcodeScanned(_barcode, new EventArgs());
}
}
else
{
}
}
private void Input_KeyPress(object sender, KeyPressEventArgs e)
{
_timer.Stop();
_timer.Start();
_barcode += e.KeyChar;
}
Your code above works...on a blank form. However there are several things that can interfere with the key events, especially when there are other controls on the page. Make sure that
The AcceptButton property isn't set on the form (this will trap the Enter key)
That there are no controls on the form with TabStop set to true (might not be viable but give it a go)
That the form has focus when you're typing (unlikely given the description but check anyway)
That focus is not otherwise on any control in the form when typing, e.g., a TextBox
That no other controls are trying to process the KeyPress or KeyDown events and that no other custom events are configured/set anywhere else in your code
One thing I notice is that you are registering the events like so;
form.KeyDown += new KeyEventHandler(Input_KeyDown);
This implies that you are instantiating this form from another place and trying to get it to send its key events to the calling code. Are you sure that the form instance is persisted/saved to a private class level variable or some such?
Hopefully there is a pretty easy solution to this.
I have a program where the user has to click next a lot to move through a setup process.
What's the best way to maintain focus on the next button while the user enters info in text boxes?
Well, that depends on the design, but it shouldn't be a problem marking your button as "default". In winforms it's called "AcceptButton" if I'm not mistaken.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton%28v=VS.100%29.aspx
A way around is handling the return key for each text box. So when the user presses enter key, it can simulate pressing next button.
First move your code in the next button to a void.
private void GoNext()
{
//Do something
}
private void btnNext_Click(object sender, EventArgs e)
{
GoNext();
}
Now handle the key press.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
e.Handled = true;
GoNext();
}
}
You may also draw focus rectangle in the lost focus event of next button for visual purposes.
I have a search text box on my WPF Windows. Whenever, the user presses enter key after writing some query in the textBox, the process should start.
Now, I also have this Search button, in the event of which I perform all this process.
So, for a texBox:
<TextBox x:Name="textBox1" Text="Query here" FontSize="20" Padding="5" Width="580" KeyDown="textBox1_KeyDown"></TextBox>
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
//how do I fire the button event from here?
}
}
It is possible but rather move your search logic into a method such as DoSearch and call it from both locations (text box and the search button).
Are you talking about manually invoking buttonSearch_onClick(this, null);?
You can Create a Common Method to do search for ex
public void MySearch()
{
//Logic
}
Then Call it for diffetnt places you want like...
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
MySearch();
}
}
private void buttonSearch_onClick(object sender, EventArgs e)
{
MySearch();
}
A event can not be fired/raised from outside the class in which it is defined.(Using reflection you can definitely do that, but not a good practice.)
Having said that, you can always call the button click event handler from the code as simple method call like below
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
OnSearchButtonClick(SearchButton, null);
}
}