copying items of one listbox to another - c#

I want to copy listbox items from one form to another....actually in the 1st form I have 2 listboxes and in the 2nd form I also have 2 listboxes. I want to move the items of the 1st form's listboxes to 2nd form..... please help me....

If you want to select the entire items from listbox1 to listbox2, then the easiest, most readable and fastest have to be:
listbox2.Items.AddRange(listbox1.Items);

public partial class Form1 : Form
{
List<String> mylistSource;
public Form1()
{
InitializeComponent();
mylistSource = new List<string>();
// populate source with test data
for (int i = 0; i < 25; i++)
{
mylistSource.Add(i.ToString());
}
//assign source to both lists
listBox1.DataSource = mylistSource;
listBox2.DataSource = mylistSource;
}
}
Just add 2 listboxes to a form and paste in code to run.
or if you just want to copy selected items you can simply do this:
foreach (var item in listBox1.SelectedItems)
{
listBox3.Items.Add(item);
}

In case of web form, use session to forward the data source of the list.

what you could do, is give the objects an extra property (eg. selected)
you bidn the collection to both the listboxes, but in the one you only show the ones with selected = false and in the other selected = true
and if you "move" the item, you just need to switch selected to true and refresh the ItemsSources

Related

C# How to prevent ListBox to select first item when you unselect the last one

