My windows form contains two listboxes. Listbox1 contains some items in it and listbox2 is empty. When I press a button on the form, then multiple selected items from listbox1 should be removed from Listbox1 and copied to Listbox2.
I tried with foreach loop on listbox1.SelectedItems but it removes only 1 item from list.
Anyone has solution or code for this?
You could do all in a single loop. You should use a simple for and loop backwards on SelectedIndices:
private void button1_Click(object sender, EventArgs e)
{
for(int x = listBox1.SelectedIndices.Count - 1; x>= 0; x--)
{
int idx = listBox1.SelectedIndices[x];
listBox2.Items.Add(listBox1.Items[idx]);
listBox1.Items.RemoveAt(idx);
}
}
you must store The values, you want to delete in other palce and then delete them from List,Here is sample code:
private void button1_Click(object sender, EventArgs e)
{
ArrayList tmpArr = new ArrayList();
foreach (object obj in listBox1.SelectedItems)
{
listBox2.Items.Add(obj);
tmpArr.Add(obj);
}
foreach (object obj in tmpArr.ToArray())
{
listBox1.Items.Remove(obj);
}
}
I did this using using the CopyTo method to copy the items to an array the length of the count of selected items and then looped around that array removing each corresponding item from ListBox1.
private void button1_Click(object sender, EventArgs e)
{
object[] itemsToRemove = new object[listBox1.SelectedItems.Count];
listBox1.SelectedItems.CopyTo(itemsToRemove, 0);
foreach (object item in itemsToRemove)
{
listBox1.Items.Remove(item);
listBox2.Items.Add(item);
}
}
For VS2005 I user something similar as I couldn't use .selectedIndices
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
for (int x = listBox1.SelectedIndices.Count - 1; x >= 0; x--)
{
int var = listBox1.SelectedIndices[x];
listBox1.Items.RemoveAt(var);
}
Its Works.
This is my method:
List<String> arr = new List<string>();
private void btnAdd_Click(object sender, EventArgs e)
{
arr.Add(txtItem.Text);
lstItem.DataSource = arr.ToArray();
txtItem.Focus();
}
//When i delete
private void btnRemove_Click(object sender, EventArgs e)
{
foreach (string item in lstItem.SelectedItems)
{
arr.Remove(item);
}
lstItem.DataSource = arr.ToArray();
}
I found this code worked for me. The aim was to delete multiple rows of "items" with the press of the delete key. I have used a ListViewBox for the original input storage.
private void visitorsOnsiteListLB_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Delete)
{
foreach (ListViewItem item in visitorsOnsiteListLB.SelectedItems)
{
visitorsOnsiteListLB.Items.Remove(item);
}
}
}
Related
So say I have three comboboxes (cmb1,cmb2,cmb3) and each has three items within it (a,b,c). If I choose an item in cmb1 say "a" then I use cmb2 or cmb3 it will only have b and c left in it and if I unselect a then it will be re-added back in to the list. How do I remove and add it back in succesfully?
List<string> list = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
list.Add("a");
list.Add("b");
list.Add("c");
foreach (string i in list)
{
comboBox1.Items.Add(i);
comboBox2.Items.Add(i);
comboBox3.Items.Add(i);
}//adds list items to combo boxes
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
if(comboBox1.Text == list[0].ToString())
{
comboBox2.Items.Remove(list[0]);
comboBox3.Items.Remove(list[0]);
}// removes item from other lists if chosen from one
if (comboBox1.Text == list[1].ToString())
{
comboBox2.Items.Remove(list[1]);
comboBox3.Items.Remove(list[1]);
}
if (comboBox1.Text == list[2].ToString())
{
comboBox2.Items.Remove(list[2]);
comboBox3.Items.Remove(list[2]);
}
}
}
This can be simplified:
List<string> list = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
list.AddRange(new string[] { "a", "b", "c" });
comboBox1.Items.AddRange(list.ToArray());
comboBox2.Items.AddRange(list.ToArray());
comboBox3.Items.AddRange(list.ToArray());
}
Using comboBox1 SelectionChangeCommitted event and a field to store the select item, add and remove the items in the other ComboBoxes:
private int PreviousSelectedIndex = -1;
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex < 0) return;
if (PreviousSelectedIndex > -1)
{
comboBox2.Items.Insert(PreviousSelectedIndex, comboBox1.Items[PreviousSelectedIndex]);
comboBox3.Items.Insert(PreviousSelectedIndex, comboBox1.Items[PreviousSelectedIndex]);
}
comboBox2.Items.RemoveAt(comboBox1.SelectedIndex);
comboBox3.Items.RemoveAt(comboBox1.SelectedIndex);
PreviousSelectedIndex = comboBox1.SelectedIndex;
}
I have form which should change text inside textbox on button click. Text should be read from List, k.pojam. So on first click to the button text Box should show first element in list, second click second element etc. How to fix problem ?
//List<Karta> list; this list already has some number of Karta objects
int cardCounter=0;
private void btnNext_Click(object sender, EventArgs e)
{
int currentCounter = 0;
foreach (Karta k in list)
{
if(cardCounter==currentCounter)
{
txtBoxPojam.Text = k.Pojam;
txtBoxPojam.Show();
cardCounter++;
}
currentCounter++;
}
edit txtBoxPojam.Text += k.Pojam; // I need something like this, but instead of adding string I would like to write another string instead of current, because obviously txtBoxPojam.Text = k.Pojam; doesnt work?
int mainCounter = 0;
int totalItems = 0;
List<string> LstItems = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
LstItems.Add("Test Item 1");
LstItems.Add("Test Item 2");
LstItems.Add("Test Item 3");
LstItems.Add("Test Item 4");
LstItems.Add("Test Item 5");
totalItems = LstItems.Count();
}
private void btnNext_Click(object sender, EventArgs e)
{
try
{
if (mainCounter > totalItems)
{
//your implementation
return;
}
txtNext.Text = LstItems[mainCounter].ToString();
mainCounter++;
}
catch (Exception)
{
}
}
Instead of txtBoxPojam.Text = k.Pojam; working combination is txtBoxPojam.Text = string.Empty; and txtBoxPojam.Text += k.Pojam;
txtBoxPojam.Text = string.Empty;
int cardCounter=0;
private void btnNext_Click(object sender, EventArgs e)
{
int currentCounter = 0;
foreach (Karta k in list)
{
if(cardCounter==currentCounter)
{
txtBoxPojam.Text += k.Pojam;
txtBoxPojam.Show();
cardCounter++;
}
currentCounter++;
}
I have a textBox where you can enter a name then you can click on a Button to delete that item with that name in a listview.
private void btnDelete_Click(object sender, EventArgs e)
{
foreach (ListViewItem Searchstr in listView1.Name)
{
listView1.Items.Remove(Searchstr);
}
}
Any idea on how to make this work?
void deleteItemFromListBox(string stringToDelete, ListView listBoxToDeleteItem)
{
for (int i = 0; i < listBoxToDeleteItem.Items.Count; i++)
{
if(stringToDelete==listBoxToDeleteItem.Items[i].Text)
{
listBoxToDeleteItem.Items[i].Remove();
}
}
}
Function takes string and listview than deletes listview element that contains the string.
I fill a listbox from a list with this code:
uList.Add(new KeyValuePair<int, string>(item.Id, item.Name));
listBoxHome.DataSource = uList;
listBoxHome.DisplayMember = "Value";
listBoxHome.ValueMember = "Key";
then i copy all the items in a new dialog form(Subtitiution) and I fill a checkedListBox1
private void button6_Click(object sender, EventArgs e)
{
Sub sub = new Sub();
for (int i = 0; i < listBoxHome.Items.Count; i++)
{
sub.checkedListBox1.Items.Add(listBoxHome.Items[i].ToString());
}
this.Opacity = 0.7;
sub.ShowDialog();
}
Then I get the selected indexes of the selected items into a list.
I want to choose 5 items and then place them in the first 5 places of the listbox.Game is the first form.I try the following code but it gives me error. Any idea?
private void button2_Click(object sender, EventArgs e)
{
List<int> indexes = new List<int>();
foreach (int indexChecked in checkedListBox1.CheckedIndices)
{
indexes.Add(indexChecked);
}
Game game = new Game();
foreach (int PlayingInd in indexes) // Loop through List with foreach
{
game.listBoxHome.Items.Insert(0, game.listBoxHome.Items[PlayingInd]);
game.listBoxHome.Items.RemoveAt(PlayingInd);
}
this.Close();
}
I'm using a listview as a shopping cart. I need to know how to recalculate the total value of the cart when I remove an item.
Here is my code for adding to listview;
private void btnACart_Click(object sender, EventArgs e)
{
int value = 0;
for (int i = 0; i < lvCart.Items.Count; i++)
{
value += int.Parse(lvCart.Items[i].SubItems[1].Text);
}
rtbTcost.Text = value.ToString();
}
Here is my code for removing items:
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int total = 0;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
lvCart.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
rtbTcost.Text = total.ToString();
}
I want to recalculate the total value of items an item is removed. How should I do that?
Something like this
On the form level declare
private int _listTotal;
Adding - I think here you have some problems because you should add to total when you add the item
private void btnACart_Click(object sender, EventArgs e)
{
int value = 0;
for (int i = 0; i < lvCart.Items.Count; i++)
{
value += int.Parse(lvCart.Items[i].SubItems[1].Text);
}
// how about lvCart.Items.Add(<myVal>)...???
_listTotal += value; // and here add myVal
rtbTcost.Text = _listTotal.ToString();
}
Then when removing - you don't want to use any "for-loops" on mutating collection. But "while" works perfectly on mutations
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int totalRemoved = 0;
while (lvCart.SelectedItems.Count > 0)
{
totalRemoved += Convert.ToInt32(lvCart.SelectedItems[0].SubItems[1].Text);
lvCart.Items.Remove(lvCart.SelectedItems[0]);
}
_listTotal -= totalRemoved;
rtbTcost.Text = _listTotal.ToString
}
Not tested but should work
You can't change the same list in foreach.
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
lvCart.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
The solution is create duplicate list and change it:
var newList = lvCart;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
newList.Items.Remove(lvCart.SelectedItems[0]);
total += Convert.ToInt32(item.SubItems[1].Text);
}
}
private void btnRemoveItem_Click(object sender, EventArgs e)
{
int total = 0;
foreach (ListViewItem item in lvCart.Items)
{
if (lvCart.Items[0].Selected)
{
total+=(int)lvCart.SelectedItems[0].SubItems[1].Text;//Add to total while romving
lvCart.Items.Remove(lvCart.SelectedItems[0]);
//total += Convert.ToInt32(item.SubItems[1].Text);
}
}
rtbTcost.Text = total.ToString();
}