Set focus to TextBox - c#

In my WindowsPhone application I have a Text Box that needs to receive focus at a given time. What I've tried so far:
textBox1.Focus();
textBox1.UpdateLayout();
textBox1.Focus();
textBox1.IsTabStop = true;
textBox1.UpdateLayout();
textBox1.Focus();
textBox1.IsTabStop = true;
textBox1.Focus();
Nothing seems to work. In the Emulator, when the Focus() method is called, the keyboard starts to rise, but then crashes back. The TextBox has IsTabStop set to true in the properties.

Does this help?
this.ActiveControl = textBox1;

If you want the text box to be selected without you having to click into it (if that is what you mean by focus), try:
textBox1.Select();

This seems to be a Silverlight bug. I've used the TextChanged event on the TextBox, and set the Focus() in there. Kind of an ugly workaround, but it worked for me. If anybody has another solution, please post.

Even I have tried all the above solutions but didn't work for me. Finally I tried with the following thing, and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}

Just set focus property of any control at Form_Activated time..
private void Form_Activated(Object sender, EventArgs e){
Textbox.Focus();
}

you can accomplish this by programmatically giving it focus. This can be done by
calling its Focusmethod, although this call can fail (and return false) under certain conditions.
For example, you cannot set focus on a control from a page’s constructor; it’s too early. You can,
however, call it from a page’s Loadedevent.

if this is a textbox created in the runtime like this:
TextBox TextBox1 = new TextBox();
TextBox1.Name = "TextBox1";
then add this line
TextBox1.Loaded += new RoutedEventHandler(TextBox1_Loaded);
and then add this following event also:
private void TextBox1_Loaded(object sender, RoutedEventArgs e)
{
((TextBox)sender).Focus();
}

This may be a little late a couple years late but after playing around in windows phone 8.1 development environment I found the solution:
TextBox.Focus(FocusState.Keyboard);
You need to pass in the type of FocusState through the FocusState class.

It's worked for me
this.YourTextbox.TabIndex = 0;

After spend hours in debugging, I found the solution
await Task.Delay(100);
tbInput.Focus(FocusState.Programmatic);

if we have tow textboxes
exp:
txtPrincipal
txtPrincipal_Contact
if want move from txtPrincipal to txtPrincipal_Contact
we need to write second textbox name with focus function in leave event of the current textbox exp:
private void txtPrincipal_Leave(object sender, EventArgs e)
{
txtPrincipal_Contact.Focus();
}

Related

How to set the focus on a form

I want to set the focus on a popup form I have created when the mouse hover it,
I checked MSDN and they said to use the SetFocuse method but it does not work.
I tried this:
private void POPUPmainmanue_MouseHover(object sender, EventArgs e)
{
POPUPmainmanue.SetFocus();
}
It's strange but I could find no SetFocus() method for a Form on MSDN.
However, using the Activate() methods works well enough.
private void POPUPmainmanue_MouseHover(object sender, EventArgs e)
{
POPUPmainmanue.Activate();
}
Also, always make sure you do not forget to set the EventHandler:
POPUPmainmanue.MouseHover += POPUPmainmanue_MouseHover;
You also did not mention whether you are using WinForms or WPF, although I think it's the former as you say 'form'. Again, what exactly is POPUPmainmanue? A form? I don't think so. A form does not have a SetFocus() method.
I don't think it's easy to implement, cause it's not very clear the question:
It's possible to set focus on textbox when the mouse hovers the form, and another more clear possiblity to set focus on a form when a form opens.
this.BringToFront();
this.Activate();

Fire event on textbox lose focus

I'm trying to call a method as soon as a TextBox on my screen gets 'un-focused' if that makes any sense? The user types in a username and as soon as that textbox loses focus I want to fire an event that checks if that username is indeed available.
Thanks!
There is a Control.Leave in C#, which I think is perfect for your purpose.
you can go to events of the textbox in visual studio, and find the Leave event.
The code generated will be like :
private void txtbox_Leave(object sender, EventArgs e)
{
//Check for available operation Code
}
Please explore the LostFocus event in a textbox. Hope it helps
select your object and to choose the panel of the properties and click on event.To unroll until LEAVE and double-click above.that will give you:
private void object_Leave( object sender, EventArgs e)
{
////put your code here
}
In WPF Try this:
TextBox.LostFocus
This will work:
Go to the "properties" of your textbox. You will see a yellow lightning bolt in the first line tab. There you will find all possible events that can be triggered. Search for "Leave" entry, double-click it. There you can put whatever you want.

