How to add list items for a combo box - c#

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');
}

Related

How to disable combox dropdown when user enters text into the combox

I have a simple combobox in c# forms which is populated from an array.
I have set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This allows me to filter through the list quickly by typing a string into the combobox and matching items are displayed as I type along. This works great.
However, when the drop down list is open and I start typing, the filtered list appears on top of the dropdown list but I cannot select from the filtered list but only from the drop down.
How to disable drop down list while open as soon as user enters a character into the combobox.
Currently only have one method for the combobox
private void SelectJobDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
//plenty of code here
}
I have tried adding other methods for the combobox such as KeyPress or Keydown but none seems to be working for as I'm very likely doing something wrong
Using Visual Studio 2015
If I understood you correctly you don't like the overlapping list over the old drop down. Since you type letters into the ComboBox I would suggest to use the comboBox1_TextUpdate event. This nice line of code should fix your problem:
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.Simple;
Setting the ComboBox.DropDownStyle property, which
specifies whether the list is always displayed or whether the list is displayed in a drop-down[...]
to ComboBoxStyle.Simple, which
Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
will remove the original dropdown (long list) and only the filtered results remain.

How to get items from ComboBox to ListBox

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.

Colorcoding based on ComboBox item

I'm using c# and Visual Studio 2010. I have a tablelayoutpanel with 8 rows and 8 columns. In one column the user will pick an item from a dropdown list in a combobox. Based on their choice, the row should assume a certain color. Any help would greatly be appreciated.
Add an event handler to your Combo Box.
public void MyComboBox_OnSelectedItemChanged(object sender, EventArgs e)
{
//change the color here
}
Make sure you add the event handler to the ComboBox properties.

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

Fill the whole row of listbox selected items in form controls

I have a text box, and I have written code on a textbox textchange event so that when I type any text, related text is retrieved from my database and shown in a listbox.
I want to be able to click on any name in the list box, and make the whole row related to that name show on form controls.
If I understand you well, you would like to copy selected text (from a selected row in listBox) to textBox. You can easily do like it:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
Mitja

Categories