简体   繁体   English

如何将项添加到选定的ListBox

[英]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. 对于每个创建的Listbox,我给它一个不同的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. 对于每个Listbox,我正在尝试创建Radiobutton,但我不知道如何将它与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. 这个想法是这样的:对于每个列表框设置Enter事件到lb_Enter() ,所以你总是在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: 您需要一种方法来选择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. 您可以在获得焦点时将ID存储在父级上。 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 此致,波格丹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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