Textbox issue in windows phone 7? - c#

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!

Related

Keyboard won't dismiss even after Focus change

I am creating a Windows 8.1 app and after the user presses a button, a popup opens over most of the screen. There are several textboxes inside the popover.
I found this sample code from microsoft about how to detect the appearance of the on-screen keyboard.
I have also found the following SO posts and sites basically informing that there is no way to force the keyboard to close, and the correct thing to do is in fact programmatically focus a hidden element on the page or disable and then re-enable the textbox:
Forcing Windows 8 soft keyboard to hide
Windows 8 soft keyboard not hidden
Show/Hide Keyboard Automatically Widnows 8
How to Dismiss Touch Keyboard
So I followed the advice and created an invisible button. When the user taps the close button, it is supposed to give focus to that button and dismiss the keyboard. What happens is the textbox does lose focus, but the keyboard does not go away. If I cause the close button to give focus to the hidden button and close the popup (which is the desired effect), the keyboard does not go away until the view (that was previously under the popup) is tapped.
How can I make closing the popup cause the keyboard to dismiss?
EDIT: It appears that there might be a way to programmatically dismiss the keyboard because triggering the App Bar to open while the keyboardi s open automatically dismisses the keyboard.
When the textbox that shows the virtual keyboard was disabled it will dismiss the virtual keyboard. so the solution is set the textbox property IsEnabled to false and set it again to true so it can be use again.
TextBox.KeyDown += (s, a) => {
if (a.Key == VirtualKey.Enter) {
TextBox.IsEnabled = false;
TextBox.IsEnabled = true;
}
It was impossible to programmatically manage the touch-keyboard's appearance and disappearance. Unfortunately, changing the IsEnabled property didn't work for me.
The touch-keyboard appearance principle was known as Focus-driven, but I had walked out by setting the property IsTabStop=True on the UserControl explicitly. Besides, the TextBox won't activate the touch-keyboard if its IsTabStop=false.
In theory, I think the system searches the next potential TextBox,
so that if so it wasn't to close and re-open, with
touchable+inputable property. Maybe there were kind of bug that while releasing the Focus, current TextBox releases only his
"touchable" focus, and didn't finish to release the keyboard's
"inputable" focus, because that by default only the input-controls
have Tab-Stoppable property.
By the way, if we close the UserControl by a CustomControl's Close button, the IsTabStop=true will be needed on his parent.
PS: Solution tested only on Windows 8.1 Store Application.
Please check my answer to my own question:
https://stackoverflow.com/a/24252140/126537
Based on #Paul 's answer. Not very elegant, but works as charm.
In my app this works fine:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.KeyDown += Strona_KeyDown;
}
private void Strona_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
this.Focus(FocusState.Pointer);
}
}
In UWP Windows 10 simple disable/enable doesn't work anymore. But this works:
TextBox.IsEnabled = false;
var t = new DispatcherTimer();
t.Interval = new TimeSpan(0, 0, 1);
t.Tick += (a, b) =>
{
t.Stop();
TextBox.IsEnabled = true;
};
t.Start();
Find more elegant solution? Please share.

WPF TextBox stealing focus after neighboring CheckBox is clicked

I've had an annoying issue with focus for controls on a ToolBar in WPF. My toolbar has a CheckBox and a TextBox control next to eachother. If you click and edit the text in the text box, then click the check box, the text box steals the focus back after clicking the check box, and the check box state is not changed when the user clicks on it.
I have similar issues with text boxes all over my application, but I believe this is the simplest case to explain, and I hope that the problem will be a common issue across all my text boxes.
Does anyone know what might be going on here?
Hook up a handler to the checkbox's Click event (or even the PreviewMouseUp event), and set the event's Handled property to 'true'.
Did you try that?:
private void MyCheckBox_Click(object sender, RoutedEventArgs e)
{
MyCheckBox.Focus();
}

want to stop user input by messagebox but bypassed by a Enter keydown

I'm coding a windows form application running on a barcode scanner.
The plantform is .Net2.0CF/C#.
What i want is whenever user input wrong, the app will pop a messagebox and block the next input(actually,a scan action) until user click the OK on the screen.
But normally the user will continuously scan the next stuff as they didn't find anything went wrong, this will insert a Enter keydown so the messagebox will be closed, in one word, the messagebox does not stop the user.
How can i code this? Below is a very simple code snippet
private void tb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "Return")
{
if(!ValidateInput(tb.Text))
MessageBox.Show("Error");
}
}
You can create your own window (Form) that displays the error message, but does not react on the enter key.
It should contain a button which the user can click (as you wrote), however you need to make sure the button does not have focus when the window is displayed. (Because if it had focus, pressing the return key will "click" the button.)
A simple way for doing this is adding another control which has TabStop set to true (e.g. a textbox, another button) and which has a lower TabIndex property than the button.
Additionally, maybe you might want to do a
System.Media.SystemSounds.Beep.Play();
when showing the window to draw the user's attention to the window.

Windows Phone TextBox Control Foreground and Background Brushes not updating properly

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.

Inserting a TAB space into a TextBox

I have seen few tutorials that claim to solve this issue online but they do not work. I would like to insert a TAB space when the TAB key is pressed, into my multiline TextBox.
A dudes response from Microsoft was that, by design, Metro apps will bring focus to the next control if you press TAB inside a TextBox. Now, this would make sense, if you were pressing TAB on a Single-line TextBox. But in a multiline TextBox? Don't you think it's more likely that the user will want to insert a TAB?
And yes, I know, you can insert a TAB space in a Metro TextBox by pressing Ctrl+TAB. But that is error prone, since most of us are used to just pressing TAB, and old habbits die hard sometimes.
Here is my issue. I have a text editor feature of my app where the user may need to enter large amounts of data. And you know what people are like, they like to separate things to make their text documents more readable and it's very uncomfortable and more tedious to use Ctrl+TAB. So I would like to know if anybody can help with a workaround for this (it can't involve a RichTextBox, though)?
Also, if I manage to find a workaround, will this increase the chances of my app release being rejected by the Store?
Subscribe to the KeyPress event of your TextBox, capture the Tab key by inspecting the KeyCode of the pressed key, and then set the Handled property of the KeyEventArgs to true so the key isn't passed onto any other controls.
Use SendKeys to send a "Tab" character to the TextBox to mimic the behavior of pressing "Ctrl+Tab", like you said:
TextBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
e.Handled = true;
SendKeys(^{TAB});
}
}
The carrot (^) represents the CTRL key.
richTextBox1.AcceptsTab = true;
in your KeyPress event of your textbox control. Make sure that you have the property set true for multiline on the textbox control
This would work if you are using a RichText Control which is what I would suggest
if (e.Key == Windows.System.VirtualKey.Tab)
{
e.Handled = true;
string SelectionText = "";
TextBox.Document.Selection.GetText(Windows.UI.Text.TextGetOptions.None, SelectionText);
TextBox.Document.Selection.TypeText(char(9) + SelectionText);
}

Categories

Resources