Adding an item

LaTTicE

Member
Joined
Jun 14, 2004
Messages
11
Programming Experience
1-3
I need to add an item to my combo box in VB.NET. When writing the same program, but in ASP.NET, I was able to do it like this...

VB.NET:
 [size=2]
cboPart.Items.Add([/size][size=2][color=#0000ff]New[/color][/size][size=2] ListItem("All", "-1"))

[/size]

How would I go about adding The text "All", with a value of "-1" in a solely VB.NET application?
 
this one?
VB.NET:
 Dim s() As String = {"mzim ,-1"}
 Dim ss As String
 For Each ss In s
 	 ComboBox1.Items.Add(ss)
 Next

hope this helps.
 
The WebForm dropDownList is a bit different than the WinForm ComboBox.
In order to have a value associated with a comboBox Item (as far as I know), you have to create a dataTable that contains two columns and set the DataSource, DisplayMember, and ValueMember properties of the comboBox.

Here is a subClassed ComboBox control that somewhat simulates the function of the WebForm dropDownList:

VB.NET:
Public Class myComboBox
    Inherits System.Windows.Forms.ComboBox

    Private dt As New DataTable()

    Public Sub New()
        MyBase.New()
        dt.Columns.Add("Display", GetType(String))
        dt.Columns.Add("Value", GetType(Integer))
        Me.DataSource = dt
        Me.DisplayMember = "Display"
        Me.ValueMember = "Value"
    End Sub

    Public Sub AddItem(ByVal strDisplay As String, ByVal iValue As Integer)
        Dim dr As DataRow = dt.NewRow
        dr("Display") = strDisplay
        dr("Value") = iValue
        dt.Rows.Add(dr)
    End Sub

End Class
 
Back
Top