I have created a tool bar which has three controls. First one being a text box , an OK button and a Clear button. Essentially I am using this toolbar to search some text. When there are no results found , I pop up a message box informing the user that no results were found. But when the user clicks "OK" button of the message box, the text box looses focus and the focus passes to the next control which is the "OK" button. What should I do to avoid the text box to loose focus. I am using c#.
-
You can't. Clicking on the Ok button forces it to have control (and therefore the textbox loses control).
You can, however, do this on your click event:
MessageBox.Show("asdf"); textBox1.Focus();
EDIT
In response to your comment, I don't think that there's an easy way to return focus to the last control once another control has received focus, and the search and clear buttons will have to receive focus when clicked. You can do this:
private Control _last; private void textBox1_Leave(object sender, EventArgs e) { _last = (Control) sender; } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("asdf"); _last.Focus(); }
Pi : Thanks for the reply. I was wondering if there was any way of solving this problem without setting the focus of the text box explicitly. -
It's not very clear your question, but you can make your control take the focus like this:
textBox1.Focus();
0 comments:
Post a Comment