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.
Related
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);
}
}
I know that, for data grids, users can select the items, copy them, and then paste them outside of the form. Is there was a way to do that with listboxes? It looks like you can select multiple items in the listbox but it doesn't appear that it actually copies it if you try to paste the selected values outside of the form.
You can catch the event when user click ctrl + c to put the item to the clipboard.
This code is for the list box with multi-selection MultiSimple=true
private void ListBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
System.Text.StringBuilder copy_buffer = new System.Text.StringBuilder();
foreach (object item in ListBox1.SelectedItems)
copy_buffer.AppendLine(item.ToString());
if (copy_buffer.Length > 0)
Clipboard.SetText(copy_buffer.ToString());
}
}
To copy selected item via Ctrl+C use this code:
private void LstVehicles_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && (e.KeyCode == Keys.C))
{
Clipboard.SetText(this.yourListBoxName.SelectedItem.ToString());
}
}
Those 2 solutions didn't work for me, what did work was though clicking on the listBox, and generating a function called
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
inside I put...
Clipboard.SetDataObject(this.listBox1.SelectedItem.ToString());
So the complete solution was
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Clipboard.SetDataObject(this.listBox1.SelectedItem.ToString());
}
So did get a line from one of the solutions and one of the comments.
Thanks.
I can select only one line in the listbox, I know it not the best,
but by code below left mouse click copy all the listbox Items
to the Clipboard:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
System.Text.StringBuilder copy_buffer = new System.Text.StringBuilder();
foreach (object item in listBox1.Items)
copy_buffer.AppendLine(item.ToString());
if (copy_buffer.Length > 0)
Clipboard.SetText(copy_buffer.ToString());
}
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 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);
}
I have this code:
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "blahblah")
{
processing ps = new processing();
pictureBox1.Image = ps.blahblah(bmp);
}
else
{...
}
}
So the action of the ComboBox is done by clicking on the button1.
It is possible to take action immediately after selecting Item? without button clicking?
Subscribe to the SelectedIndexChanged event
comboBox1.SelectedIndexChanged += OnSelectedIndexChanged;
private void OnSelectedIndexChanged(object sender, EventArgs e) {
// Handle combo box changing
}
Try using this event,
ComboBox1.SelectedIndexChanged
and do
AutoPostBack = "true"
in your mark up if you want to check the selected item immediately after selecting item.