Programatically set Focus to a TextBox in Windows Phone - c#

In a Windows Phone app I have an TextBox and a Button. The user writes some text to the TextBox and taps the Button, the text from the TextBox is added to a list. The TextBox loses focus after the Button is tapped.
What I want to do is to set the focus back to the TextBox after the Button is tapped so the user can continue writing another text without needing to tap the TextBox.
I tried calling the Focus() method of the TextBox in the Button handler but this does not work. is there another, if any, way to do this?

When Button clicked try to add bollean flag = true. Then check this flag on event OnTextBoxLostFocus.
<TextBox x:Name="tb" Grid.Row="1" LostFocus="Tb_OnLostFocus"/>
<Button x:Name="btn" Click="Btn_OnClick" />
public partial class MainPage : PhoneApplicationPage
{
private bool flag;
public MainPage()
{
InitializeComponent();
}
private void Btn_OnClick(object sender, RoutedEventArgs e)
{
flag = true;
tb.Focus();
}
private void Tb_OnLostFocus(object sender, RoutedEventArgs e)
{
if (!flag) return;
tb.Focus();
flag = false;
}
}
Hope its help.

I have tried a lot of solutions, but this is the only one that works for me (Windows Phone 8.1 app).
First catch your TextBox's Loaded event, then call Focus(FocusState.Keyboard).
private void myTextBox_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Focus(FocusState.Keyboard);
}

Even I tried with lots of above solutions but none of them worked for me as am trying to focus on page load. Finally I got this solution and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}

What happens if you call:
yourTextBox.Select(0,0)
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.select.aspx

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.

The way that it worked best for me on the phone was, if I wanted to focus on a particular textbox when the page loaded:
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());
}
or if I just wanted it at a certain point. Just repeat these two lines:
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());

Related

How to select all text of a TextBox on mouse click? (TextBox.SelectAll() not working on TextBox.Enter)

I have a TextBox and I want all the text inside of it to be highlighted when the user clicks on it (so that they can replace it easily). I have the following event handler linked up the the TextBox:
private void TextBox_Enter(object sender, EventArgs e) {
SelectAll();
}
When I click on the TextBox, the text is only selected for a fraction of a second (sometime it's so fast I can't see it at all) and then it goes back to being a cursor. Does anyone know how to fix this or if there are any relatively simple workarounds?
I tried the same thing with the TextBox.MouseClick event (and it highlighted the text), but because it was the MouseClick event the text was highlighted every time I clicked the TextBox (even when the TextBox already had focus).
I have also tried SelectionStart = 0; SelectionLength = Text.Length, but the same thing happens. This leads me be believe the issue has something to do with the event.
I also tried the TextBox.GotFocus event and had the exact same problem.
I am doing this in a Windows Form application.
The reason why you didn't see the text getting selected is that the TextBox is busy when one of those events occurred (e.g., caret positioning). You actually select the text, but then the internal event handlers of the TextBox execute and remove the selection e.g. by setting the caret position.
All you have to do is to wait until the internal event handlers have completed.
You do this by using the Dispatcher. When you invoke the Dispatcher asynchronously the delegate is not immediately executed but enqueued and executed once all previously enqueued actions (like the internal event handlers) are cleared from the dispatcher queue.
So going with the TextBox.GotFocus event in WPF (or the TextBox.Enter in WinForms) and the asynchronous Dispatcher will do the trick:
WPF
private async void SelectAll_OnTextBoxGotFocus(object sender, RoutedEventArgs e)
{
await Application.Current.Dispatcher.InvokeAsync((sender as TextBox).SelectAll);
}
WinForms
private void SelectAll_OnTextBoxEnter(object sender, EventArgs e)
{
var textBox = sender as TextBox;
textBox.BeginInvoke(new Action(textBox.SelectAll));
}
Thankfully I found a solution! It turns out that the Click event is executed before the Enter event, this allowed me to set up a JustGotFocus variable and do the following:
private void myTextBox_Click(object sender, EventArgs e) {
this.JustGotFocus = true;
if (JustGotFocus) {
myTextBox.SelectAll();
}
}
private void myTextBox_Enter(object sender, EventArgs e) {
JustGotFocus = false;
}
If anyone else has this problem hopefully my solution is useful.

Button isn't disabling after I click it

I have created a button that resets a textbox value to the default value, as shown:
<Button x:Name="DefaultButton"
Grid.Row="0"
Grid.Column="3"
Click="OnDefaultClicked">
Here is the Click method:
private void OnDefaultClicked(object sender, RoutedEventArgs e)
{
DefaultButton.IsEnabled = false;
displayedData = defaultData;
//rest of method code
}
When I click the button, the data resets to its default value automatically, but the button does not get disabled until I click it a second time. I am not sure why this is happening.
I could implement the IsEnabled property in the xaml code and bind it to a method that determines whether the button should be enabled based on the value of displayedData, but since the button is not re-enabled/disabled anywhere else in my application or used for any other purpose, this seems kind of like overkill... as far as I know, the Click event should be able to handle this alone. Regardless, my main problem is that I just don't understand why the button wouldn't get disabled until the 2nd click since the OnDefaultClicked method explicitly states it should be disabled when clicked.
Am I missing something?
Turns out the problem was in the rest of my code. The snippet posted above worked perfectly fine. I realized I had created a method that re-enables the button when the text in the box changes, to allow the user to again reset it to default after making a change. So when the default button is clicked, the text in the box changes and therefore both the OnTextChanged and OnDefaultClicked methods are triggered, which causes simultaneous enabling and disabling of the button.
Here's how I fixed it:
private bool DefaultClicked;
private void OnDefaultClicked(object sender, RoutedEventArgs e)
{
DefaultButton.IsEnabled = false;
DefaultClicked = true;
displayedData = defaultData;
//rest of method code
}
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if(!DefaultClicked)
{
DefaultButton.IsEnabled = true;
}
DefaultClicked = false;
//rest of method code
}

