Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2254
  • Last Modified:

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
0
Shiju Sasidharan
Asked:
Shiju Sasidharan
  • 6
  • 2
  • 2
1 Solution
 
RonaldBiemansCommented:
you use the valuemember and displaymember properties of the combobox for that in .net

0
 
RonaldBiemansCommented:
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"




0
 
RonaldBiemansCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RonaldBiemansCommented:
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.
0
 
AlexFMCommented:
.NET ComboBox doesn't support item data. Instead of this, ComboBox allows to keep objects of any type (not only strings). You can create your own class which has ToString function, it will be used for combobox lines. This class can keep any other additional data which you want to keep using SetItemData.

comboBox1.Items.Add("Item 1")
combobox1.SetItemData(myData)

Change this to:

comboBox1.Items.Add(new MyItem("Item 1", myData));
0
 
Shiju SasidharanAssoc Project ManagerAuthor Commented:
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
0
 
AlexFMCommented:
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

0
 
RonaldBiemansCommented:
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))
0
 
Shiju SasidharanAssoc Project ManagerAuthor Commented:
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 ???
0
 
RonaldBiemansCommented:
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now