how to add item to a selected ListBox - c#

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

Related

How do I make a listbox sort items the same way as another listbox?

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

C# show property in textbox depending on combobox selecteditem

I have maybe an easy question but I would like to ask about possibilities how to bind textbox Text property to combobox SelectedItem property. I do it through combobox SelectedItemChanged event and set text like this:
if(cmbMeasuring.SelectedItem != null)
txtMethod.Text = ((ListBoxItem)cmbMeasuring.SelectedItem).Value;
I have class ListBoxItem which holds 2 strings "Name" and "Value". Then I created BindingList for combobox:
private BindingList<ListBoxItem> lst;
and then set combobox data source in constructor:
cmbMeasuring.DataSource = lst;
cmbMeasuring.DisplayMember = "Name";
This works fine but I dont know if its the best way how to do it. But problem occurs when I change the textbox content. I do it through textbox Leave event:
private void txtMethod_Leave(object sender, EventArgs e)
{
if (cmbMeasuring.SelectedItem != null)
((ListBoxItem)cmbMeasuring.SelectedItem).Value = txtMethod.Text;
}
If textbox lost focus I assign item value. But I have also a menustrip to save input and when I click to it directly this event dont occur so the last input is not saved. I know that this could be done through textbox TextChanged event but it consume a lot of time.
Do you have any better solutions or is it OK? Im not using WPF.
Thanks.
If you have a Click event for the MenuStrip item, you can do the following
MyMenuStripItem.Focus();
This should cause the MenuStrip item to gain focus and therefore causing the TextBox to lose focus.
Try data binding on the TextBox in your form's constructor:
txtMethod.DataBindings.Add("Text", lst, "Value",
false, DataSourceUpdateMode.OnPropertyChanged);

accessing dynamically created control variables from event handler c#

I have a program that I am trying to write that
one row of the comboboxes and text boxes are created dynamically when you click the plus button that is on the form
and then you can keep clicking the plus and it keeps adding a row but moves the new row down
I have the row organized like this:
there is a comboBox for buildings, then a comboBox for rooms. And that is one row
The building comboboxes have lists of buildings in them, and when I choose an item from the list, I need to populate the room combobox with the list of room numbers that correspond with the building.
I am having an extremely hard time doing this.
This is what Ive tried.
my row class is what I use to create the new rows.
When I press the button, a new row is created with new comboboxes etc.
Here is my event handler
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if (comboBox.SelectedItem.Equals("ATLC"))
{
foreach (int x in row.ATLC)
{
row.roomComboBox.Items.Add(x);
}
}
Now my problem is I need to somehow add the corresponding room number data to the roomComboBox associated with the current row, and I dont know how and its driving me nuts.
The sender comboBox is associated with the current row, so is there a way to use the sender to reference the roomComboBox that is a member of the same row??
any help would be great. Ive searched lots of threads on here and cant seem to find an answer.
edit:
Is there a way to reference the object that a variable belongs to.
If I could somehow reference the row object of the sender comboBox then i might could use that to reference the room comboBox of the same row object...maybe? somehow? please?
When you create your comboboxes for each row, you can set the Building combobox's Tag property to the Room Numbers combobox. That will let you retrieve the associated combobox for that row. i.e...
// create combobox for row N...
ComboBox cmbBuilding = new ComboBox();
ComboBox cmbRooms = new ComboBox();
// store rooms combobox in building combobox
cmbBuilding.Tag = cmbRooms;
// ...
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
// get the room combobox
ComboBox cmbRooms = comboBox.Tag as ComboBox;
if (comboBox.SelectedItem.Equals("ATLC"))
{
foreach (int x in row.ATLC)
{
cmbRooms.Items.Add(x);
}
}
Hope this little snippet will help
(sender as ComboBox).Parent.Controls.Find("cmbRooms",true)
This code will get the control with id "cmbRooms" with reference to the ComboBox sender
Use a DataRepeater Control for Windows Forms. It is included in the Visual Basic Power Packs but can be used in C# projects as well.
Another option is to use a DataGridView.

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

C# WPF - ComboBox

I'm working on a custom control that internally uses a ComboBox.
My problem is when the ComboBox is focused and has the drop-down open, it appears to focus the entire control. I would like to automatically highlight the first item in the drop drown, but right now you have to push the Down key to do so.
Is there a way to programmatically Highlight the first item in a ComboBox (set the readonly IsHighlighted property to true)? I believe the concept of IsHighlight within a ComboBox is different than Focus. Also, I am binding via ItemsSource, so I have no reference to ComboBoxItems.
Here's a way of doing it, although it might not cover all the cases - but you didn't provide too many details (for example, what happens when there is already an element selected? Do you still want to select the first element in the list? The code below will highlight the first element only when there is no selection in the combobox. To make it always select the first element, the DropDownOpened event should be handled too).
public MainWindow()
{
InitializeComponent();
combobox.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
}
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (combobox.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
{
(combobox.ItemContainerGenerator.ContainerFromIndex(0) as ComboBoxItem).Focus();
}
}
(Hope I understood correctly and this is what you want to do).
It might not be what you are looking for but if you set mycombo.SelectedIndex = 0 then mycombo.IsDropDownOpen = True it should open it up and have the first item selected. It will be highlighted but will also be the value in the combobox as well. I'm not sure if this is not the desired effect though..

Categories