user1051723
user1051723

Reputation: 25

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.

Upvotes: 0

Views: 404

Answers (4)

Bogdan M.
Bogdan M.

Reputation: 1128

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

Upvotes: 0

Emond
Emond

Reputation: 50672

You need a way to select a ListBox:

  1. Use drag and drop (the drop shows what listbox is selected)
  2. Use a radio button or something similar to mark a listbox as the target
  3. 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.

Upvotes: 0

DeveloperX
DeveloperX

Reputation: 4683

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?!

Upvotes: 1

Marco
Marco

Reputation: 57573

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;

Upvotes: 1

Related Questions