combobox and listbox items

  • Thread starter Thread starter EricJ
  • Start date Start date
E

EricJ

Hi all,

If you want to have your own class in a combo you override the tostring
function to display the text.(got that one here 2 ;p)
But i don't seem to find how to get the value, w function do i have to
override to work w the values ?
int = cbo.selectedValue (this sort of thing)

Tnx

Eric
 
EricJ said:
If you want to have your own class in a combo you override the
tostring function to display the text.(got that one here 2 ;p)
But i don't seem to find how to get the value, w function do i have
to override to work w the values ?
int = cbo.selectedValue (this sort of thing)

My intellisense offers other Selected* items. ;) Among them, there is
SelectedItem. Memberinfo for SelectedValue also shows that it returns the
value of the member set by the ValueMember property - uses reflection so
instead I'd use
directcast(cbo.selecteditem, yourclass).property


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
thats w im doing now but it has 1 problem, you can't set the cbo to a value.
You always need the text to set a cbo. It should be possible to have a value
2.
In asp.net there is a listitem class system.web.UI.WebControls.listitem w
this class its possible, so it should be possible to have this in your own
class 2.
I know i can work around it but i am revising my custom classes we use here
and want to include a value. Probably some more things in the future but im
still experimenting w these things.

Its not an urgent matter, yust bugging me ;)

Tnx for having a look at it.

eric
 
EricJ said:
thats w im doing now

"thats"? If you'd reply under the text you're quoting and you're referring
to, it would be much easier for me to read. (sorry for mentioning the words
"quoting style" in this group, sorry, sorry... ;)
but it has 1 problem, you can't set the cbo to a
value.

I don't understand: What is the value of a combo? It displays selectable
items and in the text portion you can edit text.
You always need the text to set a cbo. It should be possible
to have a value 2.

A 2 column textbox at the top of the combo (1=string, 2=value)?
In asp.net there is a listitem class
system.web.UI.WebControls.listitem w this class its possible, so it
should be possible to have this in your own class 2.

Sorry, I don't know the web stuff.
I know i can work around it but i am revising my custom classes we
use here and want to include a value. Probably some more things in
the future but im still experimenting w these things.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hi Eric,

I had the stupid idea that you where looking for this.

Cor
\\\
Private WithEvents combobox1 As New EComboBox
Private strt As Boolean
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim Belgen As New BelgenCollection
Belgen.Add(New Belg(New String() {"Lambiek", "Man", "Bier"}))
Belgen.Add(New Belg(New String() {"Sidonia", "Vrouw",
"Sinasappelsap"}))
Belgen.Add(New Belg(New String() {"Suske", "Jongen", "Cola"}))
Me.Controls.Add(combobox1)
combobox1.DataSource = Belgen
strt = True
End Sub
Private Sub combobox1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles combobox1.SelectedIndexChanged
If strt = True Then
MessageBox.Show(combobox1.BelgDrankSelectedValue)
End If
End Sub
End Class
Public Class Belg
Public Naam As String
Public sex As String
Public drank As String
Public Sub New(ByVal fill As String())
Naam = fill(0)
sex = fill(1)
drank = fill(2)
End Sub
Public Overrides Function tostring() As String
Return Naam
End Function
End Class
Public Class BelgenCollection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal aBelg As Belg)
List.Add(aBelg)
End Sub
End Class
///
\\\
Public Class EComboBox
Inherits System.Windows.Forms.ComboBox
#Region " Windows Form Designer generated code "
Public ReadOnly Property BelgDrankSelectedValue() As String
Get
Return DirectCast(Me.SelectedValue, Belg).drank
End Get
End Property
End Class
//
 
hi Cor :)

not really :/
you bind your combo to the belgenCollection and it should work for a normal
combobox (atm anyway and thats not up to me :/)

I'm trying to get the behaviour of the webCombo w listitem (and have more
options in the listitem class)

I can't let go of the fealing it should be something simple to override some
readonly function that lets the combo know w the value is.

but i'm going to have a look at that collection, seems interesting for other
things :)

tnx

eric
 
Hi Eric,

It did intrest me and because there was someone who has copied your
procedure when there was a problem with the -1 (and I told that you had made
a class for it and I think he has searched for it in the newsgroups).

However when I did look for it, I saw that the selectedvalue from the web
list is overriddable while it is from the windowsform not. Therefore I came
with this.

:-)

And it is so nice, you should really try it.

Cor
 
looks nice and there are some things in it that will surface soon ;)
but i want to (have to) stay away from the cbo.datasource (should work
unbound)
i think i could try something w the custom combobox control if it's not over
my head. But that is not for now. the people here have to work with w i come
up w, they wouldn't be to happy w that ;p. It's 1 thing to change a class,
changing all the combo's in your app is another.

It is however strange that the web combo has better functionallity than the
win combo.

eric
 
EricJ said:
Hi all,

If you want to have your own class in a combo you override the tostring

Eric,

I use a small class like this:

Public Class ComboBoxItem
Private mID As Integer = -1
Private mText As String = ""

Public Sub New(ByVal id As Integer, ByVal text As String)

mID = id
mText = text

End Sub

Public ReadOnly Property ID() As Integer
Get
Return mID
End Get
End Property

Public ReadOnly Property Text() As String
Get
Return mText
End Get
End Property

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

End Class

As many properties as needed can be included and then read out the
property you want after the selection like this:

Dim objItem As ComboBoxItem = DirectCast(ComboBox1.SelectedItem,
ComboBoxItem)

HTH,

Charlie
 
tnx for the reply but i already have that (been using it since september)

tostring will give you text in the cbo items but not the value that should
be accessible in cbo.selectedvalue.

w the various posts in this thread i'm getting convinced, the problem is
related to the combobox itself and i will have to create my own cbo to get
this functionallity.

example of w i want to do w unbound combo's

cbo.selectedvalue = id 'sets the cbo to the right item

varID = cbo.selectedvalue ' this instaid of the cast i now use

eric
 
"thats"? If you'd reply under the text you're quoting and you're referring
to, it would be much easier for me to read. (sorry for mentioning the words
"quoting style" in this group, sorry, sorry... ;)

Sorry Armin you are completely right. I'll try to keep it in mind.
Sorry, I don't know the web stuff.

example of w i want to do w unbound combo's and this is possible in asp.net

cbo.selectedvalue = id 'sets the cbo to the right item not w the text (text
is more sensitive)
varID = cbo.selectedvalue ' this instaid of the cast i now use

But it seems that it is a shortcomming of the combobox instead of a function
i need in my class :/

I'll have a look at it later, have to much work atm :/
OT :Finishing a web project, analyzing the data need for a new win project
and helping 2 coleages and 2 students that are doing their internship here
:p

eric
 
Back
Top