This is a migrated thread and some comments may be shown as answers.

Listbox bound with List<Class>

4 Answers 166 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
ManniAT
Top achievements
Rank 2
ManniAT asked on 01 Apr 2008, 08:37 PM
Hi,

I use a forms.listbox an bind it to a List<MyClass>.
Then I set DisplayMember to the name of a porperty.

In the selectedindexchanged I do:
MyClass selEle=listBox1.SelectedValue as MyClass;

And (as expected) I get an object assigned if there is a selected item in the LB.

With RADListBox I always get null!!!

Other "unexpected" behaviour.
If I doubleclick on a listbox in designmode a handler for "SelectedIndexChanged" is added -- this is what I'm used to for years.

With RADListBox a handler for Click is added instead!

So how can I bind a List<MyClass> to a rad listbox and get the selected class element as selectedvalue?

Regards

Manfred

4 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay
Telerik team
answered on 03 Apr 2008, 04:17 PM
Hello Manfred Pohler,

RadControls for WinForms are similar to Microsoft ones. However, they provide richer functionality and therefore, there are differences in the API as well as in the behavior.

This is also the case with RadListBox. Its ListBoxElement has the ItemDataBound event, where you can associate the custom objects to the Tag property of RadListBoxItems. This is the approach you can use when you need to get an object when selecting an item.

I have attached a sample application to demonstrate the above approach. The default event handler of RadListBox on double click at design time is addressed for the Q1 2008.

If you have additional questions, do not hesitate to contact me.

Best wishes,
Nikolay
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
ManniAT
Top achievements
Rank 2
answered on 09 Apr 2008, 04:26 PM
Thanks a lot - exactly what I was looking for!
0
ManniAT
Top achievements
Rank 2
answered on 10 Apr 2008, 02:17 PM
Hi,

to make the things as compatible as possible I tried to inherti RadListBox as follows:

    public class BEListBox : RadListBox {  
        public BEListBox() {  
            ListBoxElement.ItemDataBound += new ItemDataBoundEventHandler(ListBoxElement_ItemDataBound);  
        }  
        void ListBoxElement_ItemDataBound(object sender, ItemDataBoundEventArgs e) {  
            RadListBoxItem rI = e.DataBoundItem as RadListBoxItem;  
            if (rI != null) {  
                rI.Tag = e.DataItem;  
            }  
        }  
        protected override void OnSelectedIndexChanged(EventArgs e) {  
            base.OnSelectedIndexChanged(e);  
            RadListBoxItem lI=SelectedItem as RadListBoxItem;  
            if (lI != null) {  
                SelectedValue = lI.Tag;  
            }  
        }  
    }  
 
Works so far for the ItemDataBound - but not for the "OnSelectedIndexChanged".
On the line "SelectedValur=lI.Tag I get the following exception:

System.InvalidOperationException: List Control Empty ValueMember  
   bei Telerik.WinControls.UI.RadListBoxElement.set_SelectedValue(Object value)  
   bei Telerik.WinControls.UI.RadListBox.set_SelectedValue(Object value)  
   bei Sputt1.Controls.BEListBox.OnSelectedIndexChanged(EventArgs e) in J:\Daten\WORK\Miba\SputterNEU\Protos\Sputt1\Sputt1\Controls\BEListBox.cs:Zeile 23.  
   bei Telerik.WinControls.UI.RadListBox.listBoxElement_SelectedIndexChanged(Object sender, EventArgs e)  
   bei Telerik.WinControls.UI.RadListBoxElement.OnSelectedIndexChanged(SelectedIndexChangedEventArgs e)  
   bei Telerik.WinControls.UI.RadListBoxElement.CallSelectedIndexChanged()  
   bei Telerik.WinControls.UI.RadListBoxElement.selectedItems_CollectionChanged(Object sender, RadListBoxItemCollectionChangedEventArgs e)  
   bei Telerik.WinControls.UI.RadListBoxItemCollection.OnCollectionChanged(RadListBoxItemCollectionChangedEventArgs e)  
   bei Telerik.WinControls.UI.RadListBoxItemCollection.OnInsertComplete(Int32 index, Object value)  
   bei System.Collections.CollectionBase.System.Collections.IList.Add(Object value)  
   bei Telerik.WinControls.UI.RadListBoxElement.SetSelectedIndex(RadItem item)  
   bei Telerik.WinControls.UI.RadListBoxElement.listBoxItem_MouseDown(Object sender, MouseEventArgs e)  
   bei Telerik.WinControls.RadElement.OnMouseDown(MouseEventArgs e)  
   bei Telerik.WinControls.RadItem.OnMouseDown(MouseEventArgs e)  
   bei Telerik.WinControls.RadElement.OnCLREventsRise(RoutedEventArgs args)  
   bei Telerik.WinControls.RadElement.OnBubbleEvent(RadElement sender, RoutedEventArgs args)  
   bei Telerik.WinControls.RadItem.OnBubbleEvent(RadElement sender, RoutedEventArgs args)  
   bei Telerik.WinControls.RadElement.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)  
   bei Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)  
   bei Telerik.WinControls.RadElement.RaiseRoutedEvent(RadElement sender, RoutedEventArgs args)  
   bei Telerik.WinControls.RadElement.DoMouseDown(MouseEventArgs e)  
   bei Telerik.WinControls.RadControl.OnMouseDown(MouseEventArgs e)  
   bei System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)  
   bei System.Windows.Forms.Control.WndProc(Message& m)  
   bei System.Windows.Forms.ScrollableControl.WndProc(Message& m)  
   bei Telerik.WinControls.RadControl.WndProc(Message& m)  
   bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)  
   bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)  
   bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)  
 
 

Is there a special way how I have to assign SelectedValue?

Regards

Manfred
0
Nikolay
Telerik team
answered on 11 Apr 2008, 02:21 PM
Hi Manfred Pohler,

Thank you for the question.

You cannot directly assign the Tag value to the SelectedValue value. However, you can make your own property and assign the Tag value to it. In addition, this should be done in the OnSelectedValueChanged method, so to be sure that the assignment process is finished when the SelectedIndexChanged event is fired.

I have attached a sample application, which demonstrates the approach.

If you need additional assistance, do not hesitate to contact me.

Greetings,
Nikolay
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
ManniAT
Top achievements
Rank 2
Answers by
Nikolay
Telerik team
ManniAT
Top achievements
Rank 2
Share this question
or