Solved

textbox: add text to listbox when the enter key is pressed

Posted on 2008-06-19
3
354 Views
Last Modified: 2012-06-21
Hi,

I have a reasonably easy question but can't seem to get it right:

I have a textbox in my form (textBox1), when the enter key is pressed it should enter the text in the textbox in a listbox(listBox1)

How can i make it enter the text in the listbox (once for every enter pressed)?
0
Comment
Question by:Sjaaksken
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 75 total points
ID: 21824700
You can use the KeyUp event of the textbox.

Regards,
Bruce
private void textBox1_KeyUp(object sender, KeyEventArgs e)

{

    // check the key and just act if return (or enter) was pressed

    if (e.KeyCode == Keys.Return)

    {

        listBox1.Items.Add(textBox1.Text);

        // clear the text box or just comment out the next line 

        textBox1.Clear();

    }

}

Open in new window

0
 

Author Comment

by:Sjaaksken
ID: 21824856
Hi, thanks for the reply,

apparently I was close but when I did paste your solution and ran the program it didn't enter the text when the enter key was pressed. I tried assigning other keys to the 'if' statement but same result.

Am I forgetting something? It also doesn't clear the textbox as it doesnt seem to accept the enter action
0
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21826362
I think you solved the problem. If you still have problems, just let me know.

Regards,
Bruce
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

23 Experts available now in Live!

Get 1:1 Help Now