C# How to prevent ListBox in MultiSimple selection mode to select first item automatically, when you unselect the last one selected item in the box - it happens only if listbox represented by my own class objects, and everything is ok when it represented by string objects. Thnx!
It seems like keeping track of the order of the list is the most important part. I would suggest maybe making an array of a struct. You can make this struct contain whatever you want for example:
struct itemList
{
public string itemName;
public int itemIndex;
//include whatever other variables that you need included in here.
}
Also, make sure you place your struct before the namespace. Then, making the array of the struct would look like this:
itemList[] items1 = new itemList[listBoxName.SelectedItems.Count];
All you would have to do then is to add the items to the array before you reorder the listBox
for (int i = 0; i < listBoxName.SelectedItems.Count; i++)
{
items1[i].itemName = listBoxName.SelectedItems[i].ToString();
items1[i].itemIndex = listBoxName.SelectedIndices[i];
}
Thank you very much, but i already use some like this. I don't understand why first item of listBox selected everytime i assign preloaded list as DataSource. I resolve this problem, by making another one temporal List of my object class, which items readed from binary file, and then push them one by one to my original List in foreach cycle by listOfObjects.Add(object) method. I know that every time after code ListOfTags.DataSource = null;
ListOfTags.DataSource = tags;
ListOfTags.DisplayMember = "Name"; if my tags (it is a List) are preloaded even in code (for example if i write code List<Tag> tags = new List<Tag> {new Tag("1"),new Tag("2"), new Tag("3")}; , this takes situation when first item of listbox selected, and starts selects everytime after that when i try do deselect last selected item in listBox.

How to store list of items temporarily?

I am trying to store a list of items i.e. a value selected from dropdown and text from textbox temporarily and then adding them 1 by one to database using foreach loop on listbox.
code behind button add event:
ListBox ListBoxFeatures = new ListBox();
ListBoxFeatures.Items.Add(new ListItem(txtBoxDescription.Text, ddlFeatures.SelectedValue));
and then i have used a foreach loop over it to grab all stored values and store in database but it always pick 1 row, means that i store only 1 row.
foreach(ListItem li in ListBoxFeatures.Items)
{
String txt= li.Text;
Int Value= li.SelectedValue.ToInt32();
//database logic i.e. InsertMethod
}
Your problem is you create a new listbox in each click event. You need to move the initialization outside of the button click. Assuming that ListBoxFeatures already exists before the button click, you can probably remove the whole line of ListBox ListBoxFeatures = new ListBox(); from your click method.
Shouldn't it be:
foreach(ListItem li in ListBoxFeatures.Items)
{ ... }
UPDATE
Also, the button event always creates a new listbox and then inserts 1 value to it... Why would you have more than one value?

Combo Box Size Issue After All Items Are Removed

My application contains a ComboBox that the user can delete items from. When the program starts up it populates the ComboBox from a list of strings read in from a configuration file.
Here is the code to add items:
// version list is an array of strings
foreach (string version in versionList)
{
versionComboBox.Items.Add(version);
}
if (versionComboBox.Items.Count > 0)
{
versionComboBox.SelectedIndex = 0;
}
Here is a screenshot of the combo box after it's been populated:
If the user clicks the Delete button the program removes the selected item from the ComboBox using the following code:
if (versionComboBox.SelectedIndex >= 0)
{
versionComboBox.Items.Remove(versionComboBox.SelectedItem);
}
if (versionComboBox.Items.Count > 0)
{
versionComboBox.SelectedIndex = 0;
}
Here is a screenshot of the combo box after a few items have been removed:
The problem I am having is when the last item is removed the ComboBox resizes itself to the size it was when it was initially populated. There aren't any items in the ComboBox but it sizes itself as if there were.
Here is a screenshot after all the items have been removed:
As you can see the size is too big. I would think that after all the items were cleared it would look like the following:
Any ideas as to why this is happening?
Try to use this at the end of your code when you are filling the combobox items:
comboBoxNumTreno.IntegralHeight = true; // auto fit dropdown list
Then to clear it up:
comboBoxNumTreno.ResetText();
comboBoxNumTreno.Items.Clear();
comboBoxNumTreno.SelectedIndex = -1;
comboBoxNumTreno.DropDownHeight = 106; // default value
comboBoxNumTreno.IntegralHeight = false;
I know this is an old post, but it took me a long time to figure this out and I wanted to let anyone in the future know. After you clear your combo box just do a blank add items and it resets the height.
comboBox1.Items.Clear();
comboBox1.Items.Add("");
To clear your combo box you can add this:
if (versionComboBox.Items.Count == 0)
{
versionComboBox.Text = string.Empty;
versionComboBox.Items.Clear();
versionComboBox.SelectedIndex = -1;
}
Another approach is to manipulate the items in the data source and rebind the control each time (a lot less for you to worry about).
set DropDownHeight property to fix size
versionComboBox.DropDownHeight = 106; // default value

How do I obtain selected rows in a DataGridView from different pages

I have a windows forms DataGridView, where I have data and a checkbox for each row.
I will select check box for a particular row and all the selected rows will be populated in another page.
if (grdEmp.Rows.Count > 0)
{
var selectedEmpIDs= from DataGridViewRow coll in grdEmp.Rows
where Convert.ToBoolean(coll.Cells["Select"].Value) == true
select coll;
if (selectedEmpIDs.Count() > 0)
{
foreach (DataGridViewRow row in selectedEmpIDs)
{
selectedEmp+= row.Cells["EmpId"].Value + ",";
}
}
}
This works good only for one page.
When I navigate to another page, and click the selected rows, the previous one goes off.
How do I resolve it.
Thanks
cmrhema
Note :Sorry for the confusion, When I meant it works good for a page, I meant paging.
I think I need to add more inputs,
There are 10 pages in the gridview.
I select the first record from each page of the gridview, one after another by clicking next page( Page next button).
But only the record that was selected the last is getting displayed and others and ignored off.
What could be the prblm
You can use a List or Dictionary or any other collection type globally, using Program.cs or using a static class. And store the selected rows into the list before you leave the page.
Rather than using a comma delimited string string for your list of ids you can instead use a List.
Your code will then become something like this:
if (grdEmp.Rows.Count > 0)
{
var selectedEmpIDs= from DataGridViewRow coll in grdEmp.Rows
where Convert.ToBoolean(coll.Cells["Select"].Value) == true s
select coll;
if (selectedEmpIDs.Count() > 0)
{
foreach (DataGridViewRow row in selectedEmpIDs)
{
if (!listOfIds.Contains((int)row.Cells["EmpId"].Value))
{
listOfIds.Add(((int)row.Cells["EmpId"].Value));
}
}
}
}
You will need methods to remove items from this list so adding event handlers for the checkbox selected event will probably work better.
The List object itself can simple live as a class level object of the form that containst your DataGridView.
This gets a little bit more complicated if you are managing your paging across forms, but the same principles of maintaining a list of selected ids applies.

Unable to select multiple items from a listbox in a Windows Phone 7 application

How can I select multiple items from the listbox in a Windows Phone 7 application?
e.g
listboxName.SelectedIndex = 0;
listboxName.SelectedIndex = 1;
listboxName.SelectedIndex = 2;
The above code selects 2 while I need to select all three of those.
The values I need to preselect are given to me in a array like
{true,true,true,false,false}
So I tried the using IsSelected like shown below... doesn't work.
int i = 0;
foreach (ListBoxItem currentItem in listboxName.SelectedItems)
{
if (tagindexeselected[i])
{
currentItem.IsSelected = true;
}
i++;
}
What would be the proper way to select multiple items in a listbox?
Hard to say there's a single, best way - it depends on how you're populating your list box, etc. First, be sure your list box's Selection Mode is set to Multiple or Extended.
One option is to use the ListBox's SelectedItems collection:
listBox1.SelectedItems.Add(listBox1.Items[0]);
listBox1.SelectedItems.Add(listBox1.Items[1]);
listBox1.SelectedItems.Add(listBox1.Items[2]);
Note also, in your example above, you're iterating over the SelectedItems collection - not the Items collection. If nothing is selected, that's an empty collection. Also, if your list box ItemsSource is not a series of ListBox Items (you can set your itemsSource to almost any enumeration), you will get an InvalidCastException when you go to run your foreach statement.
foreach (DataRowView item in lstServer.SelectedItems)
{
string WebServerIP = item[lstServer.DisplayMember].ToString();
string WebServerUrl = item[lstServer.ValueMember].ToString();
_WebObjIgent.Url = WebServerUrl;
}
Note : lstServer is Listbox of window application . By using Displaymember and valuemember proprty you can access value and text of listbox.

Categories