Is there something similar in C# to fflush() from C? - 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");
}

Related

Barcode Scan taking 1 character instead of a full set of String

I am trying to make an app that scan barcodes from a usb scanner. The app is supposed to look for jobs corresponding the code from the barcode. My current problem is that when I try the app, it only takes 1 character and tries to look for the job corresponding that said character instead of allowing me to type an entire string and press enter.
Here is my code:
public void SearchJob(object sender, EventArgs e)
{
this.ChangeCurrentEntity(this.Manager.GetEntity(j => j.Code == this.TypedView.BarcodeNumber)?.FirstOrDefault());
this.LoadView();
if (this.confirmation.GetConfirmation())
{
var tokenSource = new CancellationTokenSource();
this.task = Task.Run(() => { }, tokenSource.Token);
if (!task.IsCompleted)
tokenSource.Cancel();
if (dcConfig.CancelCode == "CancelCode")
{
this.Host.CloseHost();
}
}
this.TypedView.BarcodeNumber = "";
}
getConfirmation() is a popUp to confirm the job, which is currently not relevant to my problem I think.
This is from my form:
private void JobNumberTextBox_TextChanged(object sender, EventArgs e)
{
JobEntered?.Invoke(this, e);
}
Any help would be greatly appreciated.
You are waiting on the wrong event. You need to wait on the KeyPress event
private void JobNumberTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
JobEntered?.Invoke(this, e);
}
}
Side note: most barcode scanners can be configured to add an Enter key after a scan
I think I solved my issue.
I removed my text changed function and replaced it with a Keydown function so that when I press enter the event to search for a job triggers(that way i am allowed to type a full string in). I also binded my Keydown with my TextBox.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
JobEntered?.Invoke(this, e);
}
}

How do I make it so pressing Enter in a textbox does the same as clicking the button next to it?

I'm working on a C# based chat client in Windows Forms. I want to make it so a user won't always have to click the button when they want to send a message. Instead, hitting the Enter or Return key should be sufficient.
Here is a screnshot of my Form:
Handle KeyPress event for your textbox and code it like below in .cs page
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
MessageBox.Show("Your code here for enter key", "Enter Pressed");
}
}
You could create a Keydownevent for the textbox and ask for the enter key:
private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1.PerformClick();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
You can use the KeyPress event of the TextBox to know which key is pressed. When the Return key is pressed, programmatically click the Senden button.
Event Handler:
private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
// Enter key pressed
}
}
And register the event like this (or use the visual editor):
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnter);
Source
You can use your textbox keydown event to check whether you have pressed enter or not then call the submit button click event from there,
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonsSubmit_Click(this, new EventArgs());
}
}

auto press enter C# when textbox value changed as event

I develop an application and I want to run some code after press Enter the code successfully run when I press Enter. I want a way to press Enter automatically when the text box value changed.
But as event because I have a problem with threading so if I press Enter on the Keyboard it run well but when I write line code (sendkey.send("{Enter}")) it doesn't run like I press Enter on the keyboard
I want when press Enter to run a method "image ()"
private void tbResponse_TextChanged(object sender, EventArgs e)
{
SendKeys.Send("{ENTER}");
}
private void tbResponse_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
image();
}
}
Just call image() method directly without any buttons 'auto-pressing':
private void tbResponse_TextChanged(object sender, EventArgs e)
{
image();
}
private void tbResponse_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
image();
}
#Sergey Berezovskiy solution is good but if you still want to process it with keypress event then do like this
private void tbResponse_TextChanged(object sender, EventArgs e)
{
tbResponse_KeyPress(this, new KeyPressEventArgs((char)13));
}

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

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

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

Categories