I have a small window with 2 textboxes in a grid databinded to some properties, it is called from context menu of another window. I made one of textboxes focused after appearing by
<Grid FocusManager.FocusedElement="{Binding ElementName=priceBox}">
I would like to have behavior that Text in TextBox would be selected (dark blue background) so if I start type new symbols old ones being immediately deleted. I don't want to delete old symbols first. Same behavior I would like to have after I press Tab to switch to next textbox.
Is there any textbox settings to achieve this?
I have very old winforms applications and It looks like it was behaving this way by default.
You will have to set Keyboard focus on the TextBox before selecting the text
e.g:
private void SelectAllText(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
Keyboard.Focus(textBox);
textBox.SelectAll();
}
}
Related
i am building a virtual keyboard to suit the needs of the touch screen machine i'm going to be deploying on. i am using a popup window for the keyboard and have been able to wire all number buttons as follow, here's my virtual keyboard class
public partial class NumKeypad : Window
{
withoutB withoutbvn;
enterBvn ebvnn;
public NumKeypad()
{
InitializeComponent();
}
public NumKeypad(withoutB wobvn)
{
InitializeComponent();
withoutbvn = wobvn;
}
private void one_Click(object sender, RoutedEventArgs e)
{
var focusedElt = FocusManager.GetFocusedElement(withoutbvn);
var tbox = focusedElt as TextBox;
try
{
withoutbvn.ph.Text += (((sender as Button).Content as Border).Child as TextBlock).Text;//this works, but this is assigning directly to only one control. i want to assign to whatever control that has focus
}
catch (Exception ex)
{
}
}
}
on the first line of the one_click function(which handles all input button click) i'm trying to get a reference to the element currently focused in the page whose instance is "withoutbvn".
on the second line, i am tryin to convert the element to a text box so i can write to its text property. but that keeps returning null. meaning when this pop up windows come up(the keyboard pop up window comes up when a textbox or any other input element receives focus), i cannot get a reference to the focused textbox so i cannot write to it. Please how do i ensure a focused textbox remains focused so that i can assign its text property from a pop up window? Or if there's a better way to do this, pls point me in the right direction. Thanks
I've used this keyboard for WPF :
keyboard control wpf
It is a popup control which can be customized as you wish. You have the entire code and it's free. In my case I had to adjust the popup (layout and to add the German letters) and was pretty straightforward.
I also had to show a numeric keyboard, and I've used the same keyboard but with a simpler layout. Behind the scenes, all it is very simple: you have to define a key in a grid, place it where you want and make sure you generate on click the corresponding virtual key code.
i used the popup control to create the keyboard, used buttons to create all keys and wired a single event handler to all input buttons, then different event handlers for the backspace and enter buttons. once any letter, number or symbol button is clicked, the following function gets called.
try
{
IInputElement focusedControl = Keyboard.FocusedElement;
var foc = focusedControl as TextBox;
foc.Text += (((sender as Button).Content as Border).Child as TextBlock).Text;
}
catch (Exception)
{
}
that inserts the button's text to the control in focus. This is pretty basic. i'll appreciate more suggestion on how i can expand on this. Thanks
When I try to add a text in the TextBox from a canvas using handwriting, the cursor go to the TextBox and the keyboard shows, and I try to add some code like make the TextBox isReadonly or trying to hide the keyboard and doesn't work.
I want every time select an item from the ListBox the item add to the TextBox without showing the keyboard. the action on RecognizedListBox_SelectionChanged a ListBox
private void RecognizedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (RecognizedListBox.SelectedItem == null)
return;
//gte the selected item from listbox
string inputTextWritePad = RecognizedListBox.SelectedItem.ToString();
//add the item to RichEditBox
MyTextNote.Text += inputTextWritePad + " ";
//clear the canvas return the listbox to vide
ClearAllClick(sender, e);
}
If I add isReadonly for TextBox, it will permanent disable to edit it, and I can't add any text using keyboard. I don't know where I will put my code, or verify when I need the keyboard to use it.
I see if I need to hide the keyboard, I must have an event for the keyboard button or something like this
private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
if(e.Key==Windows.System.VirtualKey.Enter)
{
Windows.UI.ViewManagement.InputPane.GetForCurrentView().TryHide();
}
}
but nothing to figure out!!
update 1:
I add this code
private void MyTextNote_GotFocus(object sender, RoutedEventArgs e)
{
InputPane.GetForCurrentView().TryHide();
}
and help me to not show the keyboard, but I need to show it when I clicked the textbox, I try whit tapped but nothing help.
Here's a property to avoid displaying Keyboard if your TextBox receives focus programmatically :
<TextBox PreventKeyboardDisplayOnProgrammaticFocus="true"/>
Set this property to true to prevent the onscreen touch keyboard from
showing when focus is programmatically set on a text box. By default,
the onscreen touch keyboard is displayed whenever focus moves to an
editable text box and the most recent input was generated by touch.
Official Doc
I'm working on a WPF Application using MVVM.
I have a view with tabs, and a listbox (which is not part of the tabControl) which displays all the validation errors on the view (using INotifyDataErrorInfo interface).
What I want to achieve: Once I click on an item within the listbox, the focus goes to the tab with the corresponding control and set focus on that control.
For example, I have a validation error for First Name, so when I click on the item within the listbox describing the validation error for the first name, I want to switch to the tab where the First Name textbox is located and set focus for that textbox. Now, I followed what is written here:
Set focus on textbox in WPF from view model (C#)
especially Zamotic reply ( he had the same situation with tabs as I have).
Now, the described situation above works well for me with the textboxes: it indeed goes to the First Name textbox and sets the focus for it.
The problem is that I have a WPF custom control in one of my tabs, and when I click on a validation error within the listbox regarding this custom control, it indeed goes to the correct tab, but no focus is set to the custom control.
The custom control consists of a textBox, and I want the focus to be set to this textbox.
So, how do I handle Focus in a custom control so it fits the describes requirement above?
Here is the code for my custom control (the part of the code where I try to handle the focus):
public class NumericUpDownControl : Control
{
static NumericUpDownControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDownControl), new FrameworkPropertyMetadata(typeof(NumericUpDownControl)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
AttachToVisualTree();
this.GotFocus += new RoutedEventHandler(GetFocusHandler);
this.LostFocus += new RoutedEventHandler(LostFocusHandler);
}
private void GetFocusHandler(object sender, RoutedEventArgs e)
{
TextBox.Focus();
this.Focusable = false;
}
private void LostFocusHandler(object sender, RoutedEventArgs e)
{
IncreaseButton.Focus();
}
Textbox is the one set in the custom control Themes folder in Generic.xaml
var textBox = GetTemplateChild("valueBox") as TextBox;
if (textBox != null)
{
TextBox = textBox;
So, how do I handle the focus to achieve what I have already acheived for textboxes?
Edit 1: By the way, IncreaseButton is another control (which is a button) that my custom control consists of. When my custom control goes out of focus, I want to draw the focus off the textbox, so I just set it for that mentioned button.
Edit 2: In my view, I have a tab with the mentioned custom control. In my view model, I have a property called
public bool IsAgeFocused
And in the View I've written the following:
<numeric:NumericUpDownControl
x:Name="numericUpDownBox"
uiExtension:FocusExtension.IsFocused="{Binding IsAgeFocused}"
Focus Extension is the same class (without any additional info) like in the link:
Set focus on textbox in WPF from view model (C#)
The version described in Zamotic post (with the visibility part).
In my app,I just have a page with four text boxes, so when i click a text box soft keyboard appears, now when i want to move to next textbox then i have to tap outside the textbox to make the keyboard disappear and then click on another text box. I don't think it is user friendly, so i have two options,
1)To change the functionality of return button(to make it work as tab).
2)To reduce the frame size and so scrolling will be enabled.
How can I do the foretold two options in windows phone 7??
for the first option
Make return key of the input panel work like tab key.
make key down event of 1st textbox like this
private void txt1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
txt2.Focus();
}
}
similarly make this event for txt2 and txt3 and give focus accordingly and on on txt4 keydown event focus the main grid.
and about the 2nd way. Its a big problem in wp according to my knowledge.
For moving to next textbox #Amu 's answer will work perfect, and to dismiss the keyboard,
if (e.Key == System.Windows.Input.Key.Enter)
{
this.Focus();
}
That will take the focus away from your text box and will bring it to your screen.
And So keyboard will disappear!
TLDR: my textboxes will update their Foreground and Background colors properly when manually selected by touch input, but will fail to update if I use the Textbox.Focus() method after they are created.
The question is similar to this - windows phone - textbox background
My Application creates a textbox with an associated GotFocus event, that changes the Foreground and Background colors of the textbox to the system default whenever the textbox receives focus. Upon the user pressing the enter key on the keypad, the app then generates another identical textbox below the first.
The issue is that for any of these textboxes, if the textbox is pressed manually, everything works fine, and the textbox is displayed how it's meant to be. However, if I use TextBox.Focus() after the enter key is pressed, although focus is passed to the textbox, and the GotFocus event has been processed, the Background and Foreground colors are not updated, leaving white text on a white background.
I have tried passing focus between the textbox for a number of times (up to 10), and even though I can confirm that the focus is being passed as it should, the colors are only updated if the user gives focus to the textbox (if I give focus to the textbox via code, I must then manually deselect and then reselect it for the color change to apply. If I don't give focus via code I can simply select it).
The code for this is:
public void txtInputKeyUp(object sender, KeyEventArgs e)
{
TextBox txtBox = (TextBox)sender;
if (e.Key == Key.Enter)
{
Evaluate(txtBox);
InitializeDivider();
InitializeTextInput();
InitializeTextOutput();
txtInput[txtInput.Length - 1].Focus();
}
}
public void txtInputGotFocus(object sender, RoutedEventArgs e)
{
TextBox txtBox = (TextBox)sender;
if(txtBox.Text == "Input Here")
{
txtBox.Text = "";
}
txtBox.Foreground = (SolidColorBrush)App.Current.Resources["PhoneForegroundBrush"];
txtBox.Background = (SolidColorBrush)App.Current.Resources["PhoneBackgroundBrush"];
}
The Initializeblabla stuff basically just creates the relevant textboxes and all associated data. I've tried switching focus between textboxes, disabling and enabling the specified textboxes and a few other options, and consistently the only thing that works is not giving focus to the textbox via code, but rather waiting for the user to select the textbox, something I'm not really satisfied with. Attempting to manually edit the style didn't help either.
My eventual solution was to add a timer to the app with a very short interval (short enough to make the change not visible) and have that change the color of the textbox after the textbox had received focus.