move items from listbox to another listbox button

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

move items from listbox to another listbox button

 
0
  #1
May 10th, 2009
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:

  1. private void btnToRight_Click(object sender, EventArgs e)
  2. {
  3. if (listBox1.SelectedItem != null)
  4. {
  5. listBox2.Items.Add(listBox1.SelectedItem);
  6. listBox1.Items.Remove(listBox1.SelectedItem);
  7. btnToRight.Enabled = false;
  8. }
  9. }
  10.  
  11. private void btnToLeft_Click(object sender, EventArgs e)
  12. {
  13. if (listBox2.SelectedItem != null)
  14. {
  15. listBox1.Items.Add(listBox2.SelectedItem);
  16. listBox2.Items.Remove(listBox2.SelectedItem);
  17. btnToLeft.Enabled = false;
  18. }
  19. }
  20.  
  21. private void listBox1_Leave(object sender, EventArgs e)
  22. {
  23. if (listBox1.SelectedItem == null)
  24. {
  25. btnToRight.Enabled = false;
  26. }
  27. else
  28. {
  29. btnToRight.Enabled = true;
  30. }
  31. }
  32.  
  33. private void listBox2_Leave(object sender, EventArgs e)
  34. {
  35. if (listBox2.SelectedItem == null)
  36. {
  37. btnToLeft.Enabled = false;
  38. }
  39. else
  40. {
  41. btnToLeft.Enabled = true;
  42. }
  43. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: move items from listbox to another listbox button

 
0
  #2
May 10th, 2009
I've another solution to the question "you need to disable every button when its ListBox controls Items not selected"

  1. Form_Load()
  2. {
  3. button1.Enabled = false;
  4. button2.Enabled = false;
  5. }
  6. private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
  7. {
  8. if (listBox2.SelectedItem == null)
  9. button2.Enabled = false;
  10. else
  11. button2.Enabled = true;
  12. }
  13.  
  14. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  15. {
  16. if (listBox1.SelectedItem == null)
  17. button1.Enabled = false;
  18. else
  19. button1.Enabled = true;
  20. }
You can enhance the above code.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

Re: move items from listbox to another listbox button

 
0
  #3
May 11th, 2009
thanks Ramy I'll try this now and will post the result:-)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

Re: move items from listbox to another listbox button

 
0
  #4
May 11th, 2009
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;
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: move items from listbox to another listbox button

 
0
  #5
May 12th, 2009
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.
  1. private void btn_Click(object sender, EventArgs e)
  2. {
  3. string buttonName = ((Button)sender).Name; //I used button name as identifier you can use anything else
  4. switch(buttonName)
  5. {
  6. case "Right":
  7. //your movement code
  8. break;
  9. case "Left":
  10. //your movement code
  11. break;
  12. ....
  13. ....
  14. ....
  15. }
  16. }
I hope it works right now.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

Re: move items from listbox to another listbox button

 
0
  #6
May 12th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: move items from listbox to another listbox button

 
0
  #7
May 12th, 2009
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)
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

Re: move items from listbox to another listbox button

 
0
  #8
May 12th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: move items from listbox to another listbox button

 
0
  #9
May 12th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC