Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

vb.net - Combo Box items - Display Member for List(Of String)?

My project is in Visual Basic. I am trying to create a custom & savable "filter" for a DataGridView using several TextBoxes. Right now, any List(Of String) that is added to the Combo Box is displayed in the box as (Collection). I want my users to be able to select the one they created, so I would like the Lists to have a display name that can be selected in the Combo Box. Here is some of the code.

Dim savedFilter As New List(Of String)
savedFilter.Add(NameTextBox.Text)
savedFilter.Add(AgeTextBox.Text)
savedFilter.Add(NotesTextBox.Text)
ComboBoxSavedFilters.Items.Add(savedFilter)

Is it possible to add a display name for a List?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Or if you are lazy use buid-in generic class Tuple From MSDN.

Create collection of Tuple(Of String, List(Of String)) and use approach suggested by @Plutonix for binding collection to ComboBox

Dim savedFilter As New List(Of Tuple(Of String, List(Of String)))()
savedFilter.Add(
    Tuple.Create("default",
                 New List From {"filter1", "filter2", "filter3"}))
savedFilter.Add(
    Tuple.Create("Blue ones", 
                 New List From {"filter4", "filter5"}))
savedFilter.Add(
    Tuple.Create("Old ones", 
                 New List From {NameTextBox.Text, AgeTextBox.Text, NotesTextBox.Text}))

With ComboBoxSavedFilters
    .DisplayMember = "Item1" 'Name of first property in Tuple type
    .ValueMember = "Item2" 'Name of second property in Tuple type -List 
    .DataSource = savedFilter       
End With

Then SelectedValue will contain currently selected filter's collection, which can be accessed like that

Dim filter As List(Of String) =
    DirectCast(Me.ComboBoxSavedFilters.SelectedValue, List(Of String))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...