Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to assign ItemData to combobox / listbox in VB.net

Total Hit ( 10128)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


There is no ItemData property in VB.net for ComboBox and ListBox but you can use another approach which will give you same functionality of ItemData.

Use following code to test how you can assign ItemData. ItemData is generally used to store Id related to the Item of list.

Click here to copy the following block
Class MyList
  Public MyId As Integer
  Public MyStr As String

  Public Sub New(ByVal s As String, ByVal i As Integer)
    MyStr = s
    MyId = i
  End Sub

  '//VB.net will decide DisplayMember using this function
  Public Overrides Function ToString() As String
    ToString = MyStr
  End Function
End Class

Sub LoadComboBox()
  ComboBox1.Items.Add(New MyList("VB", 1))
  ComboBox1.Items.Add(New MyList("VB.net", 2))
  ComboBox1.Items.Add(New MyList("C#", 3))
  ComboBox1.Items.Add(New MyList("ASP.net", 4))
  ComboBox1.Items.Add(New MyList("ASP", 5))
  ComboBox1.Items.Add(New MyList("SQL", 6))

  ComboBox1.SelectedIndex = 0
End Sub

Sub ShowSelected()
  MsgBox ("Text=" & ComboBox1.SelectedItem.Mystr & " ItemData=" & CStr(ComboBox1.SelectedItem.MyId))
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  ShowSelected()
End Sub

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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.