How to get items from ComboBox to ListBox - c#

I've problem that I can't find answer to. Basically I have a ComboBox and a Listbox. What I need is that when I select and item from ComboBox, for example item named "Label", items like "Font", "ForeColor" will appear in Listbox. How do I do that? I've tried to do it like this:
listBox1.Items.Clear();
if (comboBox1.SelectedIndex == 3)
{
listBox1.Items.Add("Fonts");
}
...But nothing happened.

You need to harness the ComboBox's SelectedIndexChanged event. From there, you will known which item was selected in the ComboBox then you add that item to your ListBox.
Did I get that right ?

You are looking to change the contents of the list box when the selected item in the combo box changes. So, you need to add an event handler for the combo box. You need to handle the SelectedIndexChanged event.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
switch (comboBox1.SelectedIndex)
{
case 0:
// add some items to the list box
break;
case 1:
// add some other items to the list box
break;
// etc. etc.
}
}
This is very similar to the code in your question. Presumably your code does not work because it is attached to the wrong event handler.

Related

How to delete items from a ComboBox at runtime using C# / WinForms

I have a Combo Box that I can already add items to and I want to be able to remove the item I have selected when I hit the delete key.
Here is the code I am using now.
private void commandComboBox_KeyDown(object sender, KeyEventArgs e)
{
var myComboBox = (ComboBox)sender;
string text = myComboBox.Text;
if (e.KeyCode == Keys.Enter)
{
myComboBox.Items.Add(myComboBox.Text); // Add
}
if (e.KeyCode == Keys.Delete)
{
myComboBox.Items.Remove(myComboBox.SelectedItem);
}
}
When I click in the combobox and start typing and then hit enter I hear a windows sound (not sure which one) and then the item is added to the list.
When I hit the dropdown button I see the item there with the text I entered above. When I hit delete the item goes away (at least I think it does) and then when I click somewhere else I get this exception
System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
Also when I hit the dropdown button I still see the empty spaces
So my question is how do I properly delete items from a ComboBox :)
Also if there is something better then a ComboBox for this kind of thing plz mention them as well, TY
The Windows sound you hear is actually an error beep. Combo boxes do not accept enter key presses, so it's beeping at you "no!" Your code also runs, of course, adding the item, but that doesn't change the fact that the combo box considers you pressing Enter when it has the focus to be an error. If you are intent on the current design, you need to eat the Enter key press after you've received it so that the combo box doesn't go on to try to process it. To do so, set e.SuppressKeyPress to true.
The exception you get is because you've deleted all of the items in the combo box, but some other section of your code tries to get the text of item #0 (the first item). There is no first item because you deleted it, so an exception is thrown. I'm not sure what code it is that's responsible for this, since I can't see it, but I'm guessing you have written a handler for something like the SelectedIndexChanged event.
Indeed, this is a very unusual interface. The purpose of a combo box is to present the user with a list of choices, not to allow them to type in multiple choices. If you want that, use a multi-line text box. At least that way, they'll be able to see all the things that they've entered.
Or, you could use the classic interface idiom for this, where there is a textbox to type into that works with an Add button to add the typed text to a ListBox control. A Delete button deletes the currently selected item in the ListBox. A Clear button clears all of the items in the ListBox. Yes, it is as confusing to use as it is to explain. Avoid these whenever possible. They were more popular in the bad old days of UI design.
Also when I hit the dropdown button I still see the empty spaces
These aren't actually empty spaces. Well, they are, but not really. :-) What I mean is that they are not placeholders representing individual "empty" items. That's just what you see when the entire combo box is empty (contains no items). Because it contains no items, it can't auto-size the height of its drop-down window, so it uses a fixed size.
I had same problem with ComboBox. Noticed that error occurs only if user hits somewhere else after removing the item but not selecting a new one. Solved it by adding selection of new item after removal. Also handled last item as special case. See working code below:
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
switch (e.KeyCode)
{
case Keys.Delete:
if ((comboBox.DroppedDown) && (comboBox.SelectedItem != null))
{
if (comboBox.Items.Count == 1) // Removing Last Item
{
comboBox.DroppedDown = false;
comboBox.Text = string.Empty;
comboBox.Items.Clear();
}
else
{
comboBox.Items.Remove(comboBox.SelectedItem);
comboBox.SelectedIndex = comboBox.Items.Count - 1;
}
e.Handled = true;
}
break;
}
}

How do I check if a selected item has changed from the previously selected item?

