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.

Recommended Answers

All 17 Replies

Could you please post in the full method for the enter command ?

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.

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

@ddanbe, OP was after the text move triggering by the <enter> key press :)

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?

In this case, it's a matter of taste keyeventargs has more properties than KeyPressEventArgs. Here, both can achieve the same.

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

What don't you understand about it?

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.

how can i delete text from text box when i clicked on del button,,,,???pls help me ,,,

i used this code:

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

        }

bt it doesnt work

can anybody pls suggest me a good gui c# programming book

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.