How do you get a submit button in the Windows Phone keyboard? - c#

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

Keyboard accelerator firing when writing to Textbox

Click event of Button is fired when pressing D while writing to Textbox.
Is there an elegant way how to suppress Keyboard Accelerator while Textbox is focused?
XAML:
<StackPanel Orientation="Vertical">
<TextBox></TextBox>
<Button Click="Button_Click" Content="Button with "D" as keyboard accelerator" Margin="0,10">
<Button.KeyboardAccelerators>
<KeyboardAccelerator Key="D"></KeyboardAccelerator>
</Button.KeyboardAccelerators>
</Button>
<TextBlock x:Name="ButtonClickCounter"></TextBlock>
</StackPanel>
C#:
int buttonClickCounter;
private void Button_Click(object sender, RoutedEventArgs e)
{
ButtonClickCounter.Text = $"Button clicked {++buttonClickCounter} times";
}
EDIT:
Why Accelerator with Modifier (Alt+D or Ctrl+D) is not solution?
I am creating video player and I found that one-key shortcuts are neat solution for fast operations with video player (same as in VLC).
Best Solution so far:
Creating custom KeyboardAccelerator, that checks if focus is set to text box. Only edit in code that need to be done is changing KeyboardAccelerator to AcceleratorWithHandledActionIfTextboxIsFocused.
public class AcceleratorWithHandleDActionIfTextboxIsFocused:KeyboardAccelerator
{
public AcceleratorWithHandleActionIfTextboxIsFocused()
{
Invoked += AcceleratorWithHandleActionIfTextboxIsFocused_Invoked;
}
private void AcceleratorWithHandleActionIfTextboxIsFocused_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
var focusedElement = FocusManager.GetFocusedElement();
if (focusedElement.GetType() == typeof(TextBox))
args.Handled = true;
}
}
part of XAML:
<Button.KeyboardAccelerators>
<custom:AcceleratorWithHandleDActionIfTextboxIsFocused Key="D"></KeyboardAccelerator>
</Button.KeyboardAccelerators>
I agree with #Thomas Weller in his comments as for not using a single letter as a keyboard accelerator...
But everybody has their own requirements, anyway, you could try to e.handle = true your event when textbox is focused.
Something like this:
public void Button_Click(object sender, ExecutedRoutedEventArgs e)
{
if (textBox.isFocused)
{
e.handled = true;
}
}

How to call event key_down only in specific situations in c#?

I have 2 combo boxes and one text box(combo1, combo2, textBox). Here is the code for event key_down:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
buttonSearch_Click(sender, e);
}
When I click button ENTER on keyboard I want that program call search button on form. The problem is when I select some item from combo box and click on ENTER to give me that item, he also call searh button, ofcource, but I dont want to call search until I fill both combo boxes and text box. So, I want to call search button ONLY when my focus is on text box. Any idea how to do that?
as said, you could put the event on the textbox. Also, going with your original problem, you could check if the textbox has focus:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox1.Focused) // or whatever your textbox is called
{
buttonSearch_Click(sender, e);
}
}
}
You have specific events for each control. You are using the Form events but if you only want to have the keydown when the Textbox is focussed I suggest the following:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
buttonSearch_Click(sender,e);
}

Is there something similar in C# to fflush() from C?

