in windows form application,i wrote this code in enter event of textbox to add the enterd text into a listbob control.but it didnt worked.

listbox1.items.add(textbox1.text);

how to solve this problem.

The code is correct assuming those are the names of the objects. As KM499 said, please provide the full code as there may be other interfering factors.

these are screens for that project.
after the text is entered in text box and enter is pressed,the text should be added to text box.but it didnt worked

The enter event only fires when you give the control focus when it previously didn't have it. For example clicking on the textbox.

You would need to fire the code under a KeyPress event for textbox1 and use

if (e.KeyChar == (char)13)
{
    listbox1.items.add(textbox1.text);
}

to detect it was the enter key pressed and add the text to the list.

Edited 2 Years Ago by Mike Askew

This works:

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(textBox1.Text);
        }
    }
}

a6321bd44c4f1cf2bb21d6f4ccf462b0

Thanks Mike, I did not notice that.
Well instaed of the Button I used the OP could implement a KeyDown event from the TextBox.

Yeap, KeyDown or KeyPress as I covered above would work. IIRC KeyDown has a different event parameter which would variate it slightly from my example?

i attached two images of vs screen,but they didnt displayed,why?
and i cant understand e.keychar,plz help

when enter is pressed,text of text box isnt added to listbox.why?

Probablily because you did not implement the KeyPress event as Mike Askew proposed. You have the code already perhaps you don't know how to do it?
So: click your textbox to select it. Go to the properties window and click on the square with a lightning on it. You see a list of events you can implement for a textbox. Scroll untill you find the KeyPress event. Double click. You will be redirected to your code ready to fill in your implementation of the event, namely the code given by Mike.

i used this code:

        private void button1_Click(object sender, EventArgs e)
        {
        textBox.text=null;

        }

bt it doesnt work

Hi, Ovro Habib welcome. :)
If you have a question, please don't pose it in this or other thread, but make your own thread.
But to answer your question anyway: Follow the procedure above, but instead of using a KeyPress event, use a Click event.
Next fill in this code:

TextBox TB = sender as TextBox;
TB.Text = string.Empty;
This article has been dead for over six months. Start a new discussion instead.