Disabling a Textbox Using TextChanged Event

The form I am using requires a copy pasted URL. I am trying to have a textChanged event that will check the url as soon as it is pasted, telling the user whether it is valid or invalid. I also want to be able to lock out the textbox when this happens, with a message saying something like "Processing...".
The problem is with the code below, the textbox is never disabled, the program will do the checkUrl() method and the textbox is never disabled even though it is first to execute (I assume it is but the fact there is a function call right underneath it is messing around with something or getting higher priority).
How do I go about making the control visually disabled while the method runs?
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
checkUrl();
urlTxtBx.Enabled = true;
}
I think this is happening because the Application needs to complete all the active threads before disabling the TextBox. Please try the following code:
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
Application.DoEvents();
checkUrl();
urlTxtBx.Enabled = true;
}
This will let the UI to be updated. For more details check here.

How to clear the value of an Autocomplete Box?

Just like many things in WPF, sometimes the easiest things are the ones that are hardest to find examples for! How do you clear out the current text of an AutoCompleteBox? In my OnFocus event I want to make sure that the user is given a clear box for entry. So my event procedure looks like
private void autGlobal_GotFocus(object sender, RoutedEventArgs e)
{
AutoCompleteBox acb = (AutoCompleteBox)sender;
if (acb.SearchText == "Search Term")
{
//clear out the box if it has the focus
this.autGlobal.Text = "";
}
}
However, setting the text property directly does not seem to work. Am I missing something obvious?
You need reset Selected Item too.
private void SearchAutoCompleteBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
SearchAutoCompleteBox.SelectedItem = null;
SearchAutoCompleteBox.Text = string.Empty;
}
Have you tried setting the property on your local variable?
acb.Text = string.Empty;
I have a feeling there may be additional code affecting the .Text field when the focus or textchange events are firing.
I think I found the answer after spending a ton of time on this and the XAML. This code example will not work when the IsTextCompletionEnabled option is set to true in the XAML. I set it to false and this code works fine.

How can I give focus to a textBox after a Tab has been selected?

I'd like give focus to a textBox after a Tab has been selected but no matter what I try it doesn't work. I've looked at similar questions here but they don't get me the results I need. Here is what Ive tried.
private void tabBDERip_Click(object sender, EventArgs e)
{
textBoxPassword.Focus();
}
and
private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabAll.SelectedTab == tabBDERip)
{
textBoxPassword.Focus();
}
}
Can someone please tell me what I'm doing wrong?
Thanks
First thing the Click event of the TabPage control fires when the user clicks inside the TabPage not on the header so your SelectedIndexChanged event is the one you want to use.
I just tested code very similiar to yours:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabPage2)
{
textBox4.Focus();
}
}
And it worked fine.
Is the password textbox not enabled or something like that?
If you try to call Focus() on a different control does that also not work?
If you set a breakpoint inside the SelectedIndexChanged code does it get hit?
Update: Interesting. If the breakpoint isn't getting hit (before the if) I would double check that your eventhandler is properly attached. Look in your designer.cs for something like:
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
Update: I put my working example at http://www.ccswe.com/temp/SO_TextBoxFocus.zip maybe looking at it will help you figure out where the issue is.
Update: The easier way to attach an event handler to a control on your form:
1: Select the Control to want to attach an event handler to and then click the Events icon (lightning bolt) in the Properties window.
alt text http://www.ccswe.com/temp/Attach_EventHandler_1.png
2: Find the event you want to attach to and double click to the right.
alt text http://www.ccswe.com/temp/Attach_EventHandler_2.png
3: A code stub will be automatically generated for you and the event will be attached in the designer.
alt text http://www.ccswe.com/temp/Attach_EventHandler_3.png
If you look at the properties window again you'll now see the name of the method that was generated.

Categories

Resources