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 General / January 2008

Tip: Looking for answers? Try searching our database.

Combobox SelectedItem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert - 11 Jan 2008 17:19 GMT
I am trying to set a Combobox's SelectedItem property to an object but
without any success.
Code Example:
           for (int i = 1; i < 10; i++)
           {
               Person p = new Person("JDoe" + i.ToString());
               Combobox1.Items.Add(p);
           }

           Person findP = new Person ("JDoe5");
           Combobox1.SelectedItem = findP;

The combobox does not set the SelectedItem when I run the above code.
Obviously I'm missing something and any help is appreciated.
Jack Jackson - 11 Jan 2008 19:35 GMT
>I am trying to set a Combobox's SelectedItem property to an object but
>without any success.
[quoted text clipped - 10 lines]
>The combobox does not set the SelectedItem when I run the above code.
>Obviously I'm missing something and any help is appreciated.

It doesn't work because the object that findP references is not in the
combobox.  It was created the same as one that is there, but it is a
different object.  You must use the same object.

Since you know you want to select the 5th one, I would use:
   Combobox1.SelectedIndex = 5-1;

If you don't know ahead of time which one you want:

   int selIndx = -1;

   for (int i = 1; i < 10; i++)
   {
       Person p = new Person("JDoe" + i.ToString());
       Combobox1.Items.Add(p);
       if ( ... )
           selIndx = i - 1;    /* Select this one */
   }
   Combobox1.SelectedIndex = selIndx;

Both examples assume that the combobox is empty before the loop.
Another possibility is to save p in another variable inside the loop
and set SelectedItem to the saved variable.  I would presume that
using SelectedIndex would be more efficient than SelectedItem, since I
figure SelectedItem must sequentially examine each item in the list.

Rate this thread:







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.