Message box causes loss of focus - c#

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();
}

It's not very clear your question, but you can make your control take the focus like this:
textBox1.Focus();

Related

WPF Lost-Focus issue in same control

I have a small problem with focusing in .net/wpf. I have written a custom user control which contains a TextBox and a SearchButton. The user-control has a lost focus event, which validate the content of the textbox if the focus is leaving. But now I have the problem, if I click on my search button, the lost focus event of the user control gets fired, even if I click on the button in the custom user control. (The button additionally has the option TabStop="False":
The problem is, that I don't want to fire the event if I click on the button.
Set
Focusable="False"
of your search button and the TextBox will not lose the focus because the button doesn't get the focus.
You can Do this Check in Event like this
protected void LostFocusEvent(object sender, RoutedEventArgs e)
{
if(textBox.Text.Length>0)
{
// if there is any character in TextBox
return;
}
// your validation Code goes here
}

Control is not getting focused on Keyup event

Hi all I have created a dynamic combo box with a Textbox and a button to appear as dropdown style, every thing works fine but I handled keyup event for the textbox so that when user enter some text I will search for the results and display them
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
//Some code to filter my data
textBox1.Focus();
}
But I am unable to set the focus Immediately back to the textbox after results getting displayed so can some one help me
Code I used is from here
http://www.planetsourcecode.com/vb/scripts/showcode.asp?txtCodeId=8554&lngWid=10
I have found that the Focus() method is a bit flaky.
Other options:
textBox1.Select(textBox1.Text.Length - button1, 1);
...or simply:
textBox1.Select();
If you can verify that something else is going wrong, then this might be off base, otherwise you might simply be fighting weirdness.

Changing focus from on textbox to another, when tabpages is changed in C#?

I have several tabs in a form. Each tab has one textbox. When I enter tabpage1 I have managed to set the focus on the textBox1. When I press a button in tabpage1 I jump to a random tab in the controller. What I want now is to have the focus set on textBox in the active tabpage. I have tried using tabpage_Enter event, but it does not seem to work. My code look like this :
private void tabPage2_Enter(object sender, EventArgs e)
{
textBox2.Select();
}
Any suggestions?
I think you need to use SelectedIndexChanged event of TabControl instead of _Enter, using Enter event, focus will change to textBox2 every time the cursor enter the tabPage control.
You can use the Focus() method set the focus on a textbox. I would probably set on the tabPage_Enter event.
private void tabPage_Enter(object sender, EventArgs e){
{
var tab = sender as tabPage;
if(!tab.Focused) tab.focus();
}

Textbox losing focus after cursor selection moved to end

I have a form that loads, hits a button (to add text to a textbox), moves the cursor to the end of the textbox text, and then sets the active control to the textbox, so the user can immediately start typing. The textbox is populated fine, but the form loses its focus. This is only with the selection line in there, if I take it out, it works fine. The user has to click on the form to make it active. Any ideas?
private void createNewFolder_Load(object sender, EventArgs e)
{
addDate.PerformClick();
folderNameTextBox.Select(folderNameTextBox.Text.Length, 0);
this.ActiveControl = folderNameTextBox;
this.Focus();
}
Focusing cannot work in the Load event, the form is not yet visible. By far the simplest way is to just give the control the lowest TabIndex. Or use the Select() method:
private void createNewFolder_Load(object sender, EventArgs e)
{
addDate.PerformClick();
folderNameTextBox.Select(folderNameTextBox.Text.Length, 0);
folderNameTextBox.Select();
}
Instead of:
this.ActiveControl = folderNameTextBox;
Try:
folderNameTextBox.Focus();
If the textbox is still out of focus, try selecting it using:
folderNameTextBox.Select();

How do you return the focus to the last used control after clicking a button in a winform app?

I'm working on a windows forms application (C#) where a user is entering data in a form. At any point while editing the data in the form the user can click one of the buttons on the form to perform certain actions. By default the focus goes to the clicked button so the user has to click back on to the control they want to edit in order to continue modifying the data on the form. What I need to be able to do is return the focus to the last edited control after the button click event has been processed. Here's a sample screenshot that illustrates what I'm talking about:
The user can be entering data in textbox1, textbox2, textbox3, etc and click the button. I need the button to return the focus back to the control that most recently had the focus before the button was clicked.
I'm wondering if anyone has a better way of implementing this functionality than what I've come up with. Here's what I'm doing right now:
public partial class Form1 : Form
{
Control _lastEnteredControl;
private void textBox_Enter(object sender, EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Do something here");
_lastEnteredControl.Focus();
}
}
So basically what we have here is a class variable that points to the last entered control. Each textbox on the form is setup so the textBox_Enter method is fired when the control receives the focus. Then, when the button is clicked focus is returned to the control that had the focus before the button was clicked. Anybody have any more elegant solutions for this?
For a bit of 'simplicity' maybe try.
public Form1()
{
InitializeComponent();
foreach (Control ctrl in Controls)
{
if (ctrl is TextBox)
{
ctrl.Enter += delegate(object sender, EventArgs e)
{
_lastEnteredControl = (Control)sender;
};
}
}
}
then you don't have to worry about decorating each textbox manually (or forgetting about one too).
You could do the following
Change the button to a label and make it look like a button. The label will never get focus and you don't have to do all the extra coding.
I think what you're doing is fine. The only thing I could think of to improve it would be to store each control into a stack as they are accessed. That would give you a complete time line of what was accessed.
Your approach looks good. If you want to avoid having to add an the event handler to every control you add, you could create a recursive routine to add a GotFocus listener to every control in your form. This will work for any type of control in your form, however you could adjust it to meet your needs.
private void Form_OnLoad(object obj, EventArgs e)
{
AddGotFocusListener(this);
}
private void AddGotFocusListener(Control ctrl)
{
foreach(Control c in ctrl.Controls)
{
c.GotFocus += new EventHandler(Control_GotFocus);
if(c.Controls.Count > 0)
{
AddGotFocusListener(c);
}
}
}
private void Control_GotFocus(object obj, EventArgs e)
{
// Set focused control here
}
Your implementation looks good enough -- what I do want to know is why you want to do this in the first place? Won't it be preferrable for the focus to cycle back to the first entry? Is the data in the last text box so malleable that once they click the button it is "remembered"? Or do you have some sort of operation that the button does to that specifici text box data -- in that case shouldn't the focus go to a subsequent control instead?
I'm interested in finding out why you want to do this in the first place.
Yeah, I admit the requirement is a bit unusual. Some of the information that the users will be entering into this application exists in scans of old documents that are in a couple of different repositories. The buttons facilitate finding and opening these old docs. It's difficult to predict where the users will be on the form when they decide to pull up a document with more information to enter on the form. The intent is to make the UI flow well in spite of these funky circumstances.
Create a class called CustomTextBox that inherits from TextBox. It has a static variable called stack. When the textbox loses focus push onto the stack. When you want to find the last focused control then just pop the first item from the stack. Make sure to clear the static Stack variable.

Categories