Textbox losing focus after cursor selection moved to end - c#

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

Related

My form loses focus after button click in c#

I have a picturebox in my form that I can move around with my arrow keys, but when I click on a button the form loses focus. How do i get the focus back to the form?
I've already tried with this.focus(); but it didn't work.
Any suggestions?
This seems to make the Button loose the focus to the form.
private void button1_Click(object sender, EventArgs e)
{
this.ActiveControl = null;
}

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

how to unfocus datagridview in winform

I have some buttons, one textbox and a datagridview on a winform.
and i want when form show in the screen to put cursor on the textblock,
for this i use txtName.Focus().
But everytime when the from loads textbox doesn't focus,
indeed dagaridview takes a focus on itself.
how to solve it.
You should set the TabIndex property of the controls in your form (your TextBox for example should have the lowest TabIndex so that when the form loads it will automatically have focus )
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.TabStop = false;
textBox1.TabIndex = 0;
}
hope its help
Simply change the tabindex property of your controls.
Pay attention to use directly the tabindex property, because, if you have controls contained in other controls (groupbox or panels) it could be misleading.
Use the menu View and the TabOrder tool.
Put your textbox first in the taborder. No need to code anything
You have to make sure that the page has been loaded before giving the textbox focus. Therefore, add an event for the Form's Load event.
You can do this on the designer, or in the code behind like so:
this.Load += new EventHandler(Form1_Load);
During the loading event, call Select on your textbox.
private void Form1_Load(object sender, EventArgs e){
txt_Name.Select();
}
The Select command can choose how much of the text you select. For example, select the first letter starting a index 0 would be txt_Name.Select(0,0). More info at the
MSDN.
Alternatively, you can use the tabindex property to 0 to ensure it gets focus first (as per ionden).

How to stop textbox leave event firing on form close

using c# winforms vs2008
I've got a textbox on a form with a method being called from the textBox1_Leave event. The method takes the contents of the textbox in question and populates other textboxes based on the contents.
My problem is that is the user has focus on the text box then clicks the button to close the form (calling this.close) then the form does not close because the textbox leave event gets fired.
If the textbox does not have focus on form close then the form closes fine.
If however a user closes the form by clicking the little X close icon in the top corner the it closes fine all the time with out the textbox leave event being fired.
How can I duplicate the X close functionality so that I can always close the form without the textbox leave event being fired?
The simplest solution is going to be to check which control is actually focused before doing your post-processing - but you can't do it in the Leave handler, because the focus will still be on the text box at that point.
Instead, you need to move your logic to the LostFocus event, which is not in the designer. You'll have to wire it up at runtime:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
}
private void textBox1_LostFocus(object sender, EventArgs e)
{
if (closeButton.Focused)
return;
// Update the other text boxes here
}
}
The LostFocus event happens to fire after the new control receives focus.
Clarification - you might find that it works by putting this logic in the Leave event - if the focus is changed by the mouse. If the keyboard is used instead, you'll get the wrong behaviour. LostFocus is reliable in both cases - the focused control will always be the "new" control. This is documented on MSDN: Order of Events in Windows Forms.
Incidentally, the reason why you're not having this problem with the "red X" is that the X is not actually a control that can receive focus, it's part of the window. When the user clicks that, it's not causing the text box to lose focus, and therefore isn't causing the Leave event to fire.
Another approach:
Use the textbox's validating event instead of it's leave event, then change the button's CausesValidation property to false. You will also have to set the textbox to not cause validation in the button's click event so the validating event will not fire when the form is closing (thanks to #Powerlord for pointing this out).
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.CausesValidation = false;
this.Close();
}
You could also handle the FormClosing event and make sure the e.Cancel argument does not get set to true by the validating events on the other controls on the form. I think they will be fired off before the FormClosing event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = false;
return;
}
}
you can check to see which control has just got focus.
private void textBox1_Leave(object sender, EventArgs e)
{
if (btnClose.Focused)
return;
// go from here
}
Just check if the form owning the textbox is disposing? If it's getting closed, it's disposing. If it's disposing you could simply end the pesky 'leave' event without doing anything. I didn't check it and forgive me, I'm choked on a project of my own so and I was searching myself, so I don't think I'll have time for that.
private void GuiltyTextBox_Leave(object sender, EventArgs e) {
Form formOwningTheTextBox=(Form)((Control)sender).TopLevelControl;
if (formOwningTheTextBox.Disposing || formOwningTheTextBox.IsDisposed) return;
.......
}
I just believe this is going to work with minimum effort and wanted to send a quick answer before I resume searching my own answer.
Write Following line of code in text box leave event on top
if me.closing then
return
end if

Message box causes loss of focus

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

Categories