Listbox bound with List<Class>

5 posts, 1 answers
  1. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 01 Apr 2008 Link to this post

    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
  2. Answer
    Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 03 Apr 2008 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 09 Apr 2008 Link to this post

    Thanks a lot - exactly what I was looking for!
  5. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 10 Apr 2008 Link to this post

    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
  6. Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 11 Apr 2008 Link to this post

    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
Back to Top
UI for WinForms is Visual Studio 2017 Ready