An error in key Down function [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am working with c# windows application and i need to enter some record to database after inserting in to textbox. I have tried this code
private void textBoxItemCode_KeyDown(object sender, EventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}
but i am getting an error near e.KeyCode so how to make it

You need to use the following args
System.Windows.Forms.KeyEventArgs
Your handler should look like this
private void textBoxItemCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}
That way you have access to the KeyCode property which does not exist on the base EventArgs

It Should be like this :-
private void textBoxItemCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}

Related

windows form keyup event is not working c# [duplicate]

This question already has an answer here:
keydown in c# doesn't work for some reason
(1 answer)
Closed 5 years ago.
This code is not working in my main form
void main_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
e.SuppressKeyPress = true;
}
}
Try to set Form property: main.KeyPreview = true;

C# - Hide window on input from user [duplicate]

This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 7 years ago.
I have put this code in my application, I want the window to hide when control + Q is clicked, why does this not work?
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Q)
{
this.Hide();
}
}
You can check the Key modifier in order to handle Keys combination
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q && e.Modifiers == Keys.Control)
{
//do stuff
}
}

Disable Enter Key in Textboxes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am developing a chatting system. And I have a Button to send the text in the text box to the chat log. How am I going to stop the user from press Enter key and to disable the Enter key. I know there are many posts out there like this, but the solutions haven't worked for me.
You can try something like this:-
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
I think you do not need to stop the user from pressing enter but instead send the chat to the other person on press of enter.
Also if you have any other shortcuts to be allowed then you can have a look at this C#: How to make pressing enter in a text box trigger a button, yet still allow shortcuts such as "Ctrl+A" to get through?
Using the same you can also block
private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress=true;
}
}
Your question is a little ambiguous to say the least; however, the textbox control has an event called KeyDown : http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
This way, you can capture whenever Enter is pressed and modify and behavior as needed, here is an example
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (Keys.Enter == e.KeyCode)
{
MessageBox.Show("Enter Was Pressed");
textBox1.Text = new String(textBox1.Text.Where((ch, i) => i < textBox1.Text.Length - 2).ToArray());
textBox1.SelectionStart = textBox1.Text.Length;
}
}

Making text box visible/unvisible c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm working on a windows form application and I have a button and a textbox in it.
When the button is pressed, it should make the textbox visible and hidden.
myTextbox.Visible = !myTextbox.Visible;
Did you try Google?
textBox1.Visible = false;
You can toggle the visibility by doing:
if(textBox1.Visible == true)
textBox1.Visible = false;
else
textBox1.Visible = true;
WinForm:
private void button1_Click(object sender, System.EventArgs e)
{
textBox.Visible = !textBox.Visible;
}
WPF:
private void button1_Click(object sender, RoutedEventArgs e)
{
if (textBox.Visibility != System.Windows.Visibility.Hidden)
textBox.Visibility = System.Windows.Visibility.Hidden;
else
textBox.Visibility = System.Windows.Visibility.Visible;
}
You can find an example here
private void button1_Click(object sender, System.EventArgs e)
{
/* If the CTRL key is pressed when the
* control is clicked, hide the control. */
if(Control.ModifierKeys == Keys.Control)
{
((Control)sender).Hide();
}
}
textbox.visible=true;
you should try this on buttonClick event

C# - preparing event after pushing two buttons on keyboard [duplicate]

This question already has answers here:
How to use multiple modifier keys in C#
(9 answers)
Closed 8 years ago.
i've got problem. If I push one key I can get event for example:
if (e.KeyCode == Keys.F4)
{
Method();
}
How could I do the same if I push two keys? For example Enter + F4?
FormLoad()
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
//Works for Ctrl+F4
if (e.Control && e.KeyCode == Keys.F4)
{
//Do something
}
}
See if this work for you.

Categories