C# Form - MouseClick will click the actual control after losing form focus

If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.

Windows phone 8.1 textbox multiple click

If a textbox has focus and I want to be able to select it again is there a way to do this.
So first click the background turns blue and while it is still selected I press again the background turns green. How do I catch the second press even though its already selected?
You can subscribe to the PointerEntered and the SelectionChanged events. The first one is always fired when the pointer hits the TextBox. However if it contains text and you tap on it you will eventually select the text. The SelectionChanged handler will take care for that.
Your XAML markup looks as follows:
<TextBox x:Name="tb"
Text="Test"
PointerEntered="TextBox_PointerEntered"
SelectionChanged="TextBox_SelectionChanged"
GotFocus="TextBox_GotFocus"/>
The code behind file contains the following code:
private void TextBox_PointerEntered(object sender, PointerRoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Green);
}
private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Green);
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
tb.Background = new SolidColorBrush(Colors.Blue);
}
You will have to adjust the code to your needs and take care of special cases where both SelectionChanged AND PointerEntered are fired (at this point both handlers do the same, so there's no problem).

Put focus back on previously focused control on a button click event C# winforms

I have made a custom Number Keypad control that I want to place in my winform application. All of the buttons have an OnClick event to send a value to the focused textbox in my form where I have placed my custom control. Like this:
private void btnNum1_Click(object sender, EventArgs e)
{
if (focusedCtrl != null && focusedCtrl is TextBox)
{
focusedCtrl.Focus();
SendKeys.Send("1");
}
}
focusedCtrl is supposed to be set on the MouseDown event of the button like this:
private void btnNum1_MouseDown(object sender, EventArgs e)
{
focusedCtrl = this.ActiveControl;
}
where this.ActiveControl represents the active control on the form.
My problem is that the button always receives the focus before the event detects what the focused control was previously. How can I detect which control had the focus before the button got the focus? Is there another event I should be using? Thanks in advance!
EDIT: Also, I would rather not use the GotFocus event on each textbox in the form to set focusedCtrl since that can be tedious and because I would like to have all the coding of my custom control be in the control itself and not on the form where it is placed. (I will do this, though, if there is no other practical way to do what I am asking)
Your requirement is fairly unwise, you'll want some kind of guarantee that your button isn't going to poke text into inappropriate places. You really do need to have the form co-operate, only it knows what places are appropriate.
But it is not impossible, you can sniff at input events before they are dispatched to the control with the focus. In other words, record which control has the focus before the focusing event is fired. That's possible in Winforms with the IMessageFilter interface.
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing your existing buttons.
using System;
using System.Windows.Forms;
class CalculatorButton : Button, IMessageFilter {
public string Digit { get; set; }
protected override void OnClick(EventArgs e) {
var box = lastFocused as TextBoxBase;
if (box != null) {
box.AppendText(this.Digit);
box.SelectionStart = box.Text.Length;
box.Focus();
}
base.OnClick(e);
}
protected override void OnHandleCreated(EventArgs e) {
if (!this.DesignMode) Application.AddMessageFilter(this);
base.OnHandleCreated(e);
}
protected override void OnHandleDestroyed(EventArgs e) {
Application.RemoveMessageFilter(this);
base.OnHandleDestroyed(e);
}
bool IMessageFilter.PreFilterMessage(ref Message m) {
var focused = this.FindForm().ActiveControl;
if (focused != null && focused.GetType() != this.GetType()) lastFocused = focused;
return false;
}
private Control lastFocused;
}
Control focusedCtrl;
//Enter event handler for all your TextBoxes
private void TextBoxesEnter(object sender, EventArgs e){
focusedCtrl = sender as TextBox;
}
//Click event handler for your btnNum1
private void btnNum1_Click(object sender, EventArgs e)
{
if (focusedCtrl != null){
focusedCtrl.Focus();
SendKeys.Send("1");
}
}
you have an event called lostFocus you can use
button1.LostFocus +=new EventHandler(dataGridView1_LostFocus);
and in the event:
Control lastFocused;
void dataGridView1_LostFocus(object sender, EventArgs e)
{
lastFocused = sender as Control;
}
in that way you can always know what is the Control that was focused previously
now, correct me if i'm wrong, but you do it for the SendKeys.Send("1"); to know which textBox need to receive the number. for that you can use GotFocus event and register only the textBoxs to it.
you can also do what windows is doing and use just one textbox like here:
if it's fits your needs
What about using this with the parameter forward = false?
Control.SelectNextControl Method
You'd probably call it on your "custom Number Keypad control".

Categories