简体   繁体   中英

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;

通过检查Focused of Controls,您可以检查控件是否已经具有焦点但是我不知道你为每个列表框创建一个单选按钮是什么意思?!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM