Accepting Returns

Posted on 2004-09-27
Last Modified: 2012-05-05
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?
Question by:fattumsdad
LVL 15

Expert Comment

ID: 12164603
Is this a Windows app or web app?

Assisted Solution

w_shaila earned 100 total points
ID: 12164612
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
                  if (e.KeyChar == (char)13)

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

Accepted Solution

GrumbleBot earned 300 total points
ID: 12164973
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.


Assisted Solution

dsabo earned 100 total points
ID: 12165179
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

Author Comment

ID: 12165627
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 :(

Expert Comment

ID: 12165721
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)

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now