Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / January 2007

Tip: Looking for answers? Try searching our database.

Selecting a value in a ComboBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
robin9876@hotmail.com - 22 Jan 2007 16:53 GMT
In Visual Studio.Net 2005 Windows Form (VB) I have a combobox which is
populated from an arraylist of a user defined object. The values appear
in the combobox.

However when I try to select an option from the combobox based on a
supplied parameter it does not select an option.

I have used the following

combobox1.SelectedIndex =
combobox1.Items.IndexOf(combobox1.FindString(myString))

OR

combobox1.SelectedValue = myString

Both do not select a value, even when passing the value of the display
or value items.

How do you select a value based on a supplied parameter?
Kevin Spencer - 22 Jan 2007 17:06 GMT
The ComboBox.FindString method returns an integer. The Items.IndexOf method
takes an object as a parameter and finds that object in the list. Since the
object in question is an integer, it doesn't find it. Instead, you should
use:

combobox1.SelectedIndex = combobox1.FindString(myString)

Signature

HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.

> In Visual Studio.Net 2005 Windows Form (VB) I have a combobox which is
> populated from an arraylist of a user defined object. The values appear
[quoted text clipped - 16 lines]
>
> How do you select a value based on a supplied parameter?
robin9876@hotmail.com - 23 Jan 2007 11:23 GMT
Thanks that now works when searching on the Text of the combobox.

Is it possible to search on the Value item of the combobox when this is
a number or string?

> The ComboBox.FindString method returns an integer. The Items.IndexOf method
> takes an object as a parameter and finds that object in the list. Since the
[quoted text clipped - 33 lines]
> >
> > How do you select a value based on a supplied parameter?
Kevin Spencer - 23 Jan 2007 12:17 GMT
I'm not sure what you're asking, as all you have talked about is an
ArrayList, which is a collection of type object, and a ComboBox, which has
an Items property that is an ObjectCollection. You have not mentioned
whether the ComboBox is DataBound, and you have stated that "the values
appear in the ComboBox." Since what appears in the ComboBox is strings, or a
value indicated by calling the ToString() method on an object, I have no
idea what your situation is. However, I can give you a couple of good
references on the subject:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.aspx
http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.objectcol
lection.aspx


Signature

HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.

> Thanks that now works when searching on the Text of the combobox.
>
[quoted text clipped - 40 lines]
>> >
>> > How do you select a value based on a supplied parameter?
robin9876@hotmail.com - 23 Jan 2007 13:51 GMT
I am looking for something like ComboBox.FindValue

A scenario would be a value passed from a data source which is the
value of an item in a combobox which has a text value which is a
description.
e.g. from an employee record it selects a departmentid which then is
selected in a combobox but displays the Department name.

> I'm not sure what you're asking, as all you have talked about is an
> ArrayList, which is a collection of type object, and a ComboBox, which has
[quoted text clipped - 62 lines]
> >> >
> >> > How do you select a value based on a supplied parameter?
Jeff Gaines - 23 Jan 2007 15:06 GMT
On 23/01/2007 in message
<1169560293.124313.238150@l53g2000cwa.googlegroups.com>

>I am looking for something like ComboBox.FindValue
>
[quoted text clipped - 3 lines]
>e.g. from an employee record it selects a departmentid which then is
>selected in a combobox but displays the Department name.

There are probably several options.

You could iterate through the Combo Box items, cast them to the original
class you added them as and look for the ID.
You could have a function in the class which links the id to the displayed
text, interrogate the class with the id and it returns the text to find in
the Combo Box.
You could maintain a hash table which links the department id to a
department name (or even id to Combo Box index, although that would entail
a bit more work).

Signature

Jeff Gaines

Kevin Spencer - 23 Jan 2007 21:10 GMT
Ok, let me be a bit clearer. In order to answer your question, could you
answer a couple for me?

1. Is the ComboBox databound?
2. Is the ComboBox databound to more than one list (e.g. DataTables, one for
values, and one for display)?
3. What is the data type of the values in the ComboBox?

Signature

HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.

>I am looking for something like ComboBox.FindValue
>
[quoted text clipped - 77 lines]
>> >> >
>> >> > How do you select a value based on a supplied parameter?
robin9876@hotmail.com - 25 Jan 2007 13:55 GMT
1) No
2) No
3) String

> Ok, let me be a bit clearer. In order to answer your question, could you
> answer a couple for me?
[quoted text clipped - 100 lines]
>
> >> >> > How do you select a value based on a supplied parameter?
Jeff Williams - 25 Jan 2007 19:59 GMT
Hi Robin

I have the same issue and here is the question again with a complete
code sample

I have the code below to fill a combobox.  I would appreciate any
comments on a better way to do this fill.

My main problem is how do I set the selected combo box display value
based on the "Id" column  if the Id is  "paytype" .  Also once the user
selects a combobox item from the dropdown how do I then retrieve the
"paytype" from the Id column.

        void Fill_LookupType()
        {
            int nItem;
            nItem = 0;

            string select = "SELECT description, lookuptypevalue ";
            select += "FROM lookuptypes ";
            select += "WHERE active = 1 ";
            select += "ORDER BY description ASC ";
            dbConnect Connection = new dbConnect();
            OdbcConnection conn = new  OdbcConnection(
@Connection.Connect());
            conn.Open();
            OdbcCommand cmd = new OdbcCommand(select,conn);
            OdbcDataReader aReader = cmd.ExecuteReader();

            DataTable list = new DataTable();
            list.Columns.Add(new DataColumn("Display", typeof(string)));
            list.Columns.Add(new DataColumn("Id", typeof(string)));

            while(aReader.Read())
            {
                list.Rows.Add(list.NewRow());
                list.Rows[nItem][0] = aReader.GetString(0);
                list.Rows[nItem++][1] = aReader.GetString(1);
            }

            aReader.Close();
            conn.Close();

            this.cb_LookupType.DataSource = list;
            this.cb_LookupType.DisplayMember = "Display";
            this.cb_LookupType.ValueMember = "Id";
            ;

           }
RobinS - 25 Jan 2007 22:16 GMT
So when the user selects one of the entries in the combobox,
what does cb_Lookup.SelectedValue equal? Does it not equal
the value of the "ID" field selected?

Robin S.
---------------------------------------
> Hi Robin
>
[quoted text clipped - 46 lines]
>
>            }
Jeff Williams - 25 Jan 2007 23:52 GMT
I have no Idea I dont know how to set or retrieve the values thats what
I am asking.

How can I get the value and also set the item displayed in the combo box
using the ID field.

I guess your saying to get the value use cb_Lookup.SelectedValue so I
try that now how do i set the item in the combobox to show according to
the ID value I supply.
RobinS - 26 Jan 2007 00:21 GMT
You don't want to show the ID in the combobox, you want to show the
[display] field, right?

To set the selected item in the combobox for a specific ID, try

 cb_Lookup.SelectedValue = myID

SelectedValue is a R/W field.

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

> I have no Idea I dont know how to set or retrieve the values thats
> what I am asking.
[quoted text clipped - 5 lines]
> try that now how do i set the item in the combobox to show according
> to the ID value I supply.
Jeff Williams - 28 Jan 2007 07:42 GMT
Thanks Robin your answer is what I was looking for.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.