KeyPress question

H

Henry Jones

I am new to C# and wanted to capture the KeyPress for a textbox.

I created some code as follows:

private void textBox3_KeyPress(object sender, System.EventArgs e)

{

this.textBox2.Text = sender.ToString();

}

When I ran the code in debug, this function wasn't even entered. How can I
capture the KeyPress event (or Keydown)?



Bonus question: Why aren't all the methods listed in the IDE when in code
view? In VB they are?
 
D

David Gouge

Henry said:
I am new to C# and wanted to capture the KeyPress for a textbox.

I created some code as follows:

private void textBox3_KeyPress(object sender, System.EventArgs e)

{

this.textBox2.Text = sender.ToString();

}

When I ran the code in debug, this function wasn't even entered. How can I
capture the KeyPress event (or Keydown)?

Hi Henry,

Looks to me like you haven't wired the TextBox's KeyPress event up to
your method, like this:

this.textBox3.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.textBox3_KeyPress);

That will then result in the textbox keypress event firing and then
kicking off your method.

HTH

Dave
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Are you sure that the textBox3.KeyChangd event is being assigned?

Bonus question: Why aren't all the methods listed in the IDE when in code
view? In VB they are?

What you mean with this? all the method are listed in the dropdown where the
class member are listed.
 
H

Henry Jones

On my form I added a textbox called textBox4. When I look at the drop down
on the members list on Code View, I only see textBox4. No methods are
listed. If I double click on the textBox4, I will then see
textBox4_TextChanged.

If you do the same thing in VB, you will see all the methods for the
textBox4. I was wondering why this doesn't happen in C#. I am using Visual
Studio 2003.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

VB and C# are different in that aspect ( I think C# approach is better ) ,
In the design view select the textbox, go to property, select Events ( a
little thunder icon ) and there you have all the events, double click the
one you need and a stub method will be generated for you.
 
H

Henry Jones

Ignacio,

Thanks for the tip on how to get the events. You are right, it is more
elegant than the way VB does it.

Henry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top