pat562456

All they taught in college was Java. Now that I've gradutated and have the time I've been having some really big fun with Visual Studio 2005 Pro :-)

Question: Using the above IDE, I want to give an "Enter" button focus whenever the keyboard "Enter" key is pressed regardless of what control has focus. This is for a Windows Application. I tried setting the Form property "AcceptButton" to the "Enter" button in question and setting the "KeyPreview" property to true but it didn't work. Any help would be greatly appreciated!

BTW - I looked a the VS forums but none looked like they were designed for this sort of question. If there is one please point me in that direction.

Thanks

Pat



Re: Windows Forms General "Enter" button focus

tkerns

Here is what I tried:

I created a form with a TextBox and a Button. I created an eventhandler for the Button's Click event. I set the Form's AcceptButton property to the Button. Then when I ran the application, hitting the Enter key while the TextBox had focus, the Button's Click event was raised.

It didn't matter whether I set the Form's KeyPreview to True or not.

Tony





Re: Windows Forms General "Enter" button focus

pat

Hi Tony,

Thanks for your reply. Try adding a couple more buttons to the form. You can set the "Enter" button with initial focus by setting it's "tab" to 0 but once you give focus to another button or the textBox then pressing enter doesn't do anything (for me, that is. I could be doing something wrong). I did the exact same thing as you where I'm writing "Enter button pressed" to the text box (from the "Enter" button click event", etc. If I press another button and fire its click event and then tab to the "Enter" button pressing the "Enter" key works.

Let me know what you think.

Thanks

Pat





Re: Windows Forms General "Enter" button focus

nobugz

The AcceptButton property is used to automatically "press" the button when you type the Enter key. If you just want to set the focus to the button, do it like this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Enter) {
btnOk.Focus();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}






Re: Windows Forms General "Enter" button focus

pat

Thank You nobugz!

That snippet worked with this modification:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Enter) {
btnOk.Focus();

btnOk_Click(this, new EventArgs());
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

Now, no matter which control in the form has focus (textBox, radioButton, etc), if the user presses the "Enter" key, the event handler for btnOK gets fired. That's what I was looking to do.

Thanks again!

Pat






Re: Windows Forms General "Enter" button focus

nobugz

The Form.AcceptButton property is very specifically designed to do what you're doing. Actually, the code I posted is not correct. It should first check if the control that has the focus is not a Multi-line TextBox or RichTextBox. Rather than fixing it, try AcceptButton again, there is no known problem with it.





Re: Windows Forms General "Enter" button focus

pat

Nope,

I have three buttons and a label on my form with each button's click event writing its number to the label (i.e. -

label1.Text = "Button 3 pressed"). Through Visual Studio I'm setting the AcceptButton property for the form to button1. This is what shows up in the code:

this.AcceptButton = this.button1;

If I click on button2 it writes its number to the label and retains focus (remains highlighted). With your code commented out pressing the "Enter" key on the keyboard just resubmits button2's number to the label. I have to give focus to button1 w/ the tab key or by clicking on it in order for the enter key to fire the click event for button1.

From what I read setting the AcceptButton property should intercept the keypress event for the "Enter" key and redirect it to the Click event for whatever button is the argument to it (AcceptButton). This is not happening here.

I even tried using a KeyPressEvent for Button2:

private void button2_KeyPress(object sender, KeyPressEventArgs e)

{

if (e.KeyChar == '\r')

button1_Click(this, new EventArgs());

}

No luck!





Re: Windows Forms General "Enter" button focus

nobugz

There are no previously reported problems with the AcceptButton property. Please submit your problem to Product Feedback. They would need a sample form that reproduces the issue. Post both the Form1.cs code and the Form1.Designer.cs code. You can access the latter by clicking the Show All Files icon in Solution Explorer.





Re: Windows Forms General "Enter" button focus

pat

I will.

In the meantime, here's the biggest hack you've ever seen (I put this in the each event handler on the form):

button1.Focus();

I wouldn't think of using something like that when I actually start doing this for a living - but it works :-)

Thanks for all your help nobugz!

Regards

Pat