2010-03-02 96 views
0

当用户在列表框中选择一个名称并单击按钮时,我将该名称保存在名为“parent”的var中。将Listitem移动到列表框顶部

我想要做的是以编程方式将选定的名称移动到列表顶部,并将整个列表放到下拉列表中。我开始下面的代码,但不知道如何将选定的列表项(父)移动到列表顶部?

Private Sub GoLower(ByVal parent As String, 
     ByVal lst As ListBox, 
     ByVal ddl As DropDownList) 

     ddl.Items.Clear() 

     For Each item As ListItem In lst.Items 
      ddl.Items.Add(item.Text) 
      'MOVE the item that is parent to top of ddl???? 
     Next 

End Sub 

回答

1

以任意顺序构造列表框,然后将想要的那个移动到顶部。 这是用C#编写的,但VB.net相当于应该是明显的:

ListItem item = list.SelectedItem; 
// or find the required item another way, such as .FindByValue 
list.Items.Remove(item); 
list.Items.InsertAt(0, item); 
相关问题