Re: ComboBox ItemData?
From: Ginny Caughey [MVP] (ginny.caughey.online_at_wasteworks.com)
Date: 10/20/04
- Next message: Boris Nienke: "Re: ComboBox ItemData?"
- Previous message: Peter Foot [MVP]: "Re: Deploy .net application on pocket pc with Win CE 4.1"
- In reply to: PeterB: "Re: ComboBox ItemData?"
- Next in thread: PeterB: "Re: ComboBox ItemData?"
- Reply: PeterB: "Re: ComboBox ItemData?"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 20 Oct 2004 07:53:15 -0400
Peter,
The business objects I use never get deleted, so I never ran into the
situation you describe. If I had though, I think I would have used the same
solution that you did. With data binding particularly, and not just .Net
data binding but every kind I've ever used, there always seems to be
somewhat of a trial-and-error phase until I see what it really does.
--
Ginny Caughey
.Net Compact Framework MVP
"PeterB" <peter@data.se> wrote in message
news:%23AxPFmntEHA.3564@tk2msftngp13.phx.gbl...
> Ginny, if you use the ArrayList of objects method, do you have any trouble
> with removing items from the list?
>
> I Add a bunch of objects to my list and set the DataSource of the combobox
> to the list. All items are displayed (according to the DisplayMember of
> the combo). If I select the _last_ item in the list, and remove it. The
> combobox will change to SelectedIndex -1.
>
> private void btnDelete_Click(object sender, System.EventArgs e)
> {
> if( cmbBoxItems.SelectedIndex != -1 )
> {
> lst.Remove( (MyClass)cmbBoxItems.SelectedItem );
> ResetDataBinding();
> }
> }
>
> After this I need to reset the DataSource since my ArrayList doesn't
> implement IBindingList and doesn't propagate the changes automatically to
> the combobox. I set DataSource to null and then reset it to the list
> again.
>
> The combo is updated and now contains 1 object less than before. If I now
> select the last item... CRASH! ArgumentOutOfRangeException is thrown.
>
> If I change the delete handler to the following, I don't get an exception
> however:
>
> private void btnDelete_Click(object sender, System.EventArgs e)
> {
> int indx = cmbBoxItems.SelectedIndex;
> if( indx != -1 )
> {
> if( indx == cmbBoxItems.Items.Count - 1 && cmbBoxItems.Items.Count
> > 1 ) // Last obj
> {
> MyClass mc = (MyClass)cmbBoxItems.SelectedItem;
> cmbBoxItems.SelectedIndex--;
> lst.Remove( mc );
> }
> else
> lst.Remove( (MyClass)cmbBoxItems.SelectedItem );
>
> ResetDataBinding();
> }
> }
>
> Is it neccessary to perform the actions showed in the second code snippet.
> I prevent the SelectedIndex = -1, but that's about the only difference...
> I guess this is a bug somewhere in either ComboBox or ArrayList...
>
> regards,
>
> Peter
>
>
>
> "Ginny Caughey [MVP]" <ginny.caughey.online@wasteworks.com> skrev i
> meddelandet news:%23lL$zgetEHA.1368@TK2MSFTNGP15.phx.gbl...
>> Tim,
>>
>> I use the ArrayList of objects approach described by Alex. The objects I
>> use like this all have a property called DataObject that looks like this:
>>
>> public Customer DataObject
>> { get{return tnis;}}
>>
>> so I can just set the ValueMember of the combo box to DataObject and get
>> back the data object itself.
>>
>> --
>> Ginny Caughey
>> .Net Compact Framework MVP
>>
>>
>>
>> "Tim Johnson" <tjohnson@high-point.com> wrote in message
>> news:eU55kFatEHA.1404@TK2MSFTNGP11.phx.gbl...
>>> I'm coming from MFC-land where a combobox had an ItemDataPtr property,
>>> whereby you could stash away a ptr to some arbitrary object. Then when
>>> the
>>> user selected an item, you could fetch the object back based on the
>>> selected
>>> index. Is there anything comparable in C#? The only samples I've seen
>>> show
>>> them managing their own separate arraylist corresponding to the items in
>>> the
>>> combobox - yechh!
>>>
>>> --
>>> Tim Johnson
>>> High Point Software
>>> www.high-point.com
>>> (503) 312-8625
>>>
>>>
>>
>>
>
>
- Next message: Boris Nienke: "Re: ComboBox ItemData?"
- Previous message: Peter Foot [MVP]: "Re: Deploy .net application on pocket pc with Win CE 4.1"
- In reply to: PeterB: "Re: ComboBox ItemData?"
- Next in thread: PeterB: "Re: ComboBox ItemData?"
- Reply: PeterB: "Re: ComboBox ItemData?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|