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;
}
Related
On my form stands a checkedlistbox which i want to filter with a textbox. I tried this, but nothing happens. Which part is wrong?
List<string> ListCheckboxItems = new List<string>(); //new
private void textBox1_TextChanged(object sender, EventArgs e) //new
{
var filteredItems =
ListCheckboxItems.Where(item => ListCheckboxItems.Contains(item));
checkedListBox1.Items.Clear();
foreach (object item in filteredItems)
{
checkedListBox1.Items.Add(item);
}
}
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++;
}
quick question: When I click anywhere on the list box (Not clicking on the items) it automatically selects the first item, how can I remove this? I want the user to select a specific item.
Thanks!
Code:
private void FriendsLb_Click(object sender, EventArgs e)
{
var obj = FriendsLb.SelectedItem;
if (obj != null)
{
FriendsLb.MouseDown += FriendsLb_MouseDown;
}
}
private void FriendsLb_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var item = FriendsLb.IndexFromPoint(e.Location);
if (item >= 0)
{
try {
FriendsLb.SelectedIndex = item;
userRightClick.Show(FriendsLb, e.Location);
}
catch { return; } // No item selected
}
}
I have a ListBox that when an item is selected, it is shown in a label as well. However, when I want to remove the selected item, program breaks and shows a NullReferenceException.
My code:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = "Your Selected: " + listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = "";
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
It may appear, that there's no selected item in the listbox, so you have to check for that:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = null == listBox1.SelectedItem
? ""
: "Your Selected: " + listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e) {
// Looks redundant, listBox1_SelectedIndexChanged will do
//label1.Text = "";
// Deselect item, but not remove it
if (listBox1.SelectedIndex >= 0)
listBox1.SelectedIndex = -1;
// In case you want to remove the item (not deselect) - comment out the code below
// if (listBox1.SelectedIndex >= 0)
// listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
Edit: as for counting listbox items, there's no event fo this in the current listbox implementation. So you have to do it manually:
if (listBox1.SelectedIndex >= 0) {
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
lbItemsCount.Text = listBox1.Items.Count.ToString();
}
Another way is to use click event of the list box , if we do not want to double click the one list box item for the deselection of another list items.
ex:
private void ListBox_Right_Click(strong textobject sender, EventArgs e)
{
Btn_Left.Enabled = ListBox_Right.SelectedIndex >= 0;
ListBox_Left.ClearSelected(); // to clear the list selection/highlight
Btn_Right.Enabled = false; // for my specification
}
}
private void ListBox_Left_Click(object sender, EventArgs e)
{
Btn_Right.Enabled = ListBox_Left.SelectedIndex >= 0;
ListBox_Right.ClearSelected(); //to clear the list selection/highlight
Btn_Left.Enabled = false;// for my specification
}
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);
}
}
}