473,387 Members | 3,801 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,387 software developers and data experts.

setting selected item of a combo box filled with objects

Say I have a combo box with the following simple object

Public class MyObject
public ID as integer
public Name as string

public overrides sub ToString() as string
return name
end sub
end Class

now say I have a combo box that I add to the screen and add oh say 20 of
these objects to it (I don't want to data bind in this instance)... now say
I have the ID of 30, how would I tell the combo box to select the object
that has the ID of 30? I thought IEquatable and IComparable would do the job
since it would be a comparision... so implement a IComparable<tand
IEquatable<twith the type integer... to compare to the ID... but nothing
happened... so how would you do this? I just want to say say the selected
item = 30 and have the combo box select the correct item.. thanks!
Jul 16 '07 #1
6 5151
On Jul 16, 9:39 am, "Smokey Grindle" <nos...@nospam.comwrote:
Say I have a combo box with the following simple object

Public class MyObject
public ID as integer
public Name as string

public overrides sub ToString() as string
return name
end sub
end Class

now say I have a combo box that I add to the screen and add oh say 20 of
these objects to it (I don't want to data bind in this instance)... now say
I have the ID of 30, how would I tell the combo box to select the object
that has the ID of 30? I thought IEquatable and IComparable would do the job
since it would be a comparision... so implement a IComparable<tand
IEquatable<twith the type integer... to compare to the ID... but nothing
happened... so how would you do this? I just want to say say the selected
item = 30 and have the combo box select the correct item.. thanks!
While this might not be the best way, I would shadow the SelectedItem
property and loop through the Items collection.

Something Like:

Public Class MyCombobox
Inherits ComboBox

Public Shadows Property SelectedItem() As Object
Get
Return MyBase.SelectedItem
End Get
Set(ByVal value As Object)
If IsNumeric(value) Then
For Each o As Object In Me.Items
Try
Dim myObject As MyObject = DirectCast(o,
MyObject)
If myObject.ID = CInt(value) Then
MyBase.SelectedItem = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.SelectedItem = value
End If
End Set
End Property

End Class

Please note I only tested this to see if it would work with
"Me.ComboBox1.SelectedItem = SomeInt", so you should do some
additional testing just in case.

Thanks,

Seth Rowe

Jul 16 '07 #2
I dont want to make a custom combo box for every type I have like this...
this was just one example...

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11*********************@n2g2000hse.googlegrou ps.com...
On Jul 16, 9:39 am, "Smokey Grindle" <nos...@nospam.comwrote:
>Say I have a combo box with the following simple object

Public class MyObject
public ID as integer
public Name as string

public overrides sub ToString() as string
return name
end sub
end Class

now say I have a combo box that I add to the screen and add oh say 20 of
these objects to it (I don't want to data bind in this instance)... now
say
I have the ID of 30, how would I tell the combo box to select the object
that has the ID of 30? I thought IEquatable and IComparable would do the
job
since it would be a comparision... so implement a IComparable<tand
IEquatable<twith the type integer... to compare to the ID... but
nothing
happened... so how would you do this? I just want to say say the selected
item = 30 and have the combo box select the correct item.. thanks!

While this might not be the best way, I would shadow the SelectedItem
property and loop through the Items collection.

Something Like:

Public Class MyCombobox
Inherits ComboBox

Public Shadows Property SelectedItem() As Object
Get
Return MyBase.SelectedItem
End Get
Set(ByVal value As Object)
If IsNumeric(value) Then
For Each o As Object In Me.Items
Try
Dim myObject As MyObject = DirectCast(o,
MyObject)
If myObject.ID = CInt(value) Then
MyBase.SelectedItem = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.SelectedItem = value
End If
End Set
End Property

End Class

Please note I only tested this to see if it would work with
"Me.ComboBox1.SelectedItem = SomeInt", so you should do some
additional testing just in case.

Thanks,

Seth Rowe

Jul 16 '07 #3
On Jul 16, 10:37 am, "Smokey Grindle" <nos...@nospam.comwrote:
I dont want to make a custom combo box for every type I have like this...
this was just one example...

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11*********************@n2g2000hse.googlegrou ps.com...
On Jul 16, 9:39 am, "Smokey Grindle" <nos...@nospam.comwrote:
Say I have a combo box with the following simple object
Public class MyObject
public ID as integer
public Name as string
public overrides sub ToString() as string
return name
end sub
end Class
now say I have a combo box that I add to the screen and add oh say 20 of
these objects to it (I don't want to data bind in this instance)... now
say
I have the ID of 30, how would I tell the combo box to select the object
that has the ID of 30? I thought IEquatable and IComparable would do the
job
since it would be a comparision... so implement a IComparable<tand
IEquatable<twith the type integer... to compare to the ID... but
nothing
happened... so how would you do this? I just want to say say the selected
item = 30 and have the combo box select the correct item.. thanks!
While this might not be the best way, I would shadow the SelectedItem
property and loop through the Items collection.
Something Like:
Public Class MyCombobox
Inherits ComboBox
Public Shadows Property SelectedItem() As Object
Get
Return MyBase.SelectedItem
End Get
Set(ByVal value As Object)
If IsNumeric(value) Then
For Each o As Object In Me.Items
Try
Dim myObject As MyObject = DirectCast(o,
MyObject)
If myObject.ID = CInt(value) Then
MyBase.SelectedItem = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.SelectedItem = value
End If
End Set
End Property
End Class
Please note I only tested this to see if it would work with
"Me.ComboBox1.SelectedItem = SomeInt", so you should do some
additional testing just in case.
Thanks,
Seth Rowe
Ahh, that puts a different spin on things.

Perhaps you could use an Interface on the types that forces them to
implement an integer ID property? As it would be a single member
interface it shouldn't be very hard to implement (especially since you
seem to be using an ID property already) Then you could use the new
combobox class to handle any of these items (this also means you could
add multiple item types to a single combobox and select them by Id -
as long as they implement the interface).

If you don't want to go the Interface route, I'm not sure what to tell
you. You somehow have to have a way to inform the combobox on which
property to inspect to determine equality.

Thanks,

Seth Rowe

Jul 16 '07 #4
"Smokey Grindle" <no****@nospam.comschrieb
I dont want to make a custom combo box for every type I have like
this... this was just one example...
No good news, but... I didn't find another way but writing a loop to find
the item. I thought there must be an "IndexOf(Value as object, c as
IComparer)" method, but there isn't. Neither in the class itself, nor in the
base class, nor in the implemented interfaces, nor in the interfaces
implemented by the Combobox' Item property.

So, what's left is

for each item as MyObject in mycombo.items
if item.id = 30 then
mycombo.selecteditem = item
exit for
end if
next
Armin

Jul 16 '07 #5
right now all the objects are exposed through interface definitions... so
that part is already handled... for some reason I thought in the past I
implemented an icomparable or IEquatable to do what I wanted to do...
because I could of sworn that when it compares it just used the comparision
interface to compare objects when you say to select an object, but when i
stepped through it, that method was never called...

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11*********************@n2g2000hse.googlegrou ps.com...
On Jul 16, 10:37 am, "Smokey Grindle" <nos...@nospam.comwrote:
>I dont want to make a custom combo box for every type I have like this...
this was just one example...

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11*********************@n2g2000hse.googlegro ups.com...
On Jul 16, 9:39 am, "Smokey Grindle" <nos...@nospam.comwrote:
Say I have a combo box with the following simple object
>Public class MyObject
public ID as integer
public Name as string
>public overrides sub ToString() as string
return name
end sub
end Class
>now say I have a combo box that I add to the screen and add oh say 20
of
these objects to it (I don't want to data bind in this instance)...
now
say
I have the ID of 30, how would I tell the combo box to select the
object
that has the ID of 30? I thought IEquatable and IComparable would do
the
job
since it would be a comparision... so implement a IComparable<tand
IEquatable<twith the type integer... to compare to the ID... but
nothing
happened... so how would you do this? I just want to say say the
selected
item = 30 and have the combo box select the correct item.. thanks!
While this might not be the best way, I would shadow the SelectedItem
property and loop through the Items collection.
Something Like:
Public Class MyCombobox
Inherits ComboBox
Public Shadows Property SelectedItem() As Object
Get
Return MyBase.SelectedItem
End Get
Set(ByVal value As Object)
If IsNumeric(value) Then
For Each o As Object In Me.Items
Try
Dim myObject As MyObject = DirectCast(o,
MyObject)
If myObject.ID = CInt(value) Then
MyBase.SelectedItem = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.SelectedItem = value
End If
End Set
End Property
End Class
Please note I only tested this to see if it would work with
"Me.ComboBox1.SelectedItem = SomeInt", so you should do some
additional testing just in case.
Thanks,
Seth Rowe

Ahh, that puts a different spin on things.

Perhaps you could use an Interface on the types that forces them to
implement an integer ID property? As it would be a single member
interface it shouldn't be very hard to implement (especially since you
seem to be using an ID property already) Then you could use the new
combobox class to handle any of these items (this also means you could
add multiple item types to a single combobox and select them by Id -
as long as they implement the interface).

If you don't want to go the Interface route, I'm not sure what to tell
you. You somehow have to have a way to inform the combobox on which
property to inspect to determine equality.

Thanks,

Seth Rowe

Jul 16 '07 #6
"Smokey Grindle" <no****@nospam.comwrote in message
news:ux**************@TK2MSFTNGP02.phx.gbl...
Say I have a combo box with the following simple object

Public class MyObject
public ID as integer
public Name as string

public overrides sub ToString() as string
return name
end sub
end Class

now say I have a combo box that I add to the screen and add oh say 20 of
these objects to it (I don't want to data bind in this instance)... now
say I have the ID of 30, how would I tell the combo box to select the
object that has the ID of 30? I thought IEquatable and IComparable would
do the job since it would be a comparision... so implement a
IComparable<tand IEquatable<twith the type integer... to compare to
the ID... but nothing happened... so how would you do this? I just want to
say say the selected item = 30 and have the combo box select the correct
item.. thanks!
MyCombo.SelectedValue = 30...provided you have set the ValueMember property
to the ID of your object
Jul 18 '07 #7

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

Similar topics

3
by: Stanley J Mroczek | last post by:
I have tried this with no luck. What i want is to show the selected item on the first post. the selected item is NY but the first in the list shows as selected. If Page.IsPostBack = False Then...
2
by: farseer | last post by:
Hi, I have a combobox who's data source i set to an array of objects (call it MyObject). these objects have get properties: key, value, descr. i set ValueMember to "key", DisplayMember to...
5
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value....
1
by: Karen Grube | last post by:
Hi! I'm using a standard server side ASP.Net listbox control on a web form. The page is basically various shades of green. The listbox itself has a pale green background and forest green text...
2
by: B | last post by:
I'm trying to simply build a form with a combo box containing a list of states. I'd like for there to be NO default selected item, but invariably, the first item in the DataSource is being...
0
by: Eirik Eldorsen | last post by:
I can't figure out how to set that an level1 and level2 item is selected at the same time. Is this not possible? Here is my code that set selected item: private void UpdateMenu() { foreach...
16
Ranjan kumar Barik
by: Ranjan kumar Barik | last post by:
Hello !!! I am Ranjan. can the selected item of a combo box be displayed on the same page. I mean when I just click the item of a combo box it will atonce displayed on the page. Thanks !!!
3
by: mlevit | last post by:
Hi, I need to obtain the value of the selected item from a combo box. BEGIN IF :LOGIN_BLOCK.USER_TYPE = 'Student' THEN :GLOBAL.student_id := :LOGIN_BLOCK.ID;...
0
by: Man4ish | last post by:
Hi, I am using multiple combo list (2 here). I want to store the selected item by these two combo boxes in an array list to check that same value should not be selected by two. This way i am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.