C# type until you press enter - c#

I have Windows Form Application with TextBox and Label and I want to type something in the textbox and then press Enter to let's say show what I've typed in Label.
Example with button:
private void button1_Click(object sender, EventArgs e)
{
this.Label1.Text = this.TextBox1.Text;
}
I need to do exactly the same but with pressing Enter not button.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Label1.Text = TextBox1.Text;
}
}

I tried the code from Jan Anderssen and it works correctly. The error "Operator '==' can not be applied to operands of type 'char' and 'System.Windows.Forms.Keys" is because you are matching a character to Keys.Enter, make sure that the syntax is correct e. KeyCode.
E.KeyCode is used because in the event handler send a parameter with the value of e "KeyEventArgs e" variable and here is the key pressed.
private void txtText_KeyDown (object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.lblText.Text = this.txtText.Text;
}
}
Did you copy and paste the code?
If so, try doing it with the events of the properties box.
Click in the textbox -> Events ---> key down ---> double click and put the code there.
Do you have more than one form? This code may change.

you can use the text change event and check when enter pressed.
when you identify enter, then you can do what ever you want

Related

Need to trigger the button using only mouse click or alt + the hot key used for it in windows form

My problem
I have few buttons in a form. I need to trigger those button on mouse click or the alt + the alphabet used as the hot key in it.
I added & in front of the alphabet in the name property of the button. But my problem is that even if the alphabet is pressed without the use of alt key the below action is triggered.
The below is the method which triggers the button named FirstMatch.
public void firstMatch_Button_Click(object sender, EventArgs e)
{
Action_Raised(sender, "First Match");
}
First you need added & in front of the alphabet in the Text property not Name property of the button.
What you want to do is achieve by following code.
private void button1_Click(object sender, EventArgs e)
{
if (ModifierKeys.HasFlag(Keys.Alt) || e.GetType() == typeof(MouseEventArgs))
{
MessageBox.Show("button is clicked.");
}
}
You could change the property of your form KeyPreview to True
Now the form receives every key event first.
Then you can add an key up/down event to your form like this:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
// Replace "Keys.A" with the key you want
if (e.KeyCode == Keys.A && e.Alt)
{
this.firstMatch_Button.PerformClick();
}
}
And please remove the & otherwise the button gets further triggered.

How to check if key is pressed in a text box Windows Form

I'm using Visual Studio C# and trying to check if the ENTER key has been pressed while in a text box
private void PasswordTextBox_TextChanged(object sender, EventArgs e)
{
if(keyenterhasbeenpressed) //I dont know what to put here
{
MessageBox.Show("You pressed the enter key");
}
}
So when someone clicks enter in that text box it will do something.
The TextChanged event isn't really suited for checking for keypresses.
It's best used to examine the sender object which is the textbox itself.
To check for the ENTER key you can do the following:
private void PasswordTextBox_KeyDown(object sender, EventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
MessageBox.Show("You pressed the enter key");
}
}
or
private void PasswordTextBox_KeyPress(object sender, EventArgs e)
{
if(e.KeyChar == 13)
{
MessageBox.Show("You pressed the enter key");
}
}
It depends on what event best suits your needs.
You can always put a break point (assuming visual studio) and check what data is available to you in the eventargs and sender object
Inorder to link the function with the key press event of the textbox add the following code in the designer.cs of the form
this.textbox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandler);
Now define the function 'OnKeyDownHandler' in the cs file of the same form
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//enter key has been pressed
// add your code
}
}

How to change text of Label while typing in TextBox

Please I'm new to C#, I created a textBox and a label. What i am expecting is, if I type a value into the textBox, I want it to display on the label and if I change the value it should also change immediately on the label.
it work with the code below and i press enter key
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
label1.Text = textBox1.Text;
}
}
But I want it without press Enter/Return Key on keyboard.
Thanks for understanding
This works for VisualStudio
Select your TextBox in the Designer, go to the it's properties and click on the events (teh icon with the lightning). Then make a double click on the event that is called: TextChanged.
This creates a new function, that will always be called when the text of your TextBox changes. Insert following code into the function:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
label1.Text = tb.Text;
}
That's it.
label.DataBindings.Add("Text", textBox, "Text");
textbox KeyDown/Up/Press events may help.
For example
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
label1.Text += e.KeyData.ToString();
}

Richtextbox deletes selected text

I have the following code:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
}
}
When I press the N key , the selected text is replaced with "n". I read this Selecting text in RichTexbox in C# deletes the text ,but it had no effects.
I am using Windows Forms.
Likely, you will need e.Handled = true; in this to stop the event.
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.handled.aspx
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
e.Handled = true;
}
}
Try it yourself:
Open up the editor, type some text, mark some of this text and press N. What happens? The marked text is replaced with n.
The same thing happens in your RichTextBox. Important to understand here is, that with the event you set up, you only add some functionality and leave the default event handling (handled by the OS) intact.
So with your code, on a key press you just do
richTextBox1.Select(1, 3);
which selects some characters and afterwards the default event handling kicks in. Thus there is some marked text which gets replaced with N.
So, you simply have to mark the event as handled by yourself. Not using the Handled-property, but with the SuppressKeyPress-property.
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
e.SuppressKeyPress = true;
}
}
The documentation of Handled clearly states:
If you set Handled to true on a TextBox, that control will
not pass the key press events to the underlying Win32 text
box control, but it will still display the characters that the user typed.
Here is the official documentation of SuppressKeyPress.

C# Key Press; display key in Label

I have a form (Form1) and a label (lblTest)
What code do I need to insert so when any key is pressed, the key is displayed in the label? This event should take place when the form is selected
E.g. if the user presses g, a g is displayed in the label.
I have tried some code in the Form_KeyDown event, but I can't get it to work.
I'm currently looking at this.
You need to add
form1.KeyPreview = true;
(or set in the designer)
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx
Try the below:
...
myForm.KeyPreview = true;
...
private void CommsTesterUI_KeyDown(object sender, KeyEventArgs e)
{
label1.Text = e.KeyCode.ToString();
}
Try with this code if you need to build a string:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
lblControl.Text += (char) e.KeyCode;
}
else, if you need to show only the button pressed:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
lblControl.Text = ((char) e.KeyCode).ToString();
}
Obiosly, the focus must be on the form.

Categories

Resources