CF - Set Focus to a specific control - c#

I have a Form that has a panel with some textBoxes and checkBox that is outside the panel.
Every time the Form is loaded the checkBox has focus.
I have put en event handler when form loads and tried to set focus on first textbox instead having it on the checkbox.
this.Activated += new EventHandler(form_Activated);
in the method i try to set the focus on the first textbox in the panel
private void form_Activated(object sender, EventArgs e)
{
if (this.parametersPanel.Controls.Count > 0)
{
this.parametersPanel.Focus();
(this.parametersPanel.Controls[0]).Focus();
}
}
This does not work, can some1 help me pls?

try setting the focus directly on the textbox instead of using panel's controls index.

In desing mode, select your control and set it's tabindex to 0

Option 1:
Put this in the form's load event:
this.ActiveControl = myTextBox;
Option 2:
Put this in the form's load event:
this.Show();
myTextBox.Focus();
Focus() will not work until the TextBox is visible.

Instead of Activated try Shown

Yoc can use the solution provided by Ahmet. Since you want the text box to have the focus...setting the tab index to zero will do that.
Also you can use the textbox'e focus method to set the focus, in the form's load event....

Related

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

Change the default selected item to none

I have a WindowsForm that contains a lot of UserControl.
Each UserControl has a PictureBox, a few TextBox and a Button.
As soon as my program loads, my first TextBox is Highlighted in blue and I don't want that.
In fact I don't want anything to be selected at all.(Buttons, TextBox etc..)
I've looked into the properties but I can't find exactly how to totally remove this 'Feature'.
All my TextBox are ReadOnly but can become Write/Read while the program is running.
Any Idea how i could do this ?
Thanks in advance.
Update
Changing the TabStop properties to false did part of the job since it doesn't allow selection with tab at all. But I don't want to block the user from using tab to navigate between boxes I just don't want any selection when I run the program. Is there another way ?
Thanks for your time again.
On form Load set Focus to any label or any other control which is not tab stop on the form
private void Form1_Load(object sender, EventArgs e)
{
this.ActiveControl = label1;
}
How to remove the focus from a TextBox in WinForms?
You need to set TabFocus to false for textboxes etc
//textBox1.TabFocus = false;
textBox1.TabStop = false;
comboBoxName.SelectedIndex = -1;

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).

Where does tab goes?

I am working on winform project. At runtime when I am moving through different controls through tab key ; after one button my tab disappears for 2 clicks. I tried all things to fix this. I set tabstop=false for all contols in winform but still I am getting same problem.
Then I decided to add following code:
Control nextControl = this.GetNextControl(this.guipnlReportPatientMeasurementDetails.Controls[10], true);
where GetNextControl property gets the name of the control where my control will go after pressing tab key and Controls[10] is the button. So where should I place above piece of code so that I would get the name of next control. Should it be in button_click event or somewhere else ?
Guys please suggest.
You should set TabIndex property for each control according to the Tab order you want to impose (you can set it through designer).
Set TabStop = false only for those controls you want to exclude from Tab selection.
There's also a useful button that shows TabIndexes on the form:
You are digging yourself a hole. Find out what the real problem is, there's some kind of control that is either off the window or doesn't properly indicate the focus. If you have no clue what control that might be then add a timer and a label. Set the timer's Enabled property to True, Interval to 200. Write the Tick event like this:
private void timer1_Tick(object sender, EventArgs e) {
if (this.ActiveControl == null) label1.Text = "No control?";
else label1.Text = this.ActiveControl.Name;
}
That tells you where the tab goes.
I would first try to fix the tab order on the window before resolving to using this code. Did you do that?

Suppress Automatic Textbox Selection on Windows Forms Form.Show()

I have an application where I am trying to mimic the "soft description" textboxes like those found for the tags and title locations on this site.
The way I've done this is essentially to create my textbox, and depending on what happens when the mouse pointer enters or leaves the control, it updates the content of the textbox to get the effect.
The problem is what when my form is first shown, the mouse cursor immediately jumps into the first textbox, which removes the title telling the user what the textbox is for.
If I turn off AcceptTab on the textbox, then everything works as expected, but the user loses the ability to tab into the textbox.
Is there a way to turn off this automatic selection of the textbox?
Could you this.Focus() on the form itself, or on some label control?
Bit late but a perfect solution is to select the form on load of form.
Adding this line to the constructor will give the expecting result.
this.Select();
But while using multi thread controls like OpenFileDialog if u want to unfocus/deselect text-box this.Select() was not working so I selected a button in the form using.
button1.Select();
The TabIndex property controls what order things will tab in, and on load, focus goes to the first control (ordered by TabIndex) that has AcceptTab as true. You can change the ordering so that the control that you want the user focus to start in is lowest (and have tabs work cycle through controls as you'd expect).
Alternatively, as Jason suggested, you could simply call Focus() on whatever control or the form itself in the FormLoad event.
I used a variant on Jason's technique. First, I created a dummy textbox with tabindex 0. That way, when the form is shown, that textbox will be selected. Next, I made the dummy textbox have zero width, so that it has no visible component.
However, once the form is loaded, I don't want the user to be able to tab over to the "nonexistant" textbox. Therefore, I added these two bits:
//These functions prevent the textboxes from being implicitly selected.
private void dummyBox_Leave(object sender, EventArgs e)
{
dummyBox.TabStop = false;
}
private void Main_Enter(object sender, EventArgs e)
{
dummyBox.TabStop = true;
dummyBox.Select();
}
Where Main is the name of my form.
Hope this helps someone.
Billy3

Categories