Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to make a WPF application that has two list boxes (source and target)
and two buttons(>> button and << button) to move (not copy) one or multiple items from a list box the other.

The source list box is initialized with predefined values when the window loads (1,2,3,4 for example).

Another thing, when one or multiple items are moved back from the target list box to the source list box, it is inserted on its original order (as when the window has loaded).

The problem with my solution is that when I click the (<< button) the last saved items in MA7 arraylist items gets copied every time i click the (<< button)

Thank you.
Posted
Updated 21-Mar-15 5:40am
v5
Comments
sudevsu 18-Mar-15 12:55pm    
Post your code to understand where you are stuck.
sudevsu 18-Mar-15 13:02pm    
this is the not way to post your code. Please remove it. we actually want to help you. Show only part of code where you are stuck. Before someone deletes your question. Improve it
Member 11481290 18-Mar-15 13:47pm    
I didn't understand your response quite much.
Would you show me an example.
Thx
sudevsu 18-Mar-15 16:08pm    
http://www.c-sharpcorner.com/Forums/Thread/213414/move-items-from-one-listbox-to-another-C-Sharp-form-application.aspx

If you are looking for something like below

VB
Listbox1    ListBox2
   1               a
   2               b
   3               c

Now you have button one >> and button two <<

on button one(>>) click item should go in Listbox2
On button two(<<) click item should go in Listbox1
If you are having trouble in listbox after moving, you need to show datasource to Listbox.
something like below
Items.Add("something");
Listbox1.Datasource=Items;

let me know if you are having problems
 
Share this answer
 
This is what you are looking for. ASP.Snippets is awesome for code snippets
http://www.aspsnippets.com/Articles/How-to-move-ListBox-items-to-another-ListBox-in-ASPNet-using-jQuery.aspx[^]

If this has helped you accept it as solution
 
Share this answer
 
Comments
sudevsu 18-Mar-15 16:10pm    
if you don't want to do in Jquery you can do similarly in C# or VB code also
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace From_To_Listboxes
{
public partial class Form1 : Form
{
ArrayList MyArray = new ArrayList();
ArrayList SortedArray = new ArrayList();
ArrayList MA1 = new ArrayList();
ArrayList MA2 = new ArrayList();
ArrayList MA3 = new ArrayList();
ArrayList MA4 = new ArrayList();
ArrayList MA5 = new ArrayList();
ArrayList MA6 = new ArrayList();
ArrayList MA7 = new ArrayList();
ArrayList SelChangedFrom = new ArrayList();
ArrayList SelChangedTo = new ArrayList();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
lstbxFrom.SelectionMode = SelectionMode.MultiExtended;
lstbxTo.SelectionMode = SelectionMode.MultiExtended;

MyArray.Add("1");
MyArray.Add("2");
MyArray.Add("3");
MyArray.Add("4");
MyArray.Add("5");
MyArray.Add("6");
MyArray.Add("7");
foreach (string item in MyArray)
{
lstbxFrom.Items.Add(item);
}
}
private void btnFromTo_Click(object sender, EventArgs e)
{

lstbxTo.ClearSelected();

foreach (string item in lstbxFrom.Items)
{
MA1.Add(item);
}
foreach (string item in lstbxFrom.SelectedItems)
{
MA2.Add(item);


}
foreach (string item in MA2)
{
lstbxTo.Items.Add(item);
}
foreach (string item in MA1)
{
if (!(MA1.Contains(item) && MA2.Contains(item)))
{
MA3.Add(item);
}

}
lstbxFrom.Items.Clear();


foreach (string item in MA3)
{
lstbxFrom.Items.Add(item);
}

MA1.Clear();
MA2.Clear();
MA3.Clear();
}
private void btnToFrom_Click(object sender, EventArgs e)
{
lstbxFrom.ClearSelected();
if (lstbxTo.SelectedItems.Count != 0)
{
foreach (string item in lstbxTo.Items)
{
MA4.Add(item);
}
foreach (string item in lstbxTo.SelectedItems)
{
MA5.Add(item);
}

foreach (string item in MA5)
{
MA7.Add(item);
}
foreach (string item in MA1)
{
MA7.Add(item);
}


foreach (string item in MA7)
{
lstbxFrom.Items.Add(item);
}


foreach (string item in MA4)
{
if (!(MA4.Contains(item) && MA5.Contains(item)))
{
MA6.Add(item);
}
}

lstbxTo.Items.Clear();
foreach (string item in MA6)
{
lstbxTo.Items.Add(item);
}
}
foreach (string item in lstbxFrom.Items)
{
SortedArray.Add(item);
}
SortedArray.Sort();
lstbxFrom.Items.Clear();
foreach (string item in SortedArray)
{
lstbxFrom.Items.Add(item);
}
SortedArray.Clear();
MA4.Clear();
MA5.Clear();
MA6.Clear();
MA7.Clear();
}
}
}
 
Share this answer
 
Comments
Member 11481290 21-Mar-15 11:39am    
This is the solution to my problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900