Hello,
I am trying to move multiple items from one list box to another, i have tried the following code, but the output so far just gives me "{collection}" in the other list box. -
private void btnMoverr_Click(object sender, EventArgs e)
-
{
-
-
//lstBoxr.Items.Add(lstBoxl.SelectedItems);
-
//lstBoxl.Items.Remove(lstBoxr.SelectedItems);
-
-
while (lstBoxl.SelectedItems.Count > 0)
-
{
-
-
lstBoxr.Items.Add(lstBoxl.SelectedItems);
-
lstBoxl.Items.Remove(lstBoxl.SelectedItems);
-
}
-
-
-
//lstBoxr.SelectedItems.Add(lstBoxl.SelectedItems);
-
//lstBoxl.SelectedItems.Remove(lstBoxl.SelectedItems);
-
}
-
i'm using C# in visual studio 2008, any ideas would be appreciated!
Thanks,
Pal
What tlhintoq had said earlier is the proper way. You have to go for a second loop in reverse order. For fun I coded this way outside the first loop: -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
listBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3", "Item4" });
-
}
-
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
-
foreach (int i in listBox1.SelectedIndices)
-
{
-
listBox2.Items.Add(listBox1.Items[i].ToString());
-
}
-
listBox1.Items.Remove(listBox1.Items[3].ToString());
-
listBox1.Items.Remove(listBox1.Items[2].ToString());
-
listBox1.Items.Remove(listBox1.Items[1].ToString());
-
listBox1.Items.Remove(listBox1.Items[0].ToString());
-
-
-
-
-
-
}
-
}
-
}
-
-
17 19980
You iterate through the collection of selected items and display one by one.... Basically you have the collection not individual items...
Regards
Dheeraj Joshi
Try the followng code, hopefully it will work. Actaually ListBox Control has an event called SelectedIndexChange(), to fire it I have used another Button but wrote nothing in its eventhandler because the Page_Load() method is !IsPostback. So when you select State in the first ListBox1, and click the Button, the second ListBox2 will take the value from the first one. - using System;
-
using System.Collections;
-
using System.Configuration;
-
using System.Data;
-
using System.Linq;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.HtmlControls;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Xml.Linq;
-
-
public partial class Default3 : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (!IsPostBack)
-
{
-
ListBox1.Items.Add("anything");
-
ListBox1.Items.Add("State");
-
ListBox1.Items.Add("anything");
-
ListBox1.Items.Add("anything");
-
}
-
}
-
private void LoadListBox()
-
{
-
ListBox2.Items.Clear();
-
-
ListBox2.Items.Add("West bengal");
-
ListBox2.Items.Add("Assam");
-
ListBox2.Items.Add("Maharashtra");
-
ListBox2.Items.Add("Assam");
-
-
}
-
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
if (ListBox1.SelectedIndex == 1)
-
{
-
LoadListBox();
-
}
-
}
-
}
-
Hey sanjib65,
You are not moving the items from one list box to other....
Selected index changed occurs when property of selected item changes. How you are relating this to the problem?
Regards
Dheeraj Joshi
Thanks guys.. still havn't solved the issue yet but incase you think of anything more let me know! cheers.
Basically you want to move items(Selected items of course) from one list box to other....? Am i right..?
Regards
Dheeraj Joshi
Yes Dheeraj,
I have two list boxes, and i want to move multiple items (yes, selected) from one list box to another!
Sorry friends, probably I was in hurry and did not follow the problem. Actually what he wanted to do is clearing one ListBox and moving the Items from first to second. Have I understood it now properly? Please let me know so that I can have a try.
There is a property of "SelectedIndexCollection". This will provide a list of numbers that are the indexes of the selected times of the ListBox. For example: if the first, third and tenth items were selected you would get indexes of 0,2,9
I suggest, copy the selected items to the second ListBox in one loop then remove the items *in reverse order* during a second loop.
The reason you remove in reverse order is because as soon you remove item 2, item 3 becomes 2, 4 becomes 3 and so on, invalidating the list. But if you take away 9, then 3, then 0 you don't face that problem.
Hi
sanjib65
the aim is to move multiple selected items from one listbox to another listbox. i have tried the following code;
private void btnMovell_Click(object sender, EventArgs e)
{
lstBoxl.Items.Add(lstBoxr.Text);
lstBoxr.Items.Remove(lstBoxr.SelectedItems);
but it just moves one selected item at a time.
the aim is to move multiple selected items from one listbox to another listbox. i have tried the following code;
You are not listening to the advice being given you.
You have to go through the ListBox.Items collection in a loop and move the specific items one at a time. MSDN On the ListBox.SelectedIndexCollection
sorry im new to programming and dont understand it. im not purposely ignoring advice!
Try this -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
listBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3", "Item4" });
-
}
-
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
-
foreach (int i in listBox1.SelectedIndices)
-
{
-
listBox2.Items.Add(listBox1.Items[i].ToString());
-
}
-
}
-
}
-
}
-
-
What I've done is very simple. Adding 4 Items in listBox1 first, so that when the form opens up listBox1 is full. Now selecting all Items just clicking the button I move all items at once to listBox2.
@sanjib65
hi sanjib65,
brilliant! now everything i select is copied to the other listbox, but its not a 'move' since the items in the original listbox are still there after the move.
so i modified the code as below but, all the items don't get removed! strange.
private void btnMoverr_Click(object sender, EventArgs e)
{
foreach (int i in lstBoxl.SelectedIndices)
{
lstBoxr.Items.Add(lstBoxl.Items[i].ToString());
lstBoxl.Items.Remove(lstBoxl.Items[i]);
}
}
Any ideas??
thanks so much for your help.
Any ideas??
Yeah, go back and read the earlier advice telling you to NOT do it that way. First copy everything (in one loop) THEN remove everything selected in REVERSE order (in a second loop) so you don't screw up the indexes of the selected items.
What tlhintoq had said earlier is the proper way. You have to go for a second loop in reverse order. For fun I coded this way outside the first loop: -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
listBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3", "Item4" });
-
}
-
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
-
foreach (int i in listBox1.SelectedIndices)
-
{
-
listBox2.Items.Add(listBox1.Items[i].ToString());
-
}
-
listBox1.Items.Remove(listBox1.Items[3].ToString());
-
listBox1.Items.Remove(listBox1.Items[2].ToString());
-
listBox1.Items.Remove(listBox1.Items[1].ToString());
-
listBox1.Items.Remove(listBox1.Items[0].ToString());
-
-
-
-
-
-
}
-
}
-
}
-
-
Thanks a lot everyone! Managed in the end!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: gabe_pu |
last post by:
List Box Multiple Selections
I have a list box on my page and IŽm trying to add new
items on it. The insert operation must be under the
selected item. When I make the insertion it is done ...
|
by: Peter |
last post by:
Hello!
Please, could anyone tell, is it possible to set multiple items to be
selected in list control in the code? For example when the web form is loaded
three items of 5 are selected in list...
|
by: Robin Tucker |
last post by:
Apologies for the report, but this problem is doing my head in! :
I have a list box (just happens to be owner draw). When I select multiple
items using the CTRL key, the items I have selected...
|
by: Dany P. Wu |
last post by:
Hi everyone,
One of my Windows forms contain two listbox controls, with Add and Remove
buttons between them.
The idea is to allow users to select multiple items from one ListBox, click
the...
|
by: Brian Henry |
last post by:
Since no one else knew how to do this I sat here all morning experimenting
with this and this is what I came up with... Its an example of how to get a
list of items back from a virtual mode list...
|
by: anchi.chen |
last post by:
Hi People,
Just wondering if any of you have ever come across any javascript
examples that will allow one to drag and drop multiple items between
lists? That is, users would be able to use the...
|
by: =?Utf-8?B?S3Jpc2huYQ==?= |
last post by:
Hi,
I am devloping one web application using .net framework 2.0.One page has
7 dropdown list control.When i update the values first bind the values to
the drop down llist then selected text...
|
by: swethak |
last post by:
hi
i wrote a code to select multiple items in a drop down list.And i store all the items in my database.But in that i select multiple items and submit that items last item only stored.Please...
|
by: KrazyKasper |
last post by:
Access 2003 Multi-Column List Box Select Multiple Items
I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |