开发者

Disabling buttons problem on C#

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

        private void lbList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.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)
    {
        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
    }


How about creating one method EnableButtons that enables/disables to buttons according to given criteria like "enable cmdRight2 only if.... is true".

Then, call the method whenever some of the criteria might change. The advantage of this over the way you're doing it now is that the criteria within the method are "absolute" (in that the buttons are either enabled or disabled in one go) instead of "relative" (enable the button when the user does this or that).

You could also call this method from the Application.Idle event instead of calling it in response to some user action.

EDIT

Declare the following method:

private void EnableButtons()
{
    controlX.Enabled = (<condition...>);
    controlY.Enabled = (<condition...>);
}

You can either invoke that method from the positions in code where something should change in the buttons' enabled states, or you can do the following in the constructor of the form:

public Form1()
{
    // Other code...

    Application.Idle += new <The respective event handler>;
}

Then, declare a method with the respective signature for the event and call EnableButtons there. This method would be called in situations where your application is "idle" (waiting for user actions).


I think you want

if (this.lbList1.SelectedItems.Count == 1)
{
}
else if(this.lbList1.SelectedItems.Count > 1)
{
}
else
{
}

instead of

if (this.lbList1.SelectedItems != null)

Then you could place all of this in a method called "EnableButtons" as mentioned elsewhere


The problem is that you are removing the items one by one, so when only one item is left, you essentially have one item selected so your program enables the cmdRight. The easiest way around this is to have

cmdRight2.Enabled = false;
cmdRight.Enabled = false;

at the end of the cmdRight2_Click method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