How to add and remove values from ComboBox to CheckedListBox - c#

What I need:
I need a ComboBox and a CheckedListBox with exact same values.
I have a Button to add values and delete values.
Here is my Add Button:
private void button5_Click(object sender, EventArgs e)
{
checkedListBox1.Items.Add(comboBox1.Text);
comboBox1.Items.Add(comboBox1.Text);
comboBox1.Text = "";
}
and my Delete Button:
private void button6_Click(object sender, EventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
I would like to be able to delete the entries in the CheckedListBox without having to select it first, I only need it to be selected into the comboBox1.

Since you're adding the same strings, you can use the IndexOf() method to get the index where the current string is located in your CheckedListBox and the RemoveAt() to remove it.
Verify that the ComboBox.SelectedItem is not null. You can use the GetItemText() method to get the string currently selected. If the SelectedItem is null, you get back a empty string.
private void button6_Click(object sender, EventArgs e)
{
string currentItem = comboBox1.GetItemText(comboBox1.SelectedItem);
if (!string.IsNullOrEmpty(currentItem))
{
checkedListBox1.Items.RemoveAt(checkedListBox1.Items.IndexOf(currentItem));
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
}
Method2:
If the Items it the two controls are located at the same index, you can instead use the ComboBox.SelectedIndex to RemoveAt() both:
private void button6_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex >= 0)
{
checkedListBox1.Items.RemoveAt(comboBox1.SelectedIndex);
comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
}
}

Related

Items in Combobox gets added

I've two radiobuttons and a single checkbox.
Items in checkbox will pop up according to the radiobutton selected.
The problem is when I click the second radiobutton after clicking the first radiobutton, Combobox pops us all the values from both radiobutton 1 and 2.
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
foreach (string apacservers in Properties.Settings.Default.B)
{
comboBox1.Items.Add(ItemsB);
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
foreach (string europeservers in Properties.Settings.Default.A)
{
comboBox1.Items.Add(ItemsA);
}
}
Any help would be much appreciated
you need to add comboBox1.Items.Clear(); before both of your foreachs.

Dont Save Item in ComboBox

I have a TextBox, a Button, and a Combobox
When I click the Button, I want the text in Textbox to be added to the Combobox Items
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items.Add(textBox1.Text);
}
My form until open. This text shows in the Combobox, but when I close the Form and open it again the text is not longer shown in the Combobox.
I want to save the text to the Collection Items of the Combobox. I don't want to use a database.
As others have mentioned in the comments, you need to understand and decide where you wish to store your values.
For the purpose of my example, I have created a simple text file to store these values. The code reads from the file and adds each line as an item into the ComboBox.
private void Form1_Load(object sender, EventArgs e)
{
// Read items from file into a string array
string[] items = System.IO.File.ReadAllLines(#"D:\ComboBoxValues.txt");
// Add items to the comobobox when opening the form
comboBox1.Items.AddRange(items);
}
private void button1_Click(object sender, EventArgs e)
{
// Add your new value to the combobox
comboBox1.Items.Add(textBox1.Text);
// Put all existing comobo box items into a string array
string[] items = comboBox1.Items.OfType<string>().ToArray();
// Save the array of items to a text file (this will not append, it will re-write the file)
System.IO.File.WriteAllLines(#"D:\ComboBoxValues.txt", items);
}
This may not be the most elegant way of going about it, but from the point of providing you an understanding - this should be more than sufficient.
if you don't like to use File System, you can use Preferences(but it's not recommendable to use preference to memorize large values), check this link to see how create a new setting
private void Form1_Load(object sender, EventArgs e)
{
string[] strItems = Properties.Settings.Default.items.Split(", ");
for(int i = 0; i < strItems.length; i++) {
comboBox1.Items.Add(strItems[i]);
}
}
private void button1_Click(object sender, EventArgs e)
{
//add your new value to the combobox
comboBox1.Items.Add(textBox1.Text);
//put all existing combo box items into a string array
string[] items = comboBox1.Items.OfType<string>().ToArray();
for(int i = 0; i < items.length; i++) {
//I assumed you had an items key in your settings
if(i == items.length - 1) {
Properties.Settings.Default.items += value;
} else {
Properties.Settings.Default.items += value + ", ";
}
}
//then you should to save your settings
Properties.Settings.Default.Save();
}

How to change comboBox items if i checked radioButton using c#

if i choose radio Button the combo box items change.
if i choose another radio Button the combo box item change .
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
comboBox1.Items.Add("google");
comboBox1.Items.Add("yahoo");
}
How can I do this??
If they are in same group, you can do this just by adding listener to one of them:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if(radioButton_1.Checked)
{
comboBox1.Items.Remove("google");
comboBox1.Items.Add("yahoo");
}
else
{
comboBox1.Items.Remove("yahoo");
comboBox1.Items.Add("google");
}
}

