PC Review


Reply
Thread Tools Rate Thread

Can I make a combobox that is stronly typed?

 
 
active
Guest
Posts: n/a
 
      18th Feb 2007
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



 
Reply With Quote
 
 
 
 
RobinS
Guest
Posts: n/a
 
      19th Feb 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
active
Guest
Posts: n/a
 
      19th Feb 2007
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" <(E-Mail Removed)> wrote in message
news:3didnWh-p7DvtkTYnZ2dnUVZ_u-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>
>>

>
>



 
Reply With Quote
 
active
Guest
Posts: n/a
 
      19th Feb 2007
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" <(E-Mail Removed)> wrote in message
news:3didnWh-p7DvtkTYnZ2dnUVZ_u-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>
>>

>
>



 
Reply With Quote
 
active
Guest
Posts: n/a
 
      19th Feb 2007
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




 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      19th Feb 2007
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" <(E-Mail Removed)> schreef in bericht
news:%(E-Mail Removed)...
> 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

>
>
>



 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      19th Feb 2007
Can you show your class definition for StringWithInteger?

Robin S.
-------------------------------
" active" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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

>
>
>



 
Reply With Quote
 
active
Guest
Posts: n/a
 
      19th Feb 2007

"Cor Ligthert [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> schreef in bericht
> news:%(E-Mail Removed)...
>> 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

>>
>>
>>

>
>



 
Reply With Quote
 
active
Guest
Posts: n/a
 
      19th Feb 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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




 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      19th Feb 2007
On Feb 19, 3:57 pm, " active" <activeNOS...@a-znet.com> wrote:
> "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl> wrote in messagenews:(E-Mail Removed)...
>
> > 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.com> schreef in bericht
> >news:%(E-Mail Removed)...
> >> 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

 
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
about getting the combobox value while the value is typed or chang Tahseena Ishrat Ahmed Microsoft Access Form Coding 1 9th May 2008 04:08 AM
Howto make Combobox requery based on dependant combobox values Shane Microsoft Access Form Coding 1 22nd Apr 2008 09:14 AM
What is a combobox that cannot be typed in DF.thangld Microsoft Access Forms 2 23rd Mar 2008 06:04 PM
Need a combobox with typed items active Microsoft C# .NET 6 19th Feb 2007 01:12 PM
Typed Datasets with ComboBox gordigor Microsoft ADO .NET 1 12th Dec 2006 10:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:07 PM.