473,785 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<tan d
IEquatable<twit h 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 5203
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<tan d
IEquatable<twit h 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.Selected Item
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.Selected Item = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.Selected Item = value
End If
End Set
End Property

End Class

Please note I only tested this to see if it would work with
"Me.ComboBox1.S electedItem = 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_newsgroup s" <ro********@yah oo.comwrote in message
news:11******** *************@n 2g2000hse.googl egroups.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<tan d
IEquatable<twi th 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.Selected Item
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.Selected Item = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.Selected Item = value
End If
End Set
End Property

End Class

Please note I only tested this to see if it would work with
"Me.ComboBox1.S electedItem = 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_newsgroup s" <rowe_em...@yah oo.comwrote in message

news:11******** *************@n 2g2000hse.googl egroups.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<tan d
IEquatable<twit h 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.Selected Item
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.Selected Item = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.Selected Item = value
End If
End Set
End Property
End Class
Please note I only tested this to see if it would work with
"Me.ComboBox1.S electedItem = 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(Va lue 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.selecte ditem = 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_newsgroup s" <ro********@yah oo.comwrote in message
news:11******** *************@n 2g2000hse.googl egroups.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_newsgrou ps" <rowe_em...@yah oo.comwrote in message

news:11******* **************@ n2g2000hse.goog legroups.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<tan d
IEquatable<twi th 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.Selected Item
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.Selected Item = o
Exit For
End If
Catch
Continue For
End Try
Next
Else
MyBase.Selected Item = value
End If
End Set
End Property
End Class
Please note I only tested this to see if it would work with
"Me.ComboBox1.S electedItem = 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******** ******@TK2MSFTN GP02.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<tan d IEquatable<twit h 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.Selecte dValue = 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
1790
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 Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) Dim myCommand As SqlCommand = New SqlCommand("Get_States",
2
10265
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 "descr", and datasource to the array of MyObjects. How can i set the selected value of that combobox based on the ValueMember OR how can i find the index of an item in the combobox BY ValueMember?
5
10841
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. I have tried setting various values to represent the default value with little success. Essentially I am looking to have the first item in the list be the default selected item. I noticed in the properties for a dtat grid you could set a default...
1
4953
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 and a forest green border. However, when you move the cursor from one item to the next within the listbox or you click on an item in the listbox to select it, the background turns to the standard windows dark blue with white letters. Well, that...
2
2493
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 displayed. It seems to work fine on other forms, but for one particular form, nothing I do seems to work. My code is basically: string SQL = ""; SQL = "SELECT StateAbbreviation FROM States ORDER BY StateAbbreviation";
0
1407
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 (MenuItem n1 in mMeny.Items) { if (Convert.ToInt32(n1.Value) == selectedLevel1ID)
16
2988
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
5129
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; CALL_FORM(:GLOBAL.project_path || 'ENROLLMENT_FORM.fmx'); ELSE :GLOBAL.staff_id := :LOGIN_BLOCK.ID;
0
2028
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 correlating the two combobox for checking the redundanvy of data. But how it can implemented. Any example or modification in this code will help me. final String fList = { " -- ", "md", "gtf", "gff", "gff3" , "csv", "other"}; final...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10090
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.