Update a list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sbandalli
    New Member
    • Feb 2009
    • 53

    Update a list box

    Hello All,


    I have a list of items in the listbox.(The items to the list box were added from the textbox eg ( listBox1.Items. Add(textBox1.Te xt)), Now If I have to alter the Listbox eg: If there is an item called Electricity in the listbox and now the user decides to replace Electriciy with an Item called Food by typing in the textbox how do I do this?? Thank you all....I am working on VS 2008 Winform.
  • falaque
    New Member
    • Feb 2009
    • 12

    #2
    use KeyValuePair class

    use KeyValuePair class:
    int key=someNo.
    String value="value".
    KeyValuePair<in t,String> kvp=new KeyValuePair<st ring,string>(ke y,value);


    for all the item, create above object(remember key should be unique), where value will represent the string to display, like "Electicity " etc.

    now if user select an item. then get the selected item and change the value to new value or replace the old key value pair item with new one.

    Comment

    • babai28
      New Member
      • Jul 2008
      • 59

      #3
      The key value pair is a good suggestion. However, you may also use the item index property which is also unique for each item. There may be number of 'electricity' items with different indexes. To edit you may assign the edited test like:

      Code:
      lstBoc.Items[6].Text="New Electricity";
      Regards

      Comment

      • falaque
        New Member
        • Feb 2009
        • 12

        #4
        my earlier given solution is bit complex. i forgot the way babai28 has suggested :)

        any way do in following way:
        int index = listBox1.Select edIndex;
        listBox1.Items[index] = "new_val";

        Comment

        • sbandalli
          New Member
          • Feb 2009
          • 53

          #5
          I tried your solution like this:

          int index = listBox1.Select edIndex;
          listBox1.Items[index] = textBox1.Text;

          But as soon as I select an item in the list box and try to feed a new value in the textbox1 it shows me this error:

          An unhandled exception of type 'System.StackOv erflowException ' occurred in mscorlib.dll. Thank you all

          Comment

          • sbandalli
            New Member
            • Feb 2009
            • 53

            #6
            I tried your solution like this:

            int index = listBox1.Select edIndex;
            listBox1.Items[index] = textBox1.Text;

            But as soon as I select an item in the list box and try to feed a new value in the textbox1 it shows me this error:

            An unhandled exception of type 'System.StackOv erflowException ' occurred in mscorlib.dll. Thank you all

            Comment

            • falaque
              New Member
              • Feb 2009
              • 12

              #7
              above give code will trigger listBox1_Select edIndexChanged event so be careful.

              i think you are putting this code
              in listBox1_Select edIndexChanged event. that is why it is calling this same the same code again and again. but i think in your problem you should put the code in textbox's text change event.

              Comment

              • faelnefal
                New Member
                • Nov 2008
                • 16

                #8
                try this

                listBox1.Items[listBox1.Select edIndex] = textBox1.text;

                Comment

                • sbandalli
                  New Member
                  • Feb 2009
                  • 53

                  #9
                  Hi , I tried doint that, Before I even enter anything in the textbox and click on Save (so tht it saves in the Listbox) it says Index is not valid,because its taking -1 as the index.

                  I need to enter few Items in the textbox so that it is saved in my Listbox and then later I should be able to replace an item in the Listbox by entering new value in the TextBox , giving me error , please help...

                  Comment

                  • babai28
                    New Member
                    • Jul 2008
                    • 59

                    #10
                    Clearifications Required!

                    First tell me how are you handling the events.
                    What I mean is, how are you editing a particular item in the listbox.
                    Suppose your listbox displays three items

                    Electricity
                    Water
                    House

                    Now, you want to change the "House" text to "Bedroom". How do you expect the user to achieve this? Will he be double clicking it? Or will there be a separate button called "Edit" which when clicked after selecting the item "House" will bring the textbox before you?
                    Until you tell this it would be difficult to answer.
                    The next thing is the selectedItem.In dex property is returning -1 as there is actually no item selected. You need to handle this by putting an if condition:
                    Code:
                    if(lstBox.SelectedItems.Count!=0)
                    {
                           //put your code here
                    }

                    Comment

                    • sbandalli
                      New Member
                      • Feb 2009
                      • 53

                      #11
                      I have a Listbox, Textbox and a Combobox on the form. Suppose user enters House in the textbox then I am copying the House text from the textbox to Listbox and Combobox. Like this if the user enters
                      House
                      Electricity
                      Food.
                      Then I copy each one to the Listbox and also copying the items to the Combobox.
                      Now the user changes his mind and wants to update Electricity as Energy,The user double clicks on the Electricity ( in the listbox) then enters Energy in the Textbox. I want that change to happen in the listbox as well as Combobox.

                      I am really new to .NET, Hence my questions might be silly..I am trying to learn stuff, so please help.

                      Also is there any way I can copy the text in the textbox to multiple combobox??
                      if i do combobox1.Items .Add(textbox1.t ext); it copies only to combobox1, but I have multiple combobox ie, combobox1,combo box2 ......comboboxn .

                      Thanks a lot.........

                      Comment

                      • babai28
                        New Member
                        • Jul 2008
                        • 59

                        #12
                        Fair Enough. I would suggest the following design:
                        Just write the code as it is:
                        declare a class level variable:
                        Code:
                        int globalIndex = -1;
                        Now,
                        Handle the mouse double click event of the list box as follows:

                        Code:
                        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
                                {
                                    if (listBox1.SelectedItems.Count != 0)
                                    {
                                        globalIndex = listBox1.SelectedIndices[0];
                                        textBox1.Text = listBox1.SelectedItems[0].ToString();
                                        textBox1.SelectAll();
                                    }
                                }
                        Next handle the edit button click event as follows:
                        Code:
                        private void button1_Click(object sender, EventArgs e)
                                {
                                    if (textBox1.Text != String.Empty)
                                    {
                                        listBox1.Items[globalIndex] = textBox1.Text;
                                        comboBox1.Items[globalIndex] = textBox1.Text;
                                        textBox1.Clear();
                                    }
                                    else
                                        MessageBox.Show("Please put a value");
                                }

                        The result:
                        1) The form looks like this in the beginning:refer First Pic of the Attachment
                        2) After double clicking the item Tap: refer Second Image of the attachment
                        3) I replaced the textBox Text witht the text "Cooler" and that gets reflected in the combobox and the listbox as well as in the third pic :)
                        Check if this is exactly what you want. You may make slight changes in the design to suit your specific requirement.
                        Attached Files

                        Comment

                        • babai28
                          New Member
                          • Jul 2008
                          • 59

                          #13
                          Regarding your question of copying in multiple combo box, you yourself know the answer.

                          comboBox1.Items .Add(textBox1.T ext);
                          comboBox2.Items .Add(textBox1.T ext);
                          comboBox3.Items .Add(textBox1.T ext);
                          comboBox4.Items .Add(textBox1.T ext);
                          comboBox5.Items .Add(textBox1.T ext);
                          comboBox6.Items .Add(textBox1.T ext);
                          comboBox7.Items .Add(textBox1.T ext);
                          comboBox8.Items .Add(textBox1.T ext).
                          .
                          .
                          .
                          .
                          comboBoxn.Items .Add(textBox1.T ext);

                          Even if you put a hundred comboBoxes I guess not CLR will raise its voice :)

                          Happy .Netting.

                          Comment

                          • sbandalli
                            New Member
                            • Feb 2009
                            • 53

                            #14
                            Hi,
                            I did try, I am sorry to say again its not working, I dont know where I am going wrong, when I double click on the Listbox item I guess its not entering the event.

                            Here is my code:

                            private void listBox1_MouseD oubleClick(obje ct sender, MouseEventArgs e)
                            {
                            if (listBox1.Selec tedItems.Count != 0)
                            {
                            globalindex = listBox1.Select edIndices[0];
                            textBox1.Text = listBox1.Select edItems[0].ToString();
                            textBox1.Select All();
                            }
                            }


                            private void button2_Click(o bject sender, EventArgs e) //SAVE BUTTON
                            {
                            if (textBox1.Text == String.Empty)
                            {
                            panel1.Visible = true;
                            }



                            else if (textBox1.Text != String.Empty)
                            {

                            string itemToAdd = textBox1.Text;
                            listBox1.Items. Add(itemToAdd);

                            }
                            }

                            Sorry for the trouble,Thanks a lot for the hlep..

                            Comment

                            • sbandalli
                              New Member
                              • Feb 2009
                              • 53

                              #15
                              Hi,
                              Ignore the previous messgae...It worked......... ...thanks a lot lot ....
                              Thaks a lot for your help, It worked, It worked when I created a seperate Modify button other than my SaveButton which intially saves items in the list box. When the user wants to modify the listbox, I double click on the item in the listbox and then use Modify button in order to update. If I try to do the same usign only SaveButton, since the intial globalindex will be -1, it gives me error.
                              But seperate buttons are working ,thanks a lot...

                              I have few more questions, Now I want to enter the listbox items to the database table, I have no clue how to do this, Do i ceate a connection intially by creating DataProvider object and Generate DataSet object and then try to put the listbox items in the Database table????? Give me some ideas so that I can start working around...

                              Comment

                              Working...