PC Review


Reply
Thread Tools Rate Thread

combobox and listbox items

 
 
EricJ
Guest
Posts: n/a
 
      6th Apr 2004
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


 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      6th Apr 2004
"EricJ" <(E-Mail Removed)> schrieb
>
> 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

 
Reply With Quote
 
EricJ
Guest
Posts: n/a
 
      6th Apr 2004
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



"Armin Zingler" <(E-Mail Removed)> wrote in message
news:40729334$0$12507$(E-Mail Removed)...
> "EricJ" <(E-Mail Removed)> schrieb
> >
> > 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
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      6th Apr 2004
"EricJ" <(E-Mail Removed)> schrieb
> 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

 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      6th Apr 2004
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
//


 
Reply With Quote
 
EricJ
Guest
Posts: n/a
 
      6th Apr 2004
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



"Cor" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> //
>
>



 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      6th Apr 2004
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


 
Reply With Quote
 
EricJ
Guest
Posts: n/a
 
      6th Apr 2004
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


"Cor" <(E-Mail Removed)> wrote in message
news:#iGOXC#(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Charlie Smith
Guest
Posts: n/a
 
      6th Apr 2004
"EricJ" <(E-Mail Removed)> wrote in message news:<40726308$0$1347$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
EricJ
Guest
Posts: n/a
 
      8th Apr 2004
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

"Charlie Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "EricJ" <(E-Mail Removed)> wrote in message

news:<40726308$0$1347$(E-Mail Removed)>...
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delselect items in databound ListBox or ComboBox andersonwebdev@hotmail.com Microsoft Dot NET Framework Forms 1 12th Aug 2005 03:11 PM
How to detect when items are added to Combobox/Listbox Don Microsoft VB .NET 9 13th Apr 2005 08:45 PM
ToolTip for ListBox-/ComboBox-Items Timo Kunze Microsoft VB .NET 11 4th Aug 2004 01:57 PM
newby. Associating data with listbox or combobox items Claire Microsoft C# .NET 1 21st May 2004 11:26 AM
Bug: Combobox.dropdown = True during Form.New() causes listbox to be disabled, even though combobox is Enabled. =?Utf-8?B?UmF0a2lsZXk=?= Microsoft Dot NET Framework Forms 1 12th Feb 2004 09:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:39 AM.