hi
I would like to make a code where I can move items from listbox1 to other listbox2 using the button1(as moving the item to listbox2) and button2(as moving the item to listbox1).
I would like that before I click on the items in my listbox1, the button1 will be disabled or grayed out. And the same with my button2.
I already figured out the button1 to work but the button2 seems I have to add something which I'm not sure. Could you please check my code why I'm stuck up? Thanks

Here is the code:

private void btnToRight_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                listBox2.Items.Add(listBox1.SelectedItem);
                listBox1.Items.Remove(listBox1.SelectedItem);
                btnToRight.Enabled = false;                            
            }
        }

        private void btnToLeft_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                listBox1.Items.Add(listBox2.SelectedItem);
                listBox2.Items.Remove(listBox2.SelectedItem);
                btnToLeft.Enabled = false;                
            }
        }

        private void listBox1_Leave(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
            {
                btnToRight.Enabled = false;
            }
            else
            {
                btnToRight.Enabled = true;
            }
        }

        private void listBox2_Leave(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem == null)
            {
                btnToLeft.Enabled = false;
            }
            else
            {
                btnToLeft.Enabled = true;
            }
        }

I've another solution to the question "you need to disable every button when its ListBox controls Items not selected"

Form_Load()
{
button1.Enabled = false;
button2.Enabled = false;
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem == null)
                button2.Enabled = false;
            else
                button2.Enabled = true;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
                button1.Enabled = false;
            else
                button1.Enabled = true;
        }

You can enhance the above code.

Hi Ramy
I tried your code but it didn't work. I've seen that in your if statements, you maybe forgot to put the brackets. From what I've learned it's not advisable to write the if-statements if they don't have brackets.
I think I have it work you can try this one to see if it also works in your side. I'm not sure why your code didn't work. It might be I did something wrong or what. But you can check.
Btw, do I still have to declare the functions of the two buttons (btnToLeft and btnToRight, or maybe in your case here is the button2 and button1) I have? Because in your example, you only put the listbox1_SelectedIndexChange and listbox2_SelectedIndexChange?

Here is my code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
         }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            string inputyourText;
            inputyourText = textBox1.Text.Trim();

            if (inputyourText.Length > 1)
            {
                listBox1.Items.Add(inputyourText);
                textBox1.Text = string.Empty;
            }
            else
            {
                MessageBox.Show("Es ist ung├╝ltig");
            }
        }


        private void cmdDelete_Click(object sender, EventArgs e)
        {
            int listBoxSelectedIndex = listBox1.SelectedIndex;
            listBox1.Items.RemoveAt(listBoxSelectedIndex);
            //Selected "Enabled" in the Properties as false
            cmdDelete.Enabled = false;

         }


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem!=null)
            {
                cmdDelete.Enabled = true;
//You have to declare this code to have the "btnToRight" enabled
//if you select the items in the listbox1
                btnToRight.Enabled = true;
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {   
           //This is added to have the btnToRight and btnToLeft function
            if (listBox2.SelectedItem!=null)
            {
                cmdDelete.Enabled = true;
//You have to declare this code to have the "btnToLeft" enabled 
//if you select the items in the listbox2
                btnToLeft.Enabled = true;
            }
        }


       private void btnToRight_Click(object sender, EventArgs e)
       {
           if(listBox1.SelectedItem!=null)
           {
               listBox2.Items.Add(listBox1.SelectedItem);
               listBox1.Items.Remove(listBox1.SelectedItem);
//To make the button gray, you have to disable the "btnToRight"
// in the Properties into "False"
                btnToRight.Enabled = false;
            }
        }

       private void btnToLeft_Click(object sender, EventArgs e)
       {
           if (listBox2.SelectedItem!=null)
           {
               listBox1.Items.Add(listBox2.SelectedItem);
               listBox2.Items.Remove(listBox2.SelectedItem);
//To make the button gray, you have to disable the "btnToLeft" 
//in the Properties into "False"
                btnToLeft.Enabled = false;
            }
        }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I tried your code but it didn't work. I've seen that in your if statements, you maybe forgot to put the brackets. From what I've learned it's not advisable to write the if-statements if they don't have brackets.

No need for brackets; it's just one statement.

Btw, do I still have to declare the functions of the two buttons (btnToLeft and btnToRight, or maybe in your case here is the button2 and button1) I have? Because in your example, you only put the listbox1_SelectedIndexChange and listbox2_SelectedIndexChange?

No, you can write just one event handler for Button and one for ListBox, then assign your two buttons to the event handler inside you know which button raised it then go some action.

private void btn_Click(object sender, EventArgs e)
{
string buttonName = ((Button)sender).Name; //I used button name as identifier you can use anything else
switch(buttonName)
{
case "Right":
//your movement code 
break;
case "Left":
//your movement code 
break;
....
....
....
}
}

I hope it works right now.

Hi Ramy
I will try this again. The btn_Click on your code is that for both (left and right)?
I have my movement code here:

private void btnToRight_Click(object sender, EventArgs e)
{
if(listBox1.SelectedItem!=null)
{
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
//To make the button gray, you have to disable the "btnToRight"
// in the Properties into "False"
btnToRight.Enabled = false;
}

Btw, is there a way I can edit my previous post?
I have to edit some errors in my previous post but it seems I can't find the edit button.


Thanks again

The btn_Click on your code is that for both (left and right)?

Yes.
You can't as there's a new reply.
I see you've some logic error.
As I understood, you need when there's no selected item in left listbox the left button is disabled and for the right side too.
So, you don't need to disable the button in its handler because you do that in selectedindexchanged event handler in the listbox (refer to my first\second reply)

Hi again
What I would like is that if I don't select any text from my listbox1
the left and right button should be disable. That means it's gray out.
Once I select something from my listbox1 then the right button should be enable, so that I can move the text to the listbox2. And once I moved it, the right button should be grayed-out again

Now if I select text from my listbox2 to be moved to listbox1, the left button should be enable, so that I can move the the text to listbox1. And once I moved it, the left button should be grayed out again.

That's why I set the two buttons as false in the Properties Enable.
Which is, before I select any text, these two buttons are disable or grayed out.

Can you please tell me where is my error?

Thanks

This article has been dead for over six months. Start a new discussion instead.