I have a listbox in my winform, when an item in the listbox is selected, a value is placed into a textbox in the same Form. There my many items in my listbox which when selected, i want the text box to be empty so i can pass in the new value. How do i check is if the user has clicked on something other their initial selected item? i get the currently selected item like this below:
var selectedItem = (ReportItems)listbox.selectedItem
You can use the SelectedIndexChanged event on your ListBox . You can create an event handler for this event to determine when the selected index in the ListBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ListBox. You can use the event handler for this event to load the information in the other controls.
See MSDN documentation: link
You can add a global variable for your ReportItems and call it 'selItem'.
After the user changed the selected Item you check the "new" selectedItem with the 'selItem'-variable.. i don't think that a listbox has a method that can check if the selection has changed from the previous one..
I'm not sure if there is a reason you're not leveraging the SelectionChanged event of the ListBox or not but you should be if you're not. Further, determining if it's different than the initially selected item should be pretty straight forward because you can save the initially selected item in a private variable in your form and compare every time the method handler for SelectionChanged fires.
Other than this, there's not much more I can suggest because your questions isn't terribly clear and there is no code to look at.
My solution was to always clear the textbox first. So as soon as a user selects an item in the listview, rather than populating the textbox straight away, clear the textbox before populating it.
clearText(); is called soon as a listbox item is clicked.
public void clearText()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
}

how to add item to a selected ListBox

I have a fixed listbox, which contains fixed items. In addition, I create several listboxes. I want to add a selected item from the fixed listbox to one of selected listbox, which is created.
How do I know which listbox is actually selected?
For each created Listbox I'm giving it a different ListBox.Name. I thought this might help me but I can't still solve this problem.
For each Listbox I'm trying to create a Radiobutton, but I dont know how to use it with ListBoxes.
You could try something like this:
public partial class Form1 : Form
{
ListBox lstSelected = null;
private void lb_Enter(object sender, EventArgs e)
{
lstSelected = (ListBox)sender;
}
}
The idea is this: for every listbox set Enter event to lb_Enter(), so you always have selected listbox in lstSelected var.
When you create a new listbox, you can use
ListBox lst = new ListBox();
lst.Enter += lb_Enter;
By checking Focused of Controls you can check a control already has focus or not
But I do'nt know what dou you mean by creating a radiobutton for each listbox?!
You need a way to select a ListBox:
Use drag and drop (the drop shows what listbox is selected)
Use a radio button or something similar to mark a listbox as the target
Use separate buttons for each listbox to be clicked to move the item to a specific listbox
There is no standard way to manage this, in fact, only one control can have focus so selecting a listbox and selecting an item at the same time will require you to make one of these constructions.
To use a radiobutton you will have to find out in code what radio button is checked and then decide what listbox belongs to this radiobutton.
If you need specific implementation details post your questions, code and issues so we can have a look.
Depends on how you want to implement the selection of the listboxes. You can store the ids on the parent when you got the focus. See Enter event.
public partial class Form1 : Form
{
private string selectedListBox;
public Form1()
{
InitializeComponent();
}
private void listBox1_Enter(object sender, EventArgs e)
{
selectedListBox = (sender as ListBox).Name;
}
}
Regards,
Bogdan

How to add list items for a combo box

I have tried to create items for a combo box but it is not working.Please help me out.
This is what i have so far;
private void cb_avg_weeks_month_SelectedIndexChanged(object sender, EventArgs e)
{
cb_avg_weeks_month.Items.Add('1');
cb_avg_weeks_month.Items.Add('2');
cb_avg_weeks_month.Items.Add('3');
}
Note:
cb_avg_weeks_month describes the name i have assigned to my combo box.
Well if you're only adding items to your combo box when the selected index has changed, then it'll never run because there aren't any items for the user to change the index on.
Populate your combo-box in your form's constructor after InitializeComponent();
if you work in Visual Studio you can add items to comboBox using comboBox's property.
if you want to do it using code, you can do it in constructor:
public Form1()
{
cb_avg_weeks_month.Items.Add('1');
cb_avg_weeks_month.Items.Add('2');
cb_avg_weeks_month.Items.Add('3');
}

ListBox: Display multiple selected items?

When I select multiple items in a ListBox, how can I display them? Any help will be appreciated.
First, you need to set the SelectionMode property on your ListBox to either SelectionMode.MultiSimple or SelectionMode.MultiExtended (so that you can select multiple items).
Next, you need to add an event handler for the SelectedIndexChanged event on your ListBox. Within this event handler, accessing the SelectedItems collection of your ListBox will provide you with access to a collection of all the selected objects.
From there, you can iterate through the collection to display the objects in any manner you choose. Here's an example event handler that displays the selected items in a TextBox called textBox1:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Clear();
foreach (object selectedItem in listBox1.SelectedItems)
{
textBox1.AppendText(selectedItem.ToString() + Environment.NewLine);
}
}

Categories