Close keyboard on tapping on Enter - c#

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();
}
}

Related

How do I create keyboard input in a wpf application?

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.

Set label to textbox content

Here's what i want to do:
I want to be able to take the contents entered into a textbox. Once the user hits the ENTER key or the enter button I made, to have that content appear in a label I've placed on my form.
And have that same label change to whatever you enter in the textbox everytime you hit enter.
I hope I explained this good enough. Thanks!
Subscribe to KeyDown event of TextBox and check if Enter is pressed
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
label1.Text = textBox1.Text;
}
add a button to your form, double click it, inside the event add the code below
label1.text=textbox1.text
and do some basic tutorials, dude
Assuming you are using winforms... First you need to enssure that your form will response to keyboard events.
So you need to set the property KeyPreview to true.
Then use the event KeyDown and write this code:
private void form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
myLabelName1.Text = myTextBoxName1.Text;
}

Override the forward-key on WP7-Keyboard [duplicate]

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
}
}

How can I override the default button on a form when focus is in a TextBox

I have a winform form which has typical OK and Cancel buttons.
The OK button is set as the default button. When the user presses the ENTER key, the form is closed.
The form also has a text box which has a Search button beside it. The text box allows the user to enter search text and the Search button initiates the search function.
I would like the user to be able to press the ENTER key when the text box has the input focus and have the search function activate.
The problem is that the form is grabbing the ENTER key event before the text box's handler gets it.
EDIT: I've tried using the following event handlers, but they never get hit, and the form closes when the Enter key is hit:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
private void txtFilter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
I've also tried enabling the KeyPreview property on the form and implementing a KeyDown event handler for the form, but the Enter key never seems to cause any of these to be hit.
What is the best way to accomplish this?
Try handling the Enter and Leave events of the TextBox to clear out your form's AcceptButton property:
private void txtFilter_Enter(object sender, EventArgs e) {
this.AcceptButton = null;
}
private void txtFilter_Leave(object sender, EventArgs e) {
this.AcceptButton = closeButton;
}
Then you can just process your KeyUp event as your want:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
}
}
Add a KeyDown event handler to the textBox and then add this to it
if (e.KeyCode == Keys.Enter)
{
btnSearch.PerformClick();
}

c# Listbox control (arrows and enter keys)

I have a listbox which displays the contents of an array. The array is populated with a list of results when my "go" button is pressed.
The go button is set as the AcceptButton on the form properties so pressing the Enter key anywhere in the focus of the form re-runs the go button process.
Double clicking on a result from the array within the listbox works fine using below:
void ListBox1_DoubleClick(object sender, EventArgs e) {}
I would like to be able to use my arrow keys and enter keys to select and run an event without having to double click on the line within the listbox. (however go button runs each time instead)
Basically open the form, type search string, press enter to run go button, use up and down arrows then press enter on selection to run same event as double click above. Will need to change focus after each bit.
You can handle the KeyDown events for the controls you want to override. For example,
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//execute go button method
GoButtonMethod();
//or if it's an event handler (should be a method)
GoButton_Click(null,null);
}
}
That will perform the search. You can then focus your listbox
myListBox.Focus();
//you might need to select one value to allow arrow keys
myListBox.SelectedIndex = 0;
You can handle the Enter button in the ListBox the same way as the TextBox above and call the DoubleClick event.
This problem is similar to -
Pressing Enter Key will Add the Selected Item From ListBox to RichTextBox
Certain controls do not recognize some keys when they are pressed in Control::KeyDown event. For e.g. list box does not recognize if the key pressed is Enter key.
See the remarks section of the Control::KeyDown event reference.
One way to resolve your problem might be writing a method for the Control::PreviewKeyDown event for your list box control:
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up && this.listBox1.SelectedIndex - 1 > -1)
{
//listBox1.SelectedIndex--;
}
if (e.KeyCode == Keys.Down && this.listBox1.SelectedIndex + 1 < this.listBox1.Items.Count)
{
//listBox1.SelectedIndex++;
}
if (e.KeyCode == Keys.Enter)
{
//Do your task here :)
}
}
private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
e.IsInputKey = true;
break;
}
}

Categories

Resources