Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Accepting Returns

I have a multiline text box followed by a command button.  When the command button is pressed, it takes the text in the multiline box and sends it over a LAN (net send).  Is there a way to accomplish this same task, by pressing ENTER in the text box after the message is typed?
0
fattumsdad
Asked:
fattumsdad
3 Solutions
 
Timbo87Commented:
Is this a Windows app or web app?
0
 
w_shailaCommented:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
                  if (e.KeyChar == (char)13)
                  {
                        MessageBox.Show("Test");
                  }
            }


For Example Sake I am using MessageBox . you can write the same code where you have written in button control
or write a generic function which you can call in button Click and in the above code
0
 
GrumbleBotCommented:
You could also select the command button on the forms AcceptButton property. If this is a winform that is. not sure if it works on web from or not. This will make that the default button on the form and when enter is pressed it is the same as clicking on the command button.

Good luck
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dsaboCommented:
You could handle the keyup or keydown event. I recomend the keyup event...

private void txtUsr_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
        if(e.KeyData == System.Windows.Forms.Keys.Enter)
        {
      //your code here
         }
}

I hope this helps, David S
0
 
fattumsdadAuthor Commented:
Do I have to set any specific properties on txtMessage (the multiline text box)?  None of the above code is working...  when I press enter, it just puts me to the next line on the text box.  I tried AcceptsReturn true and false...  neither work :(
0
 
thedude112286Commented:
You need to register your function with the text box's keyup or keypress event.  For example:

to catch keypress:
TextBox tb...
// this is an event set to call your delegate
tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
...

//This was coppied from w_shaila's post
private void tb_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
          {
               if (e.KeyChar == (char)13)
               {
                    MessageBox.Show("Test");
               }
          }
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now