My problem is:
The user can search an address. If there was nothing found, the user sees an messagebox. He can close it by pressing ENTER. So far, so good. Calling SearchAddresses() can also be started by hitting ENTER. And now the user is in an endless loop because every ENTER (to let the messagebox disappear) starts an new search.
Here the codebehind:
private void TxtBoxAddress_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
btnSearch_Click(sender, e);
}
private void queryTask_Failed(object sender, TaskFailedEventArgs e)
{
//throw new NotImplementedException();
MessageBox.Show("*", "*", MessageBoxButton.OK);
isMapNearZoomed = false;
}
And here the xaml code:
<TextBox Background="Transparent" Name="TxtBoxAddress" Width="200" Text="" KeyUp="TxtBoxAddress_KeyUp"></TextBox>
<Button Content="Suchen" Name="btnSearch" Click="btnSearch_Click" Width="100"></Button>
How can I handle this endless loop in C#?
Lol. Thats a funny infinate loop. Theres lots of answers.
Try adding a global string, _lastValueSearched.
private string _lastValueSearched;
private void TxtBoxAddress_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && _lastValueSearched != TxtBoxAddress.Text)
{
//TxtBoxAddress.LoseFocus();
btnSearch_Click(sender, e);
_lastValueSearched = TxtBoxAddress.Text;
}
}
private void queryTask_Failed(object sender, TaskFailedEventArgs e)
{
//throw new NotImplementedException();
MessageBox.Show("*", "*", MessageBoxButton.OK);
isMapNearZoomed = false;
}
So on the first enter insider the TxtBoxAddress, the lastSearchValue becomes the new search value. When they press enter on the messagebox, if the TxtBoxAddress text hasn't changed, the if statement will not trigger.
Alternativly, the line commented out, TxtBoxAddres.LoseFocus() may work by itself. This should take the focus off of the TextBox, so when the user presses enter on the messagebox, the TextBox KeyDown shouldn't fire.
Use KeyPress event instead of KeyUp:
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13) // handle 'Enter' key
MessageBox.Show("test");
}

Fire button event manually

I have a search text box on my WPF Windows. Whenever, the user presses enter key after writing some query in the textBox, the process should start.
Now, I also have this Search button, in the event of which I perform all this process.
So, for a texBox:
<TextBox x:Name="textBox1" Text="Query here" FontSize="20" Padding="5" Width="580" KeyDown="textBox1_KeyDown"></TextBox>
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
//how do I fire the button event from here?
}
}
It is possible but rather move your search logic into a method such as DoSearch and call it from both locations (text box and the search button).
Are you talking about manually invoking buttonSearch_onClick(this, null);?
You can Create a Common Method to do search for ex
public void MySearch()
{
//Logic
}
Then Call it for diffetnt places you want like...
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
MySearch();
}
}
private void buttonSearch_onClick(object sender, EventArgs e)
{
MySearch();
}
A event can not be fired/raised from outside the class in which it is defined.(Using reflection you can definitely do that, but not a good practice.)
Having said that, you can always call the button click event handler from the code as simple method call like below
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
OnSearchButtonClick(SearchButton, null);
}
}

Multiline textbox with skype like Functionalities

I was trying to have a textbox that creates new line when Shift+Enter is pressed and we can get keyevents when Enter is pressed
on backend of KeyDown Event i want to note that if Enter is pressed then do something.
if (e.Key.Equals(Key.RightShift))
{
}
this works fine for single line.as far as i click AcceptReturn = true and textwrapping to wrap then on pressing Enter new line is added to textbox but the event does not fire up.
i want new line to happen at Shift+Enter
and on Enter event should fire.
any idea?
I think you probably need to handle the PreviewKeyDown event. Catch the keypress combination you want to handle, handle it, then set e.Handled = true to make sure it doesn't get handled anywhere else as the keypress event tunnels & bubbles.
XAML:
<TextBox TextWrapping="Wrap" AcceptsReturn="True"
PreviewKeyDown="TextBox_PreviewKeyDown" />
Code-behind:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyboardDevice.Modifiers == ModifierKeys.None && e.Key == Key.Enter)
{
e.Handled = true;
// Do your special enter handling here...
}
// Shift+Enter (and any other keys) will be handled as normally...
// ...you'll still get your new line on Shift+Enter
}
Note: If you want an Enter keypress to still add a new line as well as your special handling then just remove the e.Handled = true line.
Try the following on the KeyDown event, should work on a vanilla multiline textbox, no need for anything further:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (!e.Shift && e.KeyValue == (int)Keys.Enter)
{
e.SuppressKeyPress = true;
// Fire my custom event
}
}

Categories