473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Can I make a combobox that is stronly typed?

I'm using a ComboBox to display objects of a class I've defined, say CQQ.

Works great except somehow I occasionally set an Item to a String object
instead of an object of type CQQ.

It looks like Text replaces an item or something like that.

This results in a runtime error at which time I learn that the item that
should be CQQ is a String.

I can't find out where or how the setting happens.

It occurs to me that if the comboBox defined it's Items as objects of type
CQQ instead of Object it would the usage would be less error prone and more
likely to be bug free.

Is there some way I could generate a strongly typed combobox?

I know I could simply inherit a combobox and try to override anything the
changes an Item but that leaves the possibility that I'd miss some method
and not really have what I want. I'd like to just change the Items
collection once.

Is that possible?
Thanks

Feb 18 '07 #1
19 2179
Why not create a list of CQQ and then bind the combobox to it. Then to add
anything to the combobox, you'll have to add it to the list, which will
blow up if you try to add an object other than a CQQ to it. Off the top of
my head, something like this:

Dim myList As List(Of CQQ)

'add entries to the list
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))

myComboBox.DataSource = myList
myComboBox.DisplayMember = myList.field2
myComboBox.ValueMember = myList.field1

Robin S.
------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uW*************@TK2MSFTNGP02.phx.gbl...
I'm using a ComboBox to display objects of a class I've defined, say CQQ.

Works great except somehow I occasionally set an Item to a String object
instead of an object of type CQQ.

It looks like Text replaces an item or something like that.

This results in a runtime error at which time I learn that the item that
should be CQQ is a String.

I can't find out where or how the setting happens.

It occurs to me that if the comboBox defined it's Items as objects of
type CQQ instead of Object it would the usage would be less error prone
and more likely to be bug free.

Is there some way I could generate a strongly typed combobox?

I know I could simply inherit a combobox and try to override anything the
changes an Item but that leaves the possibility that I'd miss some method
and not really have what I want. I'd like to just change the Items
collection once.

Is that possible?
Thanks

Feb 19 '07 #2
Because I know nothing about using a datasource.

I'll look into what you've given below and see if I can learn enough to use
it.

There is also SetItemCore and SetItemsCore that I have to look into.
Thanks a lot

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:3d******************************@comcast.com. ..
Why not create a list of CQQ and then bind the combobox to it. Then to add
anything to the combobox, you'll have to add it to the list, which will
blow up if you try to add an object other than a CQQ to it. Off the top of
my head, something like this:

Dim myList As List(Of CQQ)

'add entries to the list
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))

myComboBox.DataSource = myList
myComboBox.DisplayMember = myList.field2
myComboBox.ValueMember = myList.field1

Robin S.
------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uW*************@TK2MSFTNGP02.phx.gbl...
>I'm using a ComboBox to display objects of a class I've defined, say CQQ.

Works great except somehow I occasionally set an Item to a String object
instead of an object of type CQQ.

It looks like Text replaces an item or something like that.

This results in a runtime error at which time I learn that the item that
should be CQQ is a String.

I can't find out where or how the setting happens.

It occurs to me that if the comboBox defined it's Items as objects of
type CQQ instead of Object it would the usage would be less error prone
and more likely to be bug free.

Is there some way I could generate a strongly typed combobox?

I know I could simply inherit a combobox and try to override anything the
changes an Item but that leaves the possibility that I'd miss some method
and not really have what I want. I'd like to just change the Items
collection once.

Is that possible?
Thanks


Feb 19 '07 #3
I've used what you suggested almost with no changes.

Below is what I've tried and it appears to be storing data but not
displaying it.

I've tried everything I can think of for the DisplayMember!

StringWithInteger has two properties: Str and Value

Public myList As List(Of StringWithInteger) = New List(Of StringWithInteger)

and

myList.Add(itemValue) 'itemValue is type StringWithInteger

and

Public Sub New()

MyBase.New()

InitializeComponent()

Me.DataSource = myList

Me.DisplayMember = myList.field2

Me.ValueMember = myList.field1

End Sub



"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:3d******************************@comcast.com. ..
Why not create a list of CQQ and then bind the combobox to it. Then to add
anything to the combobox, you'll have to add it to the list, which will
blow up if you try to add an object other than a CQQ to it. Off the top of
my head, something like this:

Dim myList As List(Of CQQ)

'add entries to the list
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))

myComboBox.DataSource = myList
myComboBox.DisplayMember = myList.field2
myComboBox.ValueMember = myList.field1

Robin S.
------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uW*************@TK2MSFTNGP02.phx.gbl...
>I'm using a ComboBox to display objects of a class I've defined, say CQQ.

Works great except somehow I occasionally set an Item to a String object
instead of an object of type CQQ.

It looks like Text replaces an item or something like that.

This results in a runtime error at which time I learn that the item that
should be CQQ is a String.

I can't find out where or how the setting happens.

It occurs to me that if the comboBox defined it's Items as objects of
type CQQ instead of Object it would the usage would be less error prone
and more likely to be bug free.

Is there some way I could generate a strongly typed combobox?

I know I could simply inherit a combobox and try to override anything the
changes an Item but that leaves the possibility that I'd miss some method
and not really have what I want. I'd like to just change the Items
collection once.

Is that possible?
Thanks


