본문 바로가기

Programming

콤보박스 목록구성 방법

 

‘//배열을 이용한 방법

dim myColors() as String = {"AQUA","BLACK","BLUE","GREEN","RED","WHITE","YELLOW"}

combobox1.DataSource=myColors


‘//배열리스트를 이용한 방법

dim myShapes as new ArrayList

with myShapes

.Add("CIRCLE")

.Add("OCTAGON")

.Add("RECTANGLE")

.Add("SQUARE")

.Add("TRAPEZOID")

.Add("TRIANGLE")

End With

combobox1.DataSource=myShapes


‘//데이터테이블을 이용한 방법

Dim Dt As New DataTable

Dt.Columns.Add("getName")

Dt.Columns.Add("getId")

Dt.Rows.Add(New String() {"사과", "1"})

Dt.Rows.Add(New String() {"배", "2"})

With ComboBox1

    .DataSource = Dt

    .DisplayMember = "getName"

    .ValueMember = "getId"

End With



‘//구조체 + 배열리스트 를 이용한 방법

Protected Structure Divisions

 Private divName As String

 Private divId As Integer

 Public Sub New(ByVal name As String, ByVal id As Integer)

  divName = name

  divId = id

 End Sub

 Public ReadOnly Property getName() As String

         Get

                 Return divName

  End Get

 End Property

 Public ReadOnly Property getId() As Integer

         Get

                 Return divID

  End Get

 End Property

End Structure

---- 실제사용코드

Dim myDivisions As New ArrayList

With myDivisions

 .Add(New Divisions("CENTRAL", 1))

 .Add(New Divisions("EAST", 2))

 .Add(New Divisions("NORTH", 3))

 .Add(New Divisions("SOUTH", 4))

End With


        'bind the arraylist to the combo box

With ComboBox1

 .DataSource = myDivisions

 .DisplayMember = "getName"

 .ValueMember = "getId"

End With