| | |
move items from listbox to another listbox button
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 55
Reputation:
Solved Threads: 0
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:
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:
C# Syntax (Toggle Plain Text)
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"
You can enhance the above code.
C# Syntax (Toggle Plain Text)
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; }
Last edited by Ramy Mahrous; May 10th, 2009 at 5:24 pm.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Feb 2009
Posts: 55
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
•
•
•
•
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.
•
•
•
•
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?
C# Syntax (Toggle Plain Text)
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; .... .... .... } }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Feb 2009
Posts: 55
Reputation:
Solved Threads: 0
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:
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
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;
}
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)?
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)
Last edited by Ramy Mahrous; May 12th, 2009 at 4:28 am.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Feb 2009
Posts: 55
Reputation:
Solved Threads: 0
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
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
My first reply works I tried it.... I don't know where's the error!!
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Similar Threads
- Listbox with Submit Button (PHP)
- Assigning Items From List Box to a Button... (VB.NET)
- Select all the items in a listbox right before submit. (JavaScript / DHTML / AJAX)
- To drag and drop multiple listbox items from one listbox control to another (ASP.NET)
- [req]How to move elements in the list box up and down using command button? (Visual Basic 4 / 5 / 6)
- Disable ASP.NET Button from Listbox (ASP.NET)
- Listbox Items as an array (ASP.NET)
- Send all items in a listBox to SQL server database table (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: Problems with static variables
- Next Thread: Problem with row filter
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






