Is it possible to know what key on the keyboard that was pressed through any of these syntax in any way.

textBox->KeyPress
textBox->KeyUp
textBox->KeyDown

I like to find an if statement that says something like this, though I know this is wrong:

if ( textBox-> KeyPress != BackSpace) 
{
//Action;
}

Recommended Answers

All 9 Replies

IIRC, the event handlers will give you an EventArgs object that has this information.

I am not sure if I understand. I think you meen that the textbox event handler has this information in any way but from here I dont know how to continue.
What object is an "EventArgs object" ?

IIRC, the event handlers will give you an EventArgs object that has this information.

KeyPress, KeyUp, and KeyDown are events. You have to set up handlers and subscribe to the event before you can use them. Here's some documentation.

The documentation shows something like this. I suppose this is my event that I have set up.
However, the documentation tells something about e->KeyCode but I dont have KeyCode as a member to e->
Am I on the right track or have I missed something.

private: System::Void Edit_TextChanged(System::Object^  sender, System::EventArgs^  e) 
{
       if (   ?    == Keys::C)
      {
	 MessageBox::Show("You pressed C");
      }

}

KeyPress, KeyUp, and KeyDown are events. You have to set up handlers and subscribe to the event before you can use them. Here's some documentation.

Am I on the right track or have I missed something.

You have now missed something, you are trying to access the KeyCode in wrong handler (TextChanged).

Instead, for e.g. the KeyUp event, use ...

private: System::Void textBox1_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
{
	if(Keys::C == e->KeyCode)
	{
              // Do something here ...
	}
}

Thanks but I still dont understand. I have created a new textBox1 to my form. The handler look like this:

private: System::Void textBox1_TextChanged_2(System::Object^  sender, System::EventArgs^  e) 
{
}

Is it true that I only will change some things so the handler look like this and also put this code inside:

private: System::Void textBox1_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)  
{
               if(Keys::C == e->KeyCode)
	{
              // Do something here ...
	}
}

If I do this, this will not compile, I think it is something still that I dont understand :-/

You have now missed something, you are trying to access the KeyCode in wrong handler (TextChanged).

Instead, for e.g. the KeyUp event, use ...

private: System::Void textBox1_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
{
	if(Keys::C == e->KeyCode)
	{
              // Do something here ...
	}
}

Strange if it does not compile,
maybe you have added the handler in a 'wrong' way.

To add the handler for the KeyUp event,
1) Select the textbox control on your form
2) right-click and select Properties
3) double-click on the row which reads "KeyUp"
=> the editor creates the handler for you
4) type in the code and compile

Thank you very much, that was the problem. I hadn´t add the handler in this way.
I had just double clicked it once the textbox was on the form, not on the KeyUp event.
So this is a difference I know now.

Strange if it does not compile,
maybe you have added the handler in a 'wrong' way.

To add the handler for the KeyUp event,
1) Select the textbox control on your form
2) right-click and select Properties
3) double-click on the row which reads "KeyUp"
=> the editor creates the handler for you
4) type in the code and compile

Is there any member to Keys:: that can tell if Any key on the keyboard is pressed then begin action ?

if(Keys:: ? == e->KeyCode)
{
//Action;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.