Deleting a given item in listview and in combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scalp994
    New Member
    • Feb 2012
    • 8

    Deleting a given item in listview and in combobox

    Hey there,
    I have a listview with 2 columns that puts the text taken from a textbox in the first column and the value of a radiobutton in the second one. Upon pressing the "add" button the text and the value appear in the listview, and the text is also added to a combobox.

    Code:
    ListViewItem lvi3 = new ListViewItem(textBox3.Text);
    //now adding the radiobutton value
    lvi3.SubItems.Add(textfromradiobutton);
    //refreshing the list
    listView3.Items.Add(lvi3);
    //adding the text from textbox to the combobox
    comboBox1.Items.Add(textBox3.Text);
    I've also a contextmenustri p which is attached to the table, and upon pressing the "delete row" button in it, it deletes a whole row from the listview.

    Code:
            private void usuńRekordToolStripMenuItem2_Click(object sender, EventArgs e)
            {
                if (listView3.SelectedItems.Count != 0)
                {
                    foreach (ListViewItem lvi3 in listView3.SelectedItems)
                        lvi3.Remove();
                }
            }
    My problem is that even if I remove a given text from the listview, it stays in the combobox. How can I implement such a feature that when a given row is deleted from the listview, the same values disappear from the combobox?
  • Samuel Jones
    New Member
    • Jan 2011
    • 48

    #2
    I have come across this issue many times before. The way I have solved it is that I have a separate list that is full of index numbers.

    It ends up being something like this
    Code:
    List
    Index    Contents
    0        4
    1        2
    2        3
    3        1
    4        0
    5
    ...
    The index column is in the same order as the listbox so that the list's index = ListBox.Selecte dIndex and the contents at that index is the corresponding SelectedIndex of the combobox.

    The code is then implemented as follows
    Code:
    -Delete Button's OnClick Event-
    {
        int ind = ListBox.SelectedIndex;
        ListBox.Remove(ind);
        ComboBox.Remove(list(ind));
        (for loop that adjusts index numbers higher than ind, i.e. if ind=2, any index higher than that needs to be subtracted 1.)
    }
    It is a nasty way of doing it, but it works.

    Comment

    • Scalp994
      New Member
      • Feb 2012
      • 8

      #3
      Thanks for the response, I've accomplished the same effect in another way, though.

      Code:
      if (listView3.SelectedItems.Count != 0)
                  {
                      foreach (ListViewItem lvi3 in listView3.SelectedItems)
                      {  
                          string selection = lvi3.Text;
                          
                          comboBox1.Items.Remove(selection);
                          lvi3.Remove();
      
                      }
                          
                      
      
      
                  }

      Comment

      • Samuel Jones
        New Member
        • Jan 2011
        • 48

        #4
        cheers for that... that will be very useful.

        Comment

        Working...