Feb 19 '07 #4
This is the attempt that seems like it should work but I get nothing in the
drop down box even though the debugger shows myList to have 187 items
StringWithInteger has two properties: Str and Value

Public myList As List(Of StringWithInteger) = New List(Of
StringWithInteger)

and

myList.Add(itemValue) 'itemValue is type StringWithInteger

and

Public Sub New()

MyBase.New()

InitializeComponent()
MyBase.DataSource = myList

MyBase.DisplayMember = "Str"

MyBase.ValueMember = "Value"

End Sub


Feb 19 '07 #5
Active,

If you know nothing from a datasource, why do you than try to use it direct
on your own usercontrol.

Can you not first try it as Robin show you on a normal combobox and then
start to implement that on your own usercombobox?

Just my thought,

Cor

" active" <ac**********@a-znet.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
This is the attempt that seems like it should work but I get nothing in
the drop down box even though the debugger shows myList to have 187 items
>StringWithInteger has two properties: Str and Value

Public myList As List(Of StringWithInteger) = New List(Of
StringWithInteger)

and

myList.Add(itemValue) 'itemValue is type StringWithInteger

and

Public Sub New()

MyBase.New()

InitializeComponent()
MyBase.DataSource = myList

MyBase.DisplayMember = "Str"

MyBase.ValueMember = "Value"

>End Sub



Feb 19 '07 #6
Can you show your class definition for StringWithInteger?

Robin S.
-------------------------------
" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
This is the attempt that seems like it should work but I get nothing in
the drop down box even though the debugger shows myList to have 187
items
>StringWithInteger has two properties: Str and Value

Public myList As List(Of StringWithInteger) = New List(Of
StringWithInteger)

and

myList.Add(itemValue) 'itemValue is type StringWithInteger

and

Public Sub New()

MyBase.New()

InitializeComponent()
MyBase.DataSource = myList

MyBase.DisplayMember = "Str"

MyBase.ValueMember = "Value"

>End Sub



Feb 19 '07 #7

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:eP**************@TK2MSFTNGP06.phx.gbl...
Active,

If you know nothing from a datasource, why do you than try to use it
direct on your own usercontrol.

Can you not first try it as Robin show you on a normal combobox and then
start to implement that on your own usercombobox?
I did just that. In fact I gave the combobox properties so the form can
supply the List and set the DisplayMember and the ValueMember so the
combobox is somewhat general purpose.

However I don't know how to "unbind" the list and then rebind it.
I need to do that because changing the list after it is bound does not
change the boxes display.
That is, how do I undo .DataSource = myList

Thanks


thanks
>
Just my thought,

Cor

" active" <ac**********@a-znet.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>This is the attempt that seems like it should work but I get nothing in
the drop down box even though the debugger shows myList to have 187
items
>>StringWithInteger has two properties: Str and Value

Public myList As List(Of StringWithInteger) = New List(Of
StringWithInteger)

and

myList.Add(itemValue) 'itemValue is type StringWithInteger

and

Public Sub New()

MyBase.New()

InitializeComponent()
MyBase.DataSource = myList

MyBase.DisplayMember = "Str"

MyBase.ValueMember = "Value"

>>End Sub




Feb 19 '07 #8
On Feb 19, 3:57 pm, " active" <activeNOS...@a-znet.comwrote:
"Cor Ligthert [MVP]" <notmyfirstn...@planet.nlwrote in messagenews:eP**************@TK2MSFTNGP06.phx.gbl. ..
Active,
If you know nothing from a datasource, why do you than try to use it
direct on your own usercontrol.
Can you not first try it as Robin show you on a normal combobox and then
start to implement that on your own usercombobox?

I did just that. In fact I gave the combobox properties so the form can
supply the List and set the DisplayMember and the ValueMember so the
combobox is somewhat general purpose.

However I don't know how to "unbind" the list and then rebind it.
I need to do that because changing the list after it is bound does not
change the boxes display.
That is, how do I undo .DataSource = myList

Thanks

thanks
Just my thought,
Cor
" active" <activeNOS...@a-znet.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
This is the attempt that seems like it should work but I get nothing in
the drop down box even though the debugger shows myList to have 187
items
>StringWithInteger has two properties: Str and Value
>Public myList As List(Of StringWithInteger) = New List(Of
StringWithInteger)
>and
>myList.Add(itemValue) 'itemValue is type StringWithInteger
>and
>Public Sub New()
>MyBase.New()
>InitializeComponent()
MyBase.DataSource = myList
MyBase.DisplayMember = "Str"
MyBase.ValueMember = "Value"
>End Sub
Instead of using List(Of StringWithInteger), try using BindingList(Of
StringWithInteger)

Chris

Feb 19 '07 #9
I think I have a new problem now.
I don't know how to "unbind" the list and then rebind it.
I need to do that because changing the list after it is bound does not
change the box's display.
That is, how do I undo .DataSource = myList
so I can change myList and the rebind it?

Thanks
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:Ce******************************@comcast.com. ..
Can you show your class definition for StringWithInteger?

I think I'm past the earlier problem now (see above) but since you asked
I've added the class below. I removed some to make it easier to read.

Public Class StringWithInteger
Private mInteger As Integer
Private mString As String

