I want the white arrow to appear in my text input boxes so users have a way of forward other than tapping away from the keyboard or using the hardware Back button.
The search fields do this in the system UI. How do I?
Here's my code
XAML:
<TextBox x:Name="InputBox" InputScope="Text" AcceptsReturn="True" TextChanged="InputBox_TextChanged"/>
CS:
void InputBox_TextChanged(object sender, KeyEventArgs e)
{
// e does not have Key property for capturing enter - ??
}
One quick note, I have tried AcceptsReturn as False also.
Also, I found that to get the white submit button that the search box has you can set the InputScope to "search":
<TextBox x:Name="InputBox" InputScope="Search" AcceptsReturn="False" KeyUp="InputBox_KeyUp"/>
I still haven't figured out if this has any unintended side-effects.
For good measure here is the code to dismiss the keyboard in the KeyUp event:
void InputBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.Focus();
}
}
Instead of handling the TextChanged method, handle the KeyUp method of the Textbox:
private void InputBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//enter has been pressed
}
}
Related
I’m using a PasswordBox control in wpf.
I want to block space in the control, but i can’t find the way.
I tried to use a KeyDown event but the event isn’t working.
What is the best way to block space in the PasswordBox?
For WPF you should using PreviewKeyDown Based on docs occurs before the KeyDown event When a key is pressed while focus is on this control.
XAML:
<PasswordBox x:name="txtPasscode" PreviewKeyDown="txtPasscode_PreviewKeyDown"/>
and in behind :
private void txtPasscode_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space && txtPasscode.IsFocused == true)
{
e.Handled = true;
}
}
Also in C# native try this :
private void txtPasscode_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ' ') e.Handled = true;
}
The KeyDown event you're handling actually fires after the character added to the passwordbox. You can block it by handling PreviewKeyDown this way(KeyDown won't fire anymore if user pressed space):
private void passwordbox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
If you need to block an event you're going to use the event beginning with "Preview"(read more here).
I have TextBox in WPF where i need to fill the box only by pasting (ctrl +v) not by typing. So i need to restrict entire key press except ctrl+v. Since WPF is not having keypress event i am facing the problem to restrict the keypress
Do it WPF style and use ApplicationCommands.Paste and make the textbox readonly.
you can add this Key_Down handler to the textBox:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.Key==Key.V)
{
//Logic here
}
else
e.handled=true;
}
Provided you don't allow Right Click + Paste, but only Ctrl + V, I would simply check for the Ctrl key modifier being pressed and prevent everything else.
So try this:
myTextBox.KeyDown += new KeyEventHandler(myTextBox_KeyDown);
private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
input = myTextBox.Text;
}
else
{
input = "";
}
}
<TextBox IsReadOnly="True" Name="Policy_text">
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Paste" CanExecute="PasteCommand_CanExecute" Executed="PasteCommand_Executed" />
</TextBox.CommandBindings>
</Textbox>
and in code behind
private void PasteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Clipboard.ContainsText();
}
private void PasteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Policy_text.Paste();
}
I want to make a wpf application in c# that displays some text on screen, and where the user is supposed to write a response and press enter to submit the response. I don't want to use a textbox, since there is only one line for the text input in the window, and I don't want the user to have to click to select the textbox. I want the application to be mouse-free.
My question is: How do I make it so that when the user has written their answer, they can submit the response simply by pressing enter?
I have tried the following snippet of code which I found on a microsoft help website:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
doSomething();
}
}
I suppose I have to add some code elsewhere, but I'm not sure where or what I need to add.
If you want to make sure your window process every Enter key press without care what control is focused you can use PreviewKeyDown event:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//Process user input
e.Handled = true;
}
}
Of course if you are doing mvvm you can create a behavior to encapsulate the event handler:
public class WindowBehavior : Behavior<Window>
{
protected override void OnAttached()
{
AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
}
private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Enter from Window");
e.Handled = true;
}
}
protected override void OnDetaching()
{
AssociatedObject.PreviewKeyDown -= AssociatedObject_PreviewKeyDown;
}
I suggest you to read this article about bubble, tunneling and direct events basic for WPF events.
If you have a button that you're using for submit, you can easily set it as the default by using the IsDefault=true (wrote a tip about doing this and the cancel for the escape here.)
Other than that, you'll have to have somewhere to write it (yet you don't want a textbox? you can select it by default, or tab into it if you don't have the focus there), and you can handle the keydown to "catch" the Enter otherwise.
I use the toolkit for Windows Phone 8 and have a page with a AutoCompleteBox.
Now I want to close the keyboard if user writes something into the AutoCompleteBox without selecting a present item (meens user typed in a different text) and commits the text with "Return".
How can this be done?
You could do something like this,
<AutoCompleteBox Name="Input" KeyUp="Input_KeyUp" FontSize="40">
<AutoCompleteBox .InputScope>
<InputScope>
<InputScopeName />
</InputScope>
</AutoCompleteBox .InputScope>
</AutoCompleteBox >
And on event,
private void Input_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.Focus();
}
}
Read whole article here : Dismiss the SIP (keyboard) in WP8
You can close the keyboard using a KeyUp event handler and then selecting Focus() of the current page:
void textBox_KeyUp(object sender, KeyEventArgs e)
{
// if the enter key is pressed
if (e.Key == Key.Enter)
{
// focus the page in order to remove focus from the text box
// and hide the soft keyboard
this.Focus();
}
}
Strange behavior! when I click on the button on the metro app all works well, but when i click enter(the button on the KB) the only thing that happens is everything is cleared!
This fails
private void TextBox_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
textBlock.Text = textBox1.Text;
// textBox1.Text = "";
}
}
This works as expected
private void Send_Click(object sender, RoutedEventArgs e)
{
textBlock.Text = textBox1.Text;
textBox1.Text = "";
}
What am I doing wrong ?
thanks
It would be best if when pressing Enter on the textbox, it will actualy simulate a click on the button, to assure that both actions are really the same.
private void TextBox_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
this.Send.PerformClick();
}
}
private void Send_Click(object sender, RoutedEventArgs e)
{
textBlock.Text = textBox1.Text;
textBox1.Text = "";
}
Also, as Chris mentioned, you shouldn't really handle the KeyDown even in this case: you can set the AcceptButton property of the form to be the Send button, which means that when pressing Enter, the button will be pressed - even if not focused. This sort of problem is a good example of why using the AcceptButton property.
I think you are trying to handle focus when the user hits Enter. This is a common requirement in applications. As for your strange behavior, I can't explain it. But I am also not sure explaining it is as important as solving the problem. So, I can show you the easy implementation of handling enter and focus:
<StackPanel>
<TextBlock FontSize="20" Foreground="White">One</TextBlock>
<TextBox x:Name="T1" Width="1000" Height="100" KeyDown="T1_KeyDown_1" />
<TextBlock FontSize="20" Foreground="White">Two</TextBlock>
<TextBox x:Name="T2" Width="1000" Height="100" KeyDown="T2_KeyDown_1" />
</StackPanel>
and
private void T1_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
T2.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
private void T2_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
T1.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
The result is perfect focus control.