I want to show any selected item from CheckedListBox in a ListBox in C#

I have a windows form application that contains one "CheckedListBox" named "ChkBox1" and it contains this items (Blue, Red, Green, Yellow).
The form contains also an empty "ListBox" named "LstBox1".
I want when i check any item from "ChkBox1" it add to "LstBox1" and when i unchecked it from "ChkBox1" it removed from "LstBox1".
I think i should use "ItemChecked" event but i don't know how can i detect if the item checked or not and add it to another list.
This is my try:
private void ChkBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (ChkBox1.CheckedItems.Count > 0)
listBox1.Items.Add(ChkBox1.Items[e.Index]);
else if (ChkBox1.CheckedItems.Count == 0)
listBox1.Items.Remove(ChkBox1.Items[e.Index]);
}
but it add the item when i unchecked it not when i check it.
and this is another try:
private void ChkBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (ChkBox1.GetItemChecked(e.Index) == true)
listBox1.Items.Add(ChkBox1.Items[e.Index]);
else if (ChkBox1.GetItemChecked(e.Index) == false)
listBox1.Items.Remove(ChkBox1.Items[e.Index]);
}
Try this:
private void ChkBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState .Checked)
{
listBox1.Items.Add(ChkBox1.Items[e.Index]);
}
else
{
listBox1.Items.Remove(ChkBox1.Items[e.Index]);
}
}
The "ItemChecked" will send you an "ItemCheckEventArgs" that contains both the old and the new value.
It also contain the index of the value that has changed.
You can also check the "CheckedItems" property to get every checked items :
private void ChkBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
LstBox1.Items.Clear();
foreach (var item in ChkBox1.CheckedItems)
LstBox1.Items.Add(item);
}

select row from winforms listview

I have listview which is populated with list of data. Now I want to select desired row and on click button to recognize that item to delete from a collection.
Question is how to recognize selected row from the listview?
private void buttonDelete_Click(object sender, EventArgs e)
{
//selected data is of custom type MyData
var selected = (MyData)....?
}
Thanks
This should works
private void buttonDelete_Click(object sender, EventArgs e)
{
//selected data is of custom type MyData
var selected = yourListView.SelectedItems.First();
}
To add to #Zaphod's answer and make a little more robust:
private void buttonDelete_Click(object sender, EventArgs e)
{
if (yourListView.SelectedItems.Any())
{
//selected data is of custom type MyData
var selected = yourListView.SelectedItems.First();
}
}
You could use .Count > 0 instead of .Any() and .SelectedItems[0] instead of .First(). Whatever you find more readable/maintainable.
Old School answer :) without any LINQ statements
if(yourListView.SelectedItems.Count > 0)
{
var item = yourListView.SelectedItems[0];
}
I don't think you have to use casting for a deleting operation, just remove all the selected indices like this:
private void buttonDelete_Click(object sender, EventArgs e){
for (int i = listView1.SelectedIndices.Count - 1; i >= 0; i--)
listView1.Items.RemoveAt(listView1.SelectedIndices[i]);
}
or more simply:
private void buttonDelete_Click(object sender, EventArgs e){
foreach(ListViewItem item in listView1.SelectedItems)
listView1.Items.Remove(item);
}
As you can see the item which is selected is of type ListViewItem, you can bind your data to this item via Text property (if the data is string) or Tag property. I don't understand what your CustomData is, is it a type inheriting ListViewItem?
YOu should do this
private void buttonDelete_Click(object sender, EventArgs e)
{
if (yourListView.SelectedItems.Any())
{
//selected data is of custom type MyData
var selected = (MyData)yourListView.SelectedItems[0];
YourCollection.Remove(selected);
}
}

Categories