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

Posted on 2008-06-19
Last Modified: 2012-06-21

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)?
Question by:Sjaaksken
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

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

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)
        // clear the text box or just comment out the next line 

Open in new window


Author Comment

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

Expert Comment

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


Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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