Home | Index | Dotnet4all Snippets | Submit resources
About | Mail us 





ListBox non-Dataset load Value and discription (Tuesday, January 03, 2006)



Found the following interesting discussion in the Newsgroups:

ListBox non-Dataset load Value and discription
by:Anonymous

Is there a way to load into a listbox..
one click at a time...
one row at a time...
both a value & display text without using a bound Dataset?
ValueID = 123
DisText = "Bob"
Private Sub btnSplitAdd_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnSplitAdd.Click

lbSplitBrkFee.BeginUpdate()
lbSplitBrkFee.Items.Add(ValueID, DisText)
lbSplitBrkFee.EndUpdate()

End Sub

User will only see "Bob", but when they click on the listbox I can get access to the ValueID 123

Thanks
Brian


 Reply:
by:Greg Burns

 cboT1_PayRate.BeginUpdate()
cboT1_PayRate.Items.Clear()

cboT1_PayRate.Items.Add(New ListBoxItem("AN", "Annually"))
cboT1_PayRate.Items.Add(New ListBoxItem("HR", "Hourly"))
cboT1_PayRate.Items.Add(New ListBoxItem("WK", "Weekly"))
cboT1_PayRate.EndUpdate()

....
Public Class ListBoxItem

Private listItemData As Object ' make this whatever datatype most
appropriate for you
Private listItemText As String

' This is what is displayed in the ComboBox drop-down
Public Overrides Function ToString() As String
Return listItemText
End Function

Public Sub New(ByVal itemData As Object, ByVal itemText As String)

listItemData = itemData
listItemText = itemText
End Sub

Public ReadOnly Property Data() As Object
Get
Data = listItemData
End Get

End Property

Public ReadOnly Property Text() As String
Get
Text = listItemText
End Get

End Property

End Class
HTH,
Greg


 Reply:
by:Greg Burns

 forgot to mention; to get value back out, just cast SelectedItem back to
custom ListBoxItem class

Dim PayRateCode as String
PayRateCode=CType(cboT1_PayRate.SelectedItem, ListBoxItem).Data.ToString

news:O$mT6yKVEHA.3024@TK2MSFTNGP09.phx.gbl...
_


 Reply:
by:Anonymous

 Thank you... Workes Great!..

Still don't understand why this is not just part of the LB control.

Oh well it is MS VB.Net ;-D



Posted by Xander Zelders



0 Comments:

Post a Comment

<< Home

 
Previous Posts
    - Knew how to do it the old way, but ...
    - InvalidActiveXStateException error when editing si...
    - when to use DoEvents in vb.net?
    - Playing Audio from a Filestream using MCI
    - XML in a combobox
    - Setup Project
    - WMI Com Interop exception handling
    - Problem With Mailto Start Process
    - Comboboxes in windows forms datagrid
    - Sometimes I want to skip a page - How to do that



Disclaimer & Terms of Use | DotNet4All.Com concept & © 2004 - 2007 by Zelders² - Holland