Link to home
Start Free TrialLog in
Avatar of Shiju S
Shiju SFlag for United States of America

asked on

VB6.SetItemData in ComboBox

Hi Experts
i upgraded VB6 project to VB.Net. i Kept some unique id's as ItemData in Combo boxes for each entry in them.
and in VB.net i tried

   VB6.SetItemData(objComboControl,0,1234)

But it returns an error

"Entry in Items array was not of a type supported by Microsoft.VisualBasic.Compatibility.VB6.SetItemData."

i am missing something?
I made lots of calculations and codings using this itemdata values
please help

Thanks
Shiju
Avatar of RonaldBiemans
RonaldBiemans

you use the valuemember and displaymember properties of the combobox for that in .net

so something like

dim dt as new datatable
dt.columns.add("Value",gettype(system.int32))
dt.columns.add("Display",gettype(system.int32))

dim dr as datarow
dr = dt.newrow
dr.item("Value") = 1234
dr.item("Display") = 0
dt.rows.add(dr)

combobox1.datasource = DT
combobox1.displaymember = "Display"
combobox1.valuemember = "Value"




On a side node, never upgrade a vb6 application with the converter in .net. It produces inefficient and unreadable code. Write the code from scratch in .net. It will be performing a lot better.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shiju S

ASKER

Thank u all for quck reply
@RonaldBiemans
   i need to keep entries for each item in combo
@AlexFM
 ur idea sounds good.
 can u please provide some sample code ..

;-)
Shiju
MyComboItem is class used as combobox item. It's s member is used as combobox line text. It's data member keeps some additional data (Integer in this sample, but may be any other data).
Form1_Load - use this code instead of SetItemData
ComboBox1_SelectedIndexChanged - use this code instead of GetItemData

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'ComboBox1
        '
        Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ComboBox1.Location = New System.Drawing.Point(12, 32)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(128, 21)
        Me.ComboBox1.TabIndex = 0
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(12, 128)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(160, 52)
        Me.Label1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.ComboBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ComboBox1.Items.Add(New MyComboItem("Item 1", 10))
        ComboBox1.Items.Add(New MyComboItem("Item 2", 20))
        ComboBox1.Items.Add(New MyComboItem("Item 3", 30))
    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim item As MyComboItem

        item = CType(ComboBox1.Items(ComboBox1.SelectedIndex), MyComboItem)
        Label1.Text = item.data.ToString()
    End Sub
End Class

Public Class MyComboItem
    Public Sub New(ByVal s As String, ByVal data As Integer)
        Me.s = s
        Me.data = data
    End Sub

    Public s As String
    Public data As Integer

    Public Overrides Function ToString() As String
        Return s
    End Function

End Class

Hi Shijusn, Just add a row to the table for each item.

here is an example using AlexFM suggestion (the points should go to him)
Public Class ListItem
    Private m_Item As String
    Private m_Value As Integer

    Public Sub New(ByVal Item As String, ByVal Value As Integer)
        m_Item = name
        m_Value = id
    End Sub

    Property Value() As Integer
        Get
            Return m_Value
        End Get
        Set(ByVal Value As Integer)
            m_Value = Value
        End Set
    End Property

    Property Item() As String
        Get
            Return m_Item
        End Get
        Set(ByVal Value As String)
            m_Item = Value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return m_Item
    End Function
end Class

then use
comboBox1.Items.Add(new MyItem("Item 1", 1234))
Avatar of Shiju S

ASKER

Thank u AlexFM
      That did the job

@RonaldBiemans
           Ur example was simple, thank u
                >>here is an example using AlexFM suggestion (the points should go to him)
                u blocked me from assisting ur answer ???