2010-03-02 89 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);