reading from text box

D

dani kotlar

This seems to be a simple question:
How do I get the program to react after text was typed into a test box
and Enter was pressed?
I tried to write a KeyDown event. The program compiles and runs, but
when I enter text and press Return nothing happens. The debugger also
gets stuck in this point. Here is the KeyDown I wrote:

private void inputTextBox_KeyDown(object sender, KeyEventArgs
e)
{
try
{
if(e.KeyCode == Keys.Enter)
{
writer.Write("CLIENT>>> " + inputTextBox.Text);
displayTextBox.Text +=
"\r\nCLIENT>>> " + inputTextBox.Text;
inputTextBox.Clear();
}
}
catch(SocketException ioe)
{
displayTextBox.Text += "\nError writing object";
}
}

What is wrong?

Danny K
 
D

Daniel

Why you catching a SocketException? Would that code throw a socket error?

You said you wrote that event, did you subscribe it to the box yeah? If you
used the designer to generate it this would be done for you

textbox.KeyDown += inputTextBox_KeyDown; //for example (.net 2.0 syntax)

Have you stuck a breakpoint in that code to see if that method fires. If it
doesnt then its the reason above i would expect.
 
D

dani kotlar

Thanks, I entered the line
textbox.KeyDown += inputTextBox_KeyDown;
and it works!

By the way, how do I get the designer (2.0 version) to generate it for
me?
 
D

Daniel

if you are using Visual Studio, click the lightening symbol in the
properties window on the right when the textbox is selected. This will show
all the evnts for that control, then find the one you want, keydown etc and
double click in the box next to it, and voila, creates the event, assigns it
and creates the method that will fire.
 

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