Public Sub New(ByVal str As String, ByVal value As Integer)
mString = str
mInteger = value
End Sub
Public Overrides Function ToString() As String
ToString = mString
End Function
Public Property Str() As String
Get
Str = mString
End Get
Set(ByVal text As String)
mString = text
End Set
End Property
Public Property Value() As Integer
Get
Value = mInteger
End Get
Set(ByVal numb As Integer)
mInteger = numb
End Set
End Property
End Class


Feb 19 '07 #10
Chris

>>
However I don't know how to "unbind" the list and then rebind it.
I need to do that because changing the list after it is bound does not
change the boxes display.
That is, how do I undo .DataSource = myList

Thanks
Chris, Does your reply relate to the above question or to the entire
requirement?
My next problem will relate to sorting the box's displayed list.
Does that impact the usage of either List or BindingList

Thanks
>
Instead of using List(Of StringWithInteger), try using BindingList(Of
StringWithInteger)

Chris

Feb 19 '07 #11
I recommend you use a BindingSource component between the data source and
the combobox. You can drag one onto the form from the Toolbox, or create it
="on the fly".

Dim myBindingSource As BindingSource = New BindingSource()

myBindingSource.DataSource = myList

myComboBox.DataSource = myBindingSource
myComboBox.ValueMember = "whatever"
myComboBox.DisplayMember = "whatever"

Now when you change the list, the BindingSource will respond, and then
repaint the control. The BindingSource is sort of like lubrication between
the data source and the control. Putting it in the middle gives you a bunch
of methods and properties you can access, and better controls the bound
data and the display of it.

As Chris Dunaway recommended, you could define your list as a
BindingList(Of T), but you would still probably want to use a BindingSource
for the reason mentioned above.

Give this a try, and let me know how it works.

Robin S.
----------------------------
" active" <ac**********@a-znet.comwrote in message
news:ea*************@TK2MSFTNGP02.phx.gbl...
>
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:eP**************@TK2MSFTNGP06.phx.gbl...
>Active,

If you know nothing from a datasource, why do you than try to use it
direct on your own usercontrol.

Can you not first try it as Robin show you on a normal combobox and then
start to implement that on your own usercombobox?

I did just that. In fact I gave the combobox properties so the form can
supply the List and set the DisplayMember and the ValueMember so the
combobox is somewhat general purpose.

However I don't know how to "unbind" the list and then rebind it.
I need to do that because changing the list after it is bound does not
change the boxes display.
That is, how do I undo .DataSource = myList

Thanks


thanks
>>
Just my thought,

Cor

" active" <ac**********@a-znet.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>This is the attempt that seems like it should work but I get nothing in
the drop down box even though the debugger shows myList to have 187
items

StringWithInteger has two properties: Str and Value

Public myList As List(Of StringWithInteger) = New List(Of
StringWithInteger)

and

myList.Add(itemValue) 'itemValue is type StringWithInteger

and

Public Sub New()

MyBase.New()

InitializeComponent()
MyBase.DataSource = myList

MyBase.DisplayMember = "Str"

MyBase.ValueMember = "Value"
End Sub




Feb 20 '07 #12
To update the display, what I previously had to do was rebind the list each
time I changed the list.
That seemed to work OK.
I inserted the BindingSource as you recommended and no longer need to rebind
after changes.
Chris Dunaway didn't really recommend. He suggested I try BindingList(Of T).
I did and didn't notice any difference.
I tried reading about all the stuff you and Chris suggested - but my
background is lacking and it is pretty slow going.
So I really want to thanks you for all the help. Now at least I know what I
need to read until I understand it.
Strange to have it working and not understand what is going on.

The List class seems a little simpler so I'd prefer to stay with that unless
there is a strong reason to use BindingList - is there such a reason?

I've been looking at internet sites trying to find out how to sort the
ComboBox display. Can you easily point me to something that would help?

Thanks again for all the help!

PS
I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be made
through it. Should I be looking at doing that?

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:vo******************************@comcast.com. ..
>I recommend you use a BindingSource component between the data source and
the combobox. You can drag one onto the form from the Toolbox, or create it
="on the fly".

Dim myBindingSource As BindingSource = New BindingSource()

myBindingSource.DataSource = myList

myComboBox.DataSource = myBindingSource
myComboBox.ValueMember = "whatever"
myComboBox.DisplayMember = "whatever"

Now when you change the list, the BindingSource will respond, and then
repaint the control. The BindingSource is sort of like lubrication between
the data source and the control. Putting it in the middle gives you a
bunch of methods and properties you can access, and better controls the
bound data and the display of it.

As Chris Dunaway recommended, you could define your list as a
BindingList(Of T), but you would still probably want to use a
BindingSource for the reason mentioned above.

Give this a try, and let me know how it works.

Robin S.

Feb 20 '07 #13

I figured the BindingSource would fix your problem.

For binding to a combobox, I think a BindingList is overkill. I use a
BindingList when binding a list of my business objects to a DataGridView so
I can access the methods and properties of the bindinglist and override
them as needed. There's no reason for you to not use a List() if it works
for you.

If you are loading the combobox from a datatable, sort it when you pull it
from the database. "Select LastName, FIrstName From MyCustomerTable ORDER
BY Lastname, FirstName".

If that's not viable, the BindingSource has sort capabilities. (See what I
mean about it having methods and properties you can use? Hubba hubba.)

For example, this will sort the data in my CustomersBindingSource by
ContactName in Ascending order.

