How can I press Enter while writing in a Text Box? - winforms

In my C# project I have a text box and a button. What I want is:
1) the user writes something in the text box
2) clicks the "Enter" button
3) this automatically triggers the button1_Click event
Step 1 is straightforward.
For step 2 the problem is that after I write sth in the text box, it is still selected and I am forced to write "Enter" (new line) in the text box which doesn't happen and I hear an error sound.
For step 3 I used
public Form1()
{
InitializeComponent();
KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter)
{
button1.PerformClick();
}
}
But the code doesn't detect that the Enter key is presses and the code isn't ran at all. Any ideas how to fix this? I suppose I have to unselect the text box and then press Enter but I don't know how it works.

Related

Chinese password in masked text box in Windows Forms?

When a Windows Forms TextBox is in password mode, it is restricted and the ImeMode is disabled (As discussed here and here). How can I design a textbox that doesn't reveal the user's input but that takes/collects the Chinese input from the keyboard (basically a workaround for taking in Chinese input in a password TextBox)?
You can use the OnKeyPress event to intercept input before it appears in the TextBox. Save the input somewhere else and put some masking char into the TextBox.
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//save the key pressed
TextBox1.Text += "*";
e.handled = true;
}

How can I get more than one character to show in a messagebox from a textbox user input?

I have made a textbox and I want the user to type in a string of numbers and hit enter. I have setup the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
String UserBarcode;
Focus();
UserBarcode = Console.ReadLine();
MessageBox.Show(UserBarcode);
}
When I enter any key into the textbox, I get a message box with nothing in it. I want to have the program wait til it hears the enter key then display the contents of the textbox.
The Textbox.TextChanged event fires as soon as the text in the textbox is changed at all. If you want a message box with the full string, you probably want to consider using the Textbox.LostFocus event or a button's Click event.
So you could have something like (I'm taking a stab at this here, as I've used VB rather than C#)
private void textBox1_LostFocus(object sender, EventArgs e)
{
MessageBox.Show(sender.Text)
}
If you're using a button, the above function should work, but you'll want to substitute textBox1.Text for sender.Text.
Take a look at Focus and Validation Events
There are several events that you can handle, depending on your goals and how your application is designed. If you want to perform validation and/or are using data binding, you may want to go with handling the validating/validated events. By default data bindings update a bound property after OnValidating. If you use LostFocus and read the value from a bound object, instead of your control, you will get inconsistent results.
I was able to figure it out finally. For some reason when I manually entered the code I kept getting multiple random errors. I started a new Visual C # Windows Forms Application, Made a textbox, chose the keydown property and double clicked on it to have the program inject the code for the keydown function and then I filled in the if statement pointing to the enter key. The final code looks like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(textBox1.Text);
}
}

Weird KeyUp behaviour (main form handles events from child?!)

The problem:
KeyUp is handled by the grid in main window (Grid_KeyUp)
Main window shows a child window (e.g. MessageBox.Show(...))
User presses [Return] (Keys.Return) to close the MessageBox
Main window actually GETS the KeyUp event from that keypress in MessageBox
I have a very simple isolated sample that shows the problem I am having. Just create an empty WPF project and use this XAML:
<Grid KeyUp="Grid_KeyUp">
<Button Click="Button_Click"></Button>
</Grid>
And the code-behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Grid_KeyUp(object sender, KeyEventArgs e)
{
Console.WriteLine("KEYUP: " + e.Key);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this, "TEST");
}
}
If you click the button and press [Return] to close the MessageBox the output window will print:
KEYUP: Return
Weird facts:
I get the same behaviour when pressing [Escape] (KEYUP: Escape gets printed), but when I press [Space] the console does not print out anything!
e.Source and e.OriginalSource point to the button in the main window (which is, obviously, wrong).
If you put a breakpoint right after MessageBox.Show(...) the event does not get handled by out KeyUp handler (i.e. the output is empty).
Could anyone explain to me what is happening?
PS Target framework is: .NET 4 Client
Clearly what is happening is that when you press the enter or escape, the message box closes and before you lift up the button the MainWindow gets focus and catches the KeyPress aswell .
The reason why this is not happening with the space bar is that pressing spacebar pushes the button down, but does not release it until you release the spacebar yourself. This means that the spacebar is released before the button is "unpressed" and therefore the MessageBox only closes once the spacebar is released, so the MainWindow does not catch the button press.
You can test this by holding the spacebar in.
You will see that the button is held down but is not pressed until you release it.
You could prolly create a flag that goes off after the MessageBox close. Check if it's the first KeyPress (and maybe check that it happened less than 10 miliseconds after the close) and then just ignore it.
I know, its ugly, but I don't think there's much else you can do (if I'm right)

Prevent Silverlight DataGrid from Processing Enter Key

I have a datagrid in a Silverlight application. Clicking on the first row, and then pressing the Enter key causes the selection to move to the next row. I do not want this behavior - the Enter key is used for totally different purposes on this screen.
I realize that this is part of the editing framework, but I need a way to turn it off. I tried setting IsReadOnly to True (even though the control isn't technically read-only) and that didn't have any effect.
I attached to the datagrid KeyDown event but it's not called when the Enter key is pressed. It works fine for other keys.
I'm stumped. Thanks for your help.
Yeah, that's annoying.
As far as I know, the only option is to create your own control that inherits from DataGrid, and do what is needed before passing the event on to the base.
public class NewDataGrid : DataGrid
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//do what is needed here
e.Handled = true;
}
base.OnKeyDown(e);
}
}

How to give an initial value to textBox?

I want my C# program to have initial values for its textboxes. For example, in one of the textboxes, it should say "Please enter your name".
When you click (or tabStop) on the textbox, the initial value should disappear and the user will be able to enter their input to the textbox.
I can do all this with click_event, but using this method the initial text would not have less opacity. How am I able to achieve this?
This is how I finally did it:
Boolean first_time_click = true;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.ForeColor = System.Drawing.Color.Gray;
textBox1.Text = "Enter the Text";
}
private void For_First_Click()
{
if (first_time_click)
{
textBox1.Clear();
textBox1.ForeColor = textBox1.ForeColor = SystemColors.WindowText;
}
first_time_click = false;
}
private void textBox1_Click(object sender, EventArgs e)
{
For_First_Click();
}
I assume you are talking about winform (tabstop) you have to handle it within the event key-press. you can use the below code:
TextBox1.Select(0, TextBox1.Text.Length);
this will select the text and window will remove it for you as soon as the user start to typing
you can use the same code to have this behavior also for TabStop
All you need to do is set the Textbox's .Text property and use GotFocus event to clear the box when the person clicks (or tabs) into it to start typing.
Always remember that there are more ways than the mouse to navigate a form, so use the GotFocus event to determine when the user enters a control, and use the Validated event to determine when they've changed data and exited the control.
For this type of effect you need java script.Because java script provide you functionality of mouse hover and mouse out these are the functions which provide you the same functionality which u seeing in this page of search bar. If you need code reply me i can give you.

Resources