Disabling Buttons Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pabicoch
    New Member
    • Feb 2010
    • 4

    Disabling Buttons Problem

    Ok so I'm trying to move items from one listbox to another by using multiple buttons i.e

    I have 2 buttons cmdRight and cmdRight2 which are both disabled on form load

    If the user selects a single item on the first listbox a cmdRIght button enables but cmdRight2 is still disabled , if the user selects multiple items on the first listbox a cmdRight2 button enables but cmdRight is disabled.

    I've got the move buttons to work but the problem I'm having is after moving multiple items with the cmdRight2 button the cmdRight button enables (which it shouldn't it should only enable after selecting a single item in the listbox). I've tried numerous if statements etc. and yet it still happens.

    I'm new to C# so any help would be appreciated.

    Thank You

    Code:
    private void lbList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (lbList1.SelectedItems != null)
                {
                    cmdRight.Enabled = true; //enable cmdRight
                    cmdClear.Enabled = true; //enable cmdClear
                    if (this.lbList1.SelectedItems.Count > 1)//if multiple items selected
                    {
                        cmdRight.Enabled = false;
                        cmdRight2.Enabled = true; //enable cmdRight2              
                    }
                }
            }
            
            private void cmdRight2_Click(object sender, EventArgs e)
            {
                foreach (int i in lbList1.SelectedIndices)
                {
                    lbList2.Items.Add(lbList1.Items[i].ToString());
                }
                while (lbList1.SelectedItems.Count > 0)
                {
                    lbList1.Items.Remove(lbList1.SelectedItems[0]);
                }
                cmdRight2.Enabled = false;
            }
    
            private void cmdRight_Click(object sender, EventArgs e)
            {
                if (lbList1.SelectedItems != null)
                {
                    lbList2.Items.Add(lbList1.SelectedItem); //Add selected item from list1 to list2
                    lbList1.Items.Remove(lbList1.SelectedItem);//remove the selected item in list1
                    
                    //cmdRight.Enabled = false; //disable cmdRight
                }
            }
    Last edited by tlhintoq; Feb 16 '10, 12:38 AM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      As you move your items, the selectedindex keeps changing...
      Thus your selectedindex event handler keeps executing...
      So at some point during the moving process the criteria match and the button enables.

      I also worry about this
      Code:
      while (lbList1.SelectedItems.Count > 0)
                  {
                      lbList1.Items.Remove(lbList1.SelectedItems[0]);
                  }
      Removing items for a list starting at zero can have unexpected results.

      As soon as you remove item 0, item 1 becomes item 0 - item 2 becomes item 1 and so on.

      I would recommend you remove items for the END of the list first, working backward down to 0 so you don't have the elements shifting position.

      Or just remove them all at once instead of one by one
      Code:
      lbList1.Items.RemoveRange(lbList1.SelectedItems);

      Comment

      • Pabicoch
        New Member
        • Feb 2010
        • 4

        #4
        --------------------------------------------------------------------------------

        As you move your items, the selectedindex keeps changing...
        Thus your selectedindex event handler keeps executing...
        So at some point during the moving process the criteria match and the button enables.
        I thought this was why but I have no idea how to fix it.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Just add an extra bool as a flag

          AmMoving = true;

          THen only enable the button if AmMoving == false;

          Comment

          Working...