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);
}
}
Related
I made it so that when I double-click the item in the listbox, it will be sent to another listbox. But the problem is that it's arranging it where there are large spaces in between every item added. How do I make it so that the 2nd listbox (toSell) sorts the same as the original listbox (applianceList)?
Program design
private void applianceList_DoubleClick(object sender, EventArgs e)
{
string appliances = Convert.ToString(applianceList.SelectedItem);
toSell.Items.Add(appliances);
}
Example of how I want it to look
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.
I have two ListBoxes bound to DataTables. When I try to move information from one ListBox to another listbox using a button click with the following code:
protected void btnAdd_Click(object sender, EventArgs e){
ListBox1.Items.Add(ListBox2.SelectedItem);
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
One items is removed from ListBox2 and added to ListBox1 but if I try and click the button again with a newly selected item nothing happens.
I'd like to be able to move up to all the items from listbox 1 into listbox 2.
Keep two datatables, one for each list box. Move the items to their respective datatables and on your button click, rebind the listboxes to the datatables.
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
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');
}