Re: Problem setting "SelectedValue" in ComboBox using .Net CF

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Tim Wilson ("Tim)
Date: 03/12/05


Date: Sat, 12 Mar 2005 11:40:34 -0500

If it happens on the device then I wouldn't think that VS.Net 2003 would
have anything to do with it. What device are you using? What is the OS on
the device? And you are sure that you have successfully installed SP3
(http://wiki.opennetcf.org/ow.asp?CompactFrameworkFAQ%2FDeterminingVersion)?
If everything looks to be in place then if you could come up with a small
project that reliably reproduces the behavior and post it back to this
newsgroup then I'll work through it with you. Also post back a description
of what you are doing and what you expect to happen.

-- 
Tim Wilson
.Net Compact Framework MVP
"charliewest" <charliewest@discussions.microsoft.com> wrote in message
news:F5526AED-EBF3-45CF-8849-D0FED33CB7F9@microsoft.com...
> I have installed SP3 and this still does not work. I have tested this on
an
> external device and my emulator with the same negative results (the
emulator
> does not have SP3 installed however).
>
> When i try force the selected item via the "SelectedIndex" property i get
> the following error: A first chance exception of type 'System.Exception'
> occurred in System.Windows.Forms.dll
>
> Could there be a bug w/ my VS 2003?
>
> "Tim Wilson" wrote:
>
> > The following code worked for me.
> >
> > // Bind to the ComboBox. Called from a Button Click handler.
> > ArrayList ar_Gender = new ArrayList();
> > ar_Gender.Add(new CreateArrayLists(" ---- ", " - ", -1));
> > ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> > ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> > this.comboBox1.DataSource = ar_Gender;
> > this.comboBox1.DisplayMember = "LongText";
> > this.comboBox1.ValueMember = "DefaultValue";
> >
> > // CreateArrayLists definition. Inside the main Form scope.
> > private class CreateArrayLists
> > {
> >   private string longText;
> >   private string shortText;
> >   private int defaultValue;
> >
> >   public CreateArrayLists(string longText, string shortText, int
> > defaultValue)
> >   {
> >     this.longText = longText;
> >     this.shortText = shortText;
> >     this.defaultValue = defaultValue;
> >   }
> >
> >   public string LongText
> >   {
> >     get
> >     {
> >       return longText;
> >     }
> >     set
> >     {
> >       longText = value;
> >     }
> >   }
> >
> >   public string ShortText
> >   {
> >     get
> >     {
> >       return shortText;
> >     }
> >     set
> >     {
> >       shortText = value;
> >     }
> >   }
> >
> >   public int DefaultValue
> >   {
> >     get
> >     {
> >       return defaultValue;
> >     }
> >     set
> >     {
> >       defaultValue = value;
> >     }
> >   }
> > }
> >
> > // Called from another Button Click handler.
> > // Selects the "Female" item.
> > this.comboBox1.SelectedValue = 1;
> >
> > If this code does not work as expected for you, and if you haven't
already,
> > download and install SP3.
> >
http://www.microsoft.com/downloads/details.aspx?FamilyID=a5a02311-194b-4c00-b445-f92bec03032f&displaylang=en
> >
> > -- 
> > Tim Wilson
> > ..Net Compact Framework MVP
> >
> > "charliewest" <charliewest@discussions.microsoft.com> wrote in message
> > news:87BE0979-D08D-4A5B-92E0-A2ED3EC1E2DD@microsoft.com...
> > > Using .Net CF, i have created a 2 dimension ArrayList, and "binded"
this
> > list
> > > to a ComboBox control using the "DataSource" property. I have set the
> > > DisplaySource and ValueMember properties as well. The control
populates
> > well,
> > > both the display values, and selected values.
> > >
> > > However, when i try to "set" the SelectedValue or SelectedIndex
> > properties,
> > > nothing happens.... The default blank value in the ComboBox is always
> > > selected. My code is:
> > >
> > > // Get Gender Array List
> > > ArrayList ar_Gender = new ArrayList();
> > > ar_Gender.Add(new CreateArrayLists(" ---- ", "  - ", -1));
> > > ar_Gender.Add(new CreateArrayLists("Male", "M", 0));
> > > ar_Gender.Add(new CreateArrayLists("Female", "F", 1));
> > > cboxGender.Size = new Size(100, kControlHeight);
> > > cboxGender.Location = new Point(lblGender.Right, lblGender.Top);
> > > cboxGender.GotFocus += new EventHandler(HideSIP);
> > > cboxGender.DataSource = ar_Gender;
> > > cboxGender.DisplayMember = "LongText";
> > > cboxGender.ValueMember = "DefaultValue";
> > > cboxGender.Font = kFontSmall;
> > > if (!bIsNewSubject) cboxGender.SelectedValue = 1;
> > >
> > > Any ideas why this doesn't work?
> > >
> > > Thanks,
> >
> >
> >

Quantcast