Shiju S
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(objComboCo ntrol,0,12 34)
But it returns an error
"Entry in Items array was not of a type supported by Microsoft.VisualBasic.Comp atibility. VB6.SetIte mData."
i am missing something?
I made lots of calculations and codings using this itemdata values
please help
Thanks
Shiju
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(objComboCo
But it returns an error
"Entry in Items array was not of a type supported by Microsoft.VisualBasic.Comp
i am missing something?
I made lots of calculations and codings using this itemdata values
please help
Thanks
Shiju
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",get type(syste m.int32))
dt.columns.add("Display",g ettype(sys tem.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"
dim dt as new datatable
dt.columns.add("Value",get
dt.columns.add("Display",g
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"
You might also want to read this
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbup2056.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbup2056.asp
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
@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_SelectedIndexCha nged - 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.ICon tainer
'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.Combo Box
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.Combo Box
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'ComboBox1
'
Me.ComboBox1.DropDownStyle = System.Windows.Forms.Combo BoxStyle.D ropDownLis t
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.ComboBo x1)
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_SelectedIndexCha nged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexCha nged
Dim item As MyComboItem
item = CType(ComboBox1.Items(Comb oBox1.Sele ctedIndex) , 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
Form1_Load - use this code instead of SetItemData
ComboBox1_SelectedIndexCha
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.ICon
'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.Combo
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.Debugg
Me.ComboBox1 = New System.Windows.Forms.Combo
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'ComboBox1
'
Me.ComboBox1.DropDownStyle
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.ComboBo
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_SelectedIndexCha
Dim item As MyComboItem
item = CType(ComboBox1.Items(Comb
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))
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))
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 ???
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 ???
:-)