CustomersBindingSource.Sort = "ContactName ASC"

There's probably also some kind of sort property on the combobox, but I'm
too tired to look it up. ;-)
I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be made
through it. Should I be looking at doing that?
I think it's talking about when you want to change the data and save it
back to the data source, like when you are binding the data to a
DataGridView. You don't need to worry about that at this point.

I'm glad it's working. Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
To update the display, what I previously had to do was rebind the list
each time I changed the list.
That seemed to work OK.
I inserted the BindingSource as you recommended and no longer need to
rebind after changes.
Chris Dunaway didn't really recommend. He suggested I try BindingList(Of
T). I did and didn't notice any difference.
I tried reading about all the stuff you and Chris suggested - but my
background is lacking and it is pretty slow going.
So I really want to thanks you for all the help. Now at least I know what
I need to read until I understand it.
Strange to have it working and not understand what is going on.

The List class seems a little simpler so I'd prefer to stay with that
unless there is a strong reason to use BindingList - is there such a
reason?

I've been looking at internet sites trying to find out how to sort the
ComboBox display. Can you easily point me to something that would help?

Thanks again for all the help!

PS
I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be made
through it. Should I be looking at doing that?

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:vo******************************@comcast.com. ..
>>I recommend you use a BindingSource component between the data source and
the combobox. You can drag one onto the form from the Toolbox, or create
it ="on the fly".

Dim myBindingSource As BindingSource = New BindingSource()

myBindingSource.DataSource = myList

myComboBox.DataSource = myBindingSource
myComboBox.ValueMember = "whatever"
myComboBox.DisplayMember = "whatever"

Now when you change the list, the BindingSource will respond, and then
repaint the control. The BindingSource is sort of like lubrication
between the data source and the control. Putting it in the middle gives
you a bunch of methods and properties you can access, and better
controls the bound data and the display of it.

As Chris Dunaway recommended, you could define your list as a
BindingList(Of T), but you would still probably want to use a
BindingSource for the reason mentioned above.

Give this a try, and let me know how it works.

Robin S.


Feb 20 '07 #14
I have a feeling I'm taking advantage of you good nature, but I can't stop!

After I wrote last night, I found the SortedList generic class

Which works OK and produces a sorted display.

However, there is something I can't figure out:

I need to get the integer part of StringWithInteger (Value) of the selected
item

I got this far
= DirectCast(StringWithIntegerComboBox1.SelectedItem , SortedList(Of String,
StringWithInteger))

StringWithInteger has two properties Str and Value

I don't think I want to cast to SortedList but something like
SortedList.Item

That's my present problem.

Thanks again

I'll also try List with the sort method you pointed out but I would like to
know how to do the above.

a smaller problem is in understanding. I continue to use

StringWithIntegerComboBox1.ItemDisplayMember = "Str"

StringWithIntegerComboBox1.ItemValueMember = "Value"

With List there was only one object so I could see how that might work.

With SortedList there are two objects and I wonder how it finds the property

It appears to find it because I tried

StringWithIntegerComboBox1.ItemDisplayMember = "qqStr"

StringWithIntegerComboBox1.ItemValueMember = "qqValue"

and this produced an exception which makes me think that it was using the
original one - not ignoring them

Suppose both objects had a property "Value", then what?



"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:AI******************************@comcast.com. ..
>
I figured the BindingSource would fix your problem.

For binding to a combobox, I think a BindingList is overkill. I use a
BindingList when binding a list of my business objects to a DataGridView
so I can access the methods and properties of the bindinglist and override
them as needed. There's no reason for you to not use a List() if it works
for you.

If you are loading the combobox from a datatable, sort it when you pull it
from the database. "Select LastName, FIrstName From MyCustomerTable ORDER
BY Lastname, FirstName".

If that's not viable, the BindingSource has sort capabilities. (See what I
mean about it having methods and properties you can use? Hubba hubba.)

For example, this will sort the data in my CustomersBindingSource by
ContactName in Ascending order.

CustomersBindingSource.Sort = "ContactName ASC"

There's probably also some kind of sort property on the combobox, but I'm
too tired to look it up. ;-)
>I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be made
through it. Should I be looking at doing that?

I think it's talking about when you want to change the data and save it
back to the data source, like when you are binding the data to a
DataGridView. You don't need to worry about that at this point.

I'm glad it's working. Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>To update the display, what I previously had to do was rebind the list
each time I changed the list.
That seemed to work OK.
I inserted the BindingSource as you recommended and no longer need to
rebind after changes.
Chris Dunaway didn't really recommend. He suggested I try BindingList(Of
T). I did and didn't notice any difference.
I tried reading about all the stuff you and Chris suggested - but my
background is lacking and it is pretty slow going.
So I really want to thanks you for all the help. Now at least I know what
I need to read until I understand it.
Strange to have it working and not understand what is going on.

The List class seems a little simpler so I'd prefer to stay with that
unless there is a strong reason to use BindingList - is there such a
reason?

I've been looking at internet sites trying to find out how to sort the
ComboBox display. Can you easily point me to something that would help?

Thanks again for all the help!

PS
I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be made
through it. Should I be looking at doing that?

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:vo******************************@comcast.com ...
>>>I recommend you use a BindingSource component between the data source and
the combobox. You can drag one onto the form from the Toolbox, or create
it ="on the fly".

Dim myBindingSource As BindingSource = New BindingSource()

myBindingSource.DataSource = myList

myComboBox.DataSource = myBindingSource
myComboBox.ValueMember = "whatever"
myComboBox.DisplayMember = "whatever"

Now when you change the list, the BindingSource will respond, and then
repaint the control. The BindingSource is sort of like lubrication
between the data source and the control. Putting it in the middle gives
you a bunch of methods and properties you can access, and better
controls the bound data and the display of it.

As Chris Dunaway recommended, you could define your list as a
BindingList(Of T), but you would still probably want to use a
BindingSource for the reason mentioned above.

Give this a try, and let me know how it works.

Robin S.



Feb 20 '07 #15

You're on the right path with the DirectCast, but you need to cast it to
the object that is in the sortedlist, not to a SortedList itself.

In your sorted list, I'm assuming StringWithInteger is still a class with
both fields in it. Did you try this:

myItem = _
DirectCast(StringWIthIntegerComboBox1.SelectedItem , _
StringWithInteger)

Debug.Print(myItem.str)
Debug.Print(myItem.Value.ToString)

As for the data binding, my guess would be it sorts by the first value, but
it retains the list of objects (second value). So it data binds to the
objects in the list, and only uses the key to sort them.

Robin S.
------------------------------------------

" active" <ac**********@a-znet.comwrote in message
news:Oc**************@TK2MSFTNGP05.phx.gbl...
>I have a feeling I'm taking advantage of you good nature, but I can't
stop!

After I wrote last night, I found the SortedList generic class

Which works OK and produces a sorted display.

However, there is something I can't figure out:

I need to get the integer part of StringWithInteger (Value) of the
selected item

I got this far
= DirectCast(StringWithIntegerComboBox1.SelectedItem , SortedList(Of
String, StringWithInteger))

StringWithInteger has two properties Str and Value

I don't think I want to cast to SortedList but something like
SortedList.Item

That's my present problem.

Thanks again

I'll also try List with the sort method you pointed out but I would like
to know how to do the above.

a smaller problem is in understanding. I continue to use

StringWithIntegerComboBox1.ItemDisplayMember = "Str"

StringWithIntegerComboBox1.ItemValueMember = "Value"

With List there was only one object so I could see how that might work.

With SortedList there are two objects and I wonder how it finds the
property

It appears to find it because I tried

StringWithIntegerComboBox1.ItemDisplayMember = "qqStr"

StringWithIntegerComboBox1.ItemValueMember = "qqValue"

and this produced an exception which makes me think that it was using the
original one - not ignoring them

Suppose both objects had a property "Value", then what?



"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:AI******************************@comcast.com. ..
>>
I figured the BindingSource would fix your problem.

For binding to a combobox, I think a BindingList is overkill. I use a
BindingList when binding a list of my business objects to a DataGridView
so I can access the methods and properties of the bindinglist and
override them as needed. There's no reason for you to not use a List()
if it works for you.

If you are loading the combobox from a datatable, sort it when you pull
it from the database. "Select LastName, FIrstName From MyCustomerTable
ORDER BY Lastname, FirstName".

If that's not viable, the BindingSource has sort capabilities. (See what
I mean about it having methods and properties you can use? Hubba hubba.)

For example, this will sort the data in my CustomersBindingSource by
ContactName in Ascending order.

CustomersBindingSource.Sort = "ContactName ASC"

There's probably also some kind of sort property on the combobox, but
I'm too tired to look it up. ;-)
>>I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be
made through it. Should I be looking at doing that?

I think it's talking about when you want to change the data and save it
back to the data source, like when you are binding the data to a
DataGridView. You don't need to worry about that at this point.

I'm glad it's working. Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>>To update the display, what I previously had to do was rebind the list
each time I changed the list.
That seemed to work OK.
I inserted the BindingSource as you recommended and no longer need to
rebind after changes.
Chris Dunaway didn't really recommend. He suggested I try
BindingList(Of T). I did and didn't notice any difference.
I tried reading about all the stuff you and Chris suggested - but my
background is lacking and it is pretty slow going.
So I really want to thanks you for all the help. Now at least I know
what I need to read until I understand it.
Strange to have it working and not understand what is going on.

The List class seems a little simpler so I'd prefer to stay with that
unless there is a strong reason to use BindingList - is there such a
reason?

I've been looking at internet sites trying to find out how to sort the
ComboBox display. Can you easily point me to something that would help?

Thanks again for all the help!

PS
I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be
made through it. Should I be looking at doing that?

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:vo******************************@comcast.co m...
I recommend you use a BindingSource component between the data source
and the combobox. You can drag one onto the form from the Toolbox, or
create it ="on the fly".

Dim myBindingSource As BindingSource = New BindingSource()

myBindingSource.DataSource = myList

myComboBox.DataSource = myBindingSource
myComboBox.ValueMember = "whatever"
myComboBox.DisplayMember = "whatever"

Now when you change the list, the BindingSource will respond, and then
repaint the control. The BindingSource is sort of like lubrication
between the data source and the control. Putting it in the middle
gives you a bunch of methods and properties you can access, and better
controls the bound data and the display of it.

As Chris Dunaway recommended, you could define your list as a
BindingList(Of T), but you would still probably want to use a
BindingSource for the reason mentioned above.

Give this a try, and let me know how it works.

Robin S.



Feb 20 '07 #16
I came up with the following for SortedList(Of String, StringWithInteger)

'ControlGenericComboBox1.SelectedItem is of type:
System.Collections.Generic.KeyValuePair(Of String, .StringWithInteger)

'And has properties Key and Value

and

DirectCast(ControlGenericComboBox1.SelectedItem,
System.Collections.Generic.KeyValuePair(Of String,
StringWithInteger)).Value.Numb

where Numb is a property of my class

Also, ComboBox disallows sort if a datasource is used.

Also, with

List(Of StringWithInteger)

I couldn't get the following to work.

The doc's seem to say it should.

BindingSource1.Sort = "Str ASC"

Just occurred to me: I wonder if

System.Collections.Generic.KeyValuePair

applies here.

Thanks for all the help

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:wK******************************@comcast.com. ..
>
You're on the right path with the DirectCast, but you need to cast it to
the object that is in the sortedlist, not to a SortedList itself.

In your sorted list, I'm assuming StringWithInteger is still a class with
both fields in it. Did you try this:

myItem = _
DirectCast(StringWIthIntegerComboBox1.SelectedItem , _
StringWithInteger)

Debug.Print(myItem.str)
Debug.Print(myItem.Value.ToString)

As for the data binding, my guess would be it sorts by the first value,
but it retains the list of objects (second value). So it data binds to the
objects in the list, and only uses the key to sort them.

Robin S.
------------------------------------------

" active" <ac**********@a-znet.comwrote in message
news:Oc**************@TK2MSFTNGP05.phx.gbl...
>>I have a feeling I'm taking advantage of you good nature, but I can't
stop!

After I wrote last night, I found the SortedList generic class

Which works OK and produces a sorted display.

However, there is something I can't figure out:

I need to get the integer part of StringWithInteger (Value) of the
selected item

I got this far
= DirectCast(StringWithIntegerComboBox1.SelectedItem , SortedList(Of
String, StringWithInteger))

StringWithInteger has two properties Str and Value

I don't think I want to cast to SortedList but something like
SortedList.Item

That's my present problem.

Thanks again

I'll also try List with the sort method you pointed out but I would like
to know how to do the above.

a smaller problem is in understanding. I continue to use

StringWithIntegerComboBox1.ItemDisplayMember = "Str"

StringWithIntegerComboBox1.ItemValueMember = "Value"

With List there was only one object so I could see how that might work.

With SortedList there are two objects and I wonder how it finds the
property

It appears to find it because I tried

StringWithIntegerComboBox1.ItemDisplayMember = "qqStr"

StringWithIntegerComboBox1.ItemValueMember = "qqValue"

and this produced an exception which makes me think that it was using the
original one - not ignoring them

Suppose both objects had a property "Value", then what?



"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:AI******************************@comcast.com ...
>>>
I figured the BindingSource would fix your problem.

For binding to a combobox, I think a BindingList is overkill. I use a
BindingList when binding a list of my business objects to a DataGridView
so I can access the methods and properties of the bindinglist and
override them as needed. There's no reason for you to not use a List()
if it works for you.

If you are loading the combobox from a datatable, sort it when you pull
it from the database. "Select LastName, FIrstName From MyCustomerTable
ORDER BY Lastname, FirstName".

If that's not viable, the BindingSource has sort capabilities. (See what
I mean about it having methods and properties you can use? Hubba hubba.)

For example, this will sort the data in my CustomersBindingSource by
ContactName in Ascending order.

CustomersBindingSource.Sort = "ContactName ASC"

There's probably also some kind of sort property on the combobox, but
I'm too tired to look it up. ;-)

I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be
made through it. Should I be looking at doing that?

I think it's talking about when you want to change the data and save it
back to the data source, like when you are binding the data to a
DataGridView. You don't need to worry about that at this point.

I'm glad it's working. Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl.. .
To update the display, what I previously had to do was rebind the list
each time I changed the list.
That seemed to work OK.
I inserted the BindingSource as you recommended and no longer need to
rebind after changes.
Chris Dunaway didn't really recommend. He suggested I try
BindingList(Of T). I did and didn't notice any difference.
I tried reading about all the stuff you and Chris suggested - but my
background is lacking and it is pretty slow going.
So I really want to thanks you for all the help. Now at least I know
what I need to read until I understand it.
Strange to have it working and not understand what is going on.

The List class seems a little simpler so I'd prefer to stay with that
unless there is a strong reason to use BindingList - is there such a
reason?

I've been looking at internet sites trying to find out how to sort the
ComboBox display. Can you easily point me to something that would help?

Thanks again for all the help!

PS
I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be
made through it. Should I be looking at doing that?

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:vo******************************@comcast.c om...
>I recommend you use a BindingSource component between the data source
>and the combobox. You can drag one onto the form from the Toolbox, or
>create it ="on the fly".
>
Dim myBindingSource As BindingSource = New BindingSource()
>
myBindingSource.DataSource = myList
>
myComboBox.DataSource = myBindingSource
myComboBox.ValueMember = "whatever"
myComboBox.DisplayMember = "whatever"
>
Now when you change the list, the BindingSource will respond, and then
repaint the control. The BindingSource is sort of like lubrication
between the data source and the control. Putting it in the middle
gives you a bunch of methods and properties you can access, and better
controls the bound data and the display of it.
>
As Chris Dunaway recommended, you could define your list as a
BindingList(Of T), but you would still probably want to use a
BindingSource for the reason mentioned above.
>
Give this a try, and let me know how it works.
>
Robin S.
>




Feb 21 '07 #17
Now I don't know what the heck you're doing. Please post your class
definition for StringWithInteger, what you're using to create a list of
them, and the code you are using to bind the combobox.

Robin S.
---------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uj**************@TK2MSFTNGP06.phx.gbl...
>I came up with the following for SortedList(Of String, StringWithInteger)

'ControlGenericComboBox1.SelectedItem is of type:
System.Collections.Generic.KeyValuePair(Of String, .StringWithInteger)

'And has properties Key and Value

and

DirectCast(ControlGenericComboBox1.SelectedItem,
System.Collections.Generic.KeyValuePair(Of String,
StringWithInteger)).Value.Numb

where Numb is a property of my class

Also, ComboBox disallows sort if a datasource is used.

Also, with

List(Of StringWithInteger)

I couldn't get the following to work.

The doc's seem to say it should.

BindingSource1.Sort = "Str ASC"

Just occurred to me: I wonder if

System.Collections.Generic.KeyValuePair

applies here.

Thanks for all the help

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:wK******************************@comcast.com. ..
>>
You're on the right path with the DirectCast, but you need to cast it to
the object that is in the sortedlist, not to a SortedList itself.

In your sorted list, I'm assuming StringWithInteger is still a class
with both fields in it. Did you try this:

myItem = _
DirectCast(StringWIthIntegerComboBox1.SelectedItem , _
StringWithInteger)

Debug.Print(myItem.str)
Debug.Print(myItem.Value.ToString)

As for the data binding, my guess would be it sorts by the first value,
but it retains the list of objects (second value). So it data binds to
the objects in the list, and only uses the key to sort them.

Robin S.
------------------------------------------

" active" <ac**********@a-znet.comwrote in message
news:Oc**************@TK2MSFTNGP05.phx.gbl...
>>>I have a feeling I'm taking advantage of you good nature, but I can't
stop!

After I wrote last night, I found the SortedList generic class

Which works OK and produces a sorted display.

However, there is something I can't figure out:

I need to get the integer part of StringWithInteger (Value) of the
selected item

I got this far
= DirectCast(StringWithIntegerComboBox1.SelectedItem , SortedList(Of
String, StringWithInteger))

StringWithInteger has two properties Str and Value

I don't think I want to cast to SortedList but something like
SortedList.Item

That's my present problem.

Thanks again

I'll also try List with the sort method you pointed out but I would
like to know how to do the above.

a smaller problem is in understanding. I continue to use

StringWithIntegerComboBox1.ItemDisplayMember = "Str"

StringWithIntegerComboBox1.ItemValueMember = "Value"

With List there was only one object so I could see how that might work.

With SortedList there are two objects and I wonder how it finds the
property

It appears to find it because I tried

StringWithIntegerComboBox1.ItemDisplayMember = "qqStr"

StringWithIntegerComboBox1.ItemValueMember = "qqValue"

and this produced an exception which makes me think that it was using
the original one - not ignoring them

Suppose both objects had a property "Value", then what?



"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:AI******************************@comcast.co m...

I figured the BindingSource would fix your problem.

For binding to a combobox, I think a BindingList is overkill. I use a
BindingList when binding a list of my business objects to a
DataGridView so I can access the methods and properties of the
bindinglist and override them as needed. There's no reason for you to
not use a List() if it works for you.

If you are loading the combobox from a datatable, sort it when you
pull it from the database. "Select LastName, FIrstName From
MyCustomerTable ORDER BY Lastname, FirstName".

If that's not viable, the BindingSource has sort capabilities. (See
what I mean about it having methods and properties you can use? Hubba
hubba.)

For example, this will sort the data in my CustomersBindingSource by
ContactName in Ascending order.

CustomersBindingSource.Sort = "ContactName ASC"

There's probably also some kind of sort property on the combobox, but
I'm too tired to look it up. ;-)

I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be
made through it. Should I be looking at doing that?

I think it's talking about when you want to change the data and save
it back to the data source, like when you are binding the data to a
DataGridView. You don't need to worry about that at this point.

I'm glad it's working. Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl. ..
To update the display, what I previously had to do was rebind the
list each time I changed the list.
That seemed to work OK.
I inserted the BindingSource as you recommended and no longer need to
rebind after changes.
Chris Dunaway didn't really recommend. He suggested I try
BindingList(Of T). I did and didn't notice any difference.
I tried reading about all the stuff you and Chris suggested - but my
background is lacking and it is pretty slow going.
So I really want to thanks you for all the help. Now at least I know
what I need to read until I understand it.
Strange to have it working and not understand what is going on.
>
The List class seems a little simpler so I'd prefer to stay with that
unless there is a strong reason to use BindingList - is there such a
reason?
>
I've been looking at internet sites trying to find out how to sort
the ComboBox display. Can you easily point me to something that would
help?
>
Thanks again for all the help!
>
PS
I still make changes by changing the list.
The BindingSource doc seemed to say that changes in the data can be
made through it. Should I be looking at doing that?
>
>
>
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:vo******************************@comcast. com...
>>I recommend you use a BindingSource component between the data source
>>and the combobox. You can drag one onto the form from the Toolbox, or
>>create it ="on the fly".
>>
>Dim myBindingSource As BindingSource = New BindingSource()
>>
>myBindingSource.DataSource = myList
>>
>myComboBox.DataSource = myBindingSource
>myComboBox.ValueMember = "whatever"
>myComboBox.DisplayMember = "whatever"
>>
>Now when you change the list, the BindingSource will respond, and
>then repaint the control. The BindingSource is sort of like
>lubrication between the data source and the control. Putting it in
>the middle gives you a bunch of methods and properties you can
>access, and better controls the bound data and the display of it.
>>
>As Chris Dunaway recommended, you could define your list as a
>BindingList(Of T), but you would still probably want to use a
>BindingSource for the reason mentioned above.
>>
>Give this a try, and let me know how it works.
>>
>Robin S.
>>
>
>




Feb 22 '07 #18
I've been trying both List and SortedList
The below DirectCast is for SortedList and it works well so I thought I
include it in case someone else needed it.

I've posted the code for StringWithInteger and your prior help related
correctly to that and List.
I should have mentioned in the last post that the DirectCast pertained to
SortedList, not List.
It would have been nice to find out why my Sort does not work with List but
I leave that for another time.
I'm ready to move on, thanks to your help.

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:UN******************************@comcast.com. ..
Now I don't know what the heck you're doing. Please post your class
definition for StringWithInteger, what you're using to create a list of
them, and the code you are using to bind the combobox.

Robin S.
---------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uj**************@TK2MSFTNGP06.phx.gbl...
>>I came up with the following for SortedList(Of String, StringWithInteger)

'ControlGenericComboBox1.SelectedItem is of type:
System.Collections.Generic.KeyValuePair(Of String, .StringWithInteger)

'And has properties Key and Value

and

DirectCast(ControlGenericComboBox1.SelectedItem ,
System.Collections.Generic.KeyValuePair(Of String,
StringWithInteger)).Value.Numb

where Numb is a property of my class
>>
Also, with

List(Of StringWithInteger)

I couldn't get the following to work.

The doc's seem to say it should.

BindingSource1.Sort = "Str ASC"

Feb 22 '07 #19
You're welcome. I'm glad you got it to work for you.
Robin S.
------------------------------
" active" <ac**********@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
I've been trying both List and SortedList
The below DirectCast is for SortedList and it works well so I thought I
include it in case someone else needed it.

I've posted the code for StringWithInteger and your prior help related
correctly to that and List.
I should have mentioned in the last post that the DirectCast pertained to
SortedList, not List.
It would have been nice to find out why my Sort does not work with List
but I leave that for another time.
I'm ready to move on, thanks to your help.

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:UN******************************@comcast.com. ..
>Now I don't know what the heck you're doing. Please post your class
definition for StringWithInteger, what you're using to create a list of
them, and the code you are using to bind the combobox.

Robin S.
---------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uj**************@TK2MSFTNGP06.phx.gbl...
>>>I came up with the following for SortedList(Of String,
StringWithInteger)

'ControlGenericComboBox1.SelectedItem is of type:
System.Collections.Generic.KeyValuePair(Of String, .StringWithInteger)

'And has properties Key and Value

and

DirectCast(ControlGenericComboBox1.SelectedIte m,
System.Collections.Generic.KeyValuePair(Of String,
StringWithInteger)).Value.Numb

where Numb is a property of my class
>>>
Also, with

List(Of StringWithInteger)

I couldn't get the following to work.

The doc's seem to say it should.

BindingSource1.Sort = "Str ASC"


Feb 23 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Bates | last post by:
I've got what at first appears to be a really simple problem. I have a dropdown combobox with a list of names in, the user can enter text into the edit portion or drop the combobox and select a...
2
by: Lars Netzel | last post by:
I have a Combobox that is filled from a dataset. I want to let the user be able to edit the text in the Combobox and if he enters a text that is not a equal to any of the items in the combobox, I...
5
by: Gil | last post by:
Is there a way to tell if a combbox is in dropdown mode. I tried and if statement combobox.dropdown = true but i get an error. dropwndown function doesnt store if its true or false what i am...
5
by: Rich | last post by:
Hello, I have a search application to search data in tables in a database (3 sql server tables). I populate 2 comboboxes with with data from each table. One combobox will contain unique...
1
by: fiaolle | last post by:
Hi The first set of source code is the class for a combobox in a grid, hopefully. In the second set of code we try to use the combobox class, but the grid is empty. I don't understand how this...
3
by: Magnus | last post by:
Im using a set combobox (ComboBox1) to provide a selection of records from a database table. I have a typed dataset (DataSet1) that contains the typed datatable (DataTable1) that the combobox is...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I have to load 30,000 unique names into a combox. Filling a dataTable takes only a few milliseconds. But populating the combobox and displaying the list takes several seconds - way...
6
by: active | last post by:
I need a combobox where the items are typed as string - not object. And another where the items are from a class I generated. Can one make such comboboxes from System.Windows.Forms.ComboBox?...
3
by: Simon van Beek | last post by:
Dear reader, What can be wrong in my ComboBox, the property "Auto Expand" is set to Yes, but by typing in the ComboBox it doesn't expand. Is this because the source of the ComboBox is a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.