GetSelect items in RadListBox

14 posts, 0 answers
  1. Ingimar
    Ingimar avatar
    1 posts
    Member since:
    May 2008

    Posted 16 Oct 2008 Link to this post

    Hi

    I am new to Yelerik controls and need some help..

    I am having a problem looping trough selected items in a ListBox and get the selected value of the item..

    Usually i would do this in a foreach loop:

    private ArrayList GetItems() 
            { 
                ArrayList retVal = new ArrayList(); 
                for(int i = 0; i < listBoxLanguages.Items.Count; i++) 
                { 
                    if(listBoxLanguages.GetSelected(i) == true
                        retVal.Add(i); 
                } 
                return retVal; 
            } 

    but this does not work in RadListBox control...... GetSelected method does not exist :-(

    Any help in appreciated.
  2. Nick
    Admin
    Nick avatar
    767 posts

    Posted 17 Oct 2008 Link to this post

    Hello Ingimar,

    Thank you for your question.

    Use the following code to get the indexes of the selected items:

    foreach (int i in this.radListBox1.SelectedIndices) {  
                    MessageBox.Show(i.ToString());  

    Do not hesitate to write me back if you need further assistance.

    Kind regards,
    Nick
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ingimar Andresson
    Ingimar Andresson avatar
    10 posts
    Member since:
    Jun 2008

    Posted 20 Oct 2008 Link to this post

    Hi

    Thanks for your reply.
    Maybe i was unclear in my thread..sorry. I will try again...

    I populate my RadListBox from BLL class..

            private void lbLanguages_Initialized(object sender, EventArgs e) 
            { 
                this.lbLanguages.DataSource = languages.GetLanguages(); 
                this.lbLanguages.DisplayMember = "description"
                this.lbLanguages.ValueMember = "langID"
            } 

    this works fine..

    This data is taken from a DB table in my DB trough a DataSet witch has a Primary key witch is int and starts with the value 1. (= first selection im my listbox).

    Now when i run your code the first selection gets the value 0 not 1

    What i need is to get the ValueMember value of the selected items in my listbox..

    I have checked out the following thread witch might be a soulution to my problem but I am not able to get it to work..

    http://www.telerik.com/community/forums/thread/b311D-bcggkg.aspx

    any suggestions??

    Regards Ingimar


  5. Nick
    Admin
    Nick avatar
    767 posts

    Posted 20 Oct 2008 Link to this post

    Hi Ingimar Andresson,

    Please refer to the following sample code:

     public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
                  
                  
            }  
            BindingList<A> l;  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                
                 
                l = new BindingList<A>();  
                A a = new A();  
                a.C = 1;  
                a.B = 11;  
                l.Add(a);  
     
                A a1 = new A();  
                a1.B = 22;  
                a1.C = 2;  
     
                l.Add(a1);  
                this.radListBox1.DisplayMember = "B"//database column name if you are using a database  
                this.radListBox1.ValueMember = "C"//database column name if you are using a database  
     
     
                this.radListBox1.DataSource = l;  
     
     
            }  
     
            private void radButton1_Click(object sender, EventArgs e)  
            {  
                foreach (RadListBoxItem i in this.radListBox1.SelectedItems) {  
                    MessageBox.Show("value member: " + i.Value.ToString() + " display member: "+i.Text.ToString());  
                      
                }  
            }  
     
              
        }  
     
        class A {  
            int b;  
     
            public int B  
            {  
                get { return b; }  
                set { b = value; }  
            }  
            int c;  
     
            public int C  
            {  
                get { return c; }  
                set { c = value; }  
            }  
     
        } 


    In the previous code sample I showed how to get the idices of the selected items which is unrelated to DisplayMember, ValueMember. So this is a third property of each row containing information about the position of the item in the listbox - positions start from zero. 

    The link you send me is applicable if you want to cast the selected item to your custom object. There is no problem getting the ValueMember or the DisplayMember.

    Please do not hesitate to contact me if I have still left your question unanswered.

     
    Greetings,
    Nick
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Ingimar Andresson
    Ingimar Andresson avatar
    10 posts
    Member since:
    Jun 2008

    Posted 20 Oct 2008 Link to this post

    This did the trick :-) ....it was to close to see ;-)

    Thank you for all the help.

    Ragards

    //Ingimar
  7. Ingimar Andresson
    Ingimar Andresson avatar
    10 posts
    Member since:
    Jun 2008

    Posted 20 Oct 2008 Link to this post

    I have small problem to solve.....is there any way to convert RadListBoxItem to int value?

    I neew to put the data into DB table witch takes the selected ValueMember as int.

    Thank you for all the help.

    Ragards

    //Ingimar
  8. Ingimar Andresson
    Ingimar Andresson avatar
    10 posts
    Member since:
    Jun 2008

    Posted 21 Oct 2008 Link to this post

    Ok, problem solved... RadListItem is a object
    Converted with following code:

                            byte lang; 
                            object lang1; 
                             
                            foreach (RadListBoxItem i in lbLanguages.SelectedItems) 
                            { 
                                lang1 = i.Value; 
                                lang = (byte)lang1; 
                                userLang.AddUserLanguages(userNew.ProviderUserKey, lang); 
                               //MessageBox.Show("ValueMember " + i.Value.ToString() + "Display Member " + i.Text.ToString()); 
                            } 

    Thanks :-)


  9. Nick
    Admin
    Nick avatar
    767 posts

    Posted 21 Oct 2008 Link to this post

    Hi Ingimar,

    Thank you for contacting me back.

    You cannot convert RadListBoxItem to int, and I hope there is no need to do that. You may use the Value property and cast it to int if it is int in your database:

    (int)(((RadListBoxItem) this.radListBox1.SelectedItem).Value 

    I hope this helps and is an answer to your question. Don't hesitate to contact me back if I have missed something or you need further assistance. 

    Sincerely yours,
    Nick
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  10. SethSpearman
    SethSpearman avatar
    4 posts
    Member since:
    Apr 2007

    Posted 17 Dec 2008 Link to this post

    I know how to do that...but what about this.

    Let's say have bound my RadListBox to a collection class. One of the properties of the child members of the collection is called IsSelected.  Is there an automated way to set that properrty on the child when the item is selected in the RadListBox.  Assume the multiSelect is enabled on the RadListBox. 

    Thanks.

    Seth
  11. Nick
    Admin
    Nick avatar
    767 posts

    Posted 18 Dec 2008 Link to this post

    Hello SethSpearman,

    Thank you for contacting us.

    You have to use the ValueMember(s) to find your objects and modify their properties:

    foreach (RadListBoxItem i in this.radListBox1.SelectedItems) {   
        modify(i.Value);   
                       

    Do not hesitate to write me back if you have more questions.

    All the best,
    Nick
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  12. SethSpearman
    SethSpearman avatar
    4 posts
    Member since:
    Apr 2007

    Posted 18 Dec 2008 Link to this post

    Specifically, then, how would I mark one of the objects as Selected.

    'let's say I want to mark every other item in the list as SELECTED.
    foreach (radListBoxItem i in this.radListBox1.Items) {

    /* how do I mark i as selected...or see if it is selected  */

    }

    Seth
  13. Nick
    Admin
    Nick avatar
    767 posts

    Posted 19 Dec 2008 Link to this post

    Hi SethSpearman,

    Thank you for contacting me back.

    You may use the following code to iterate through all rows and check if a row is selected:

    private void radButton1_Click(object sender, EventArgs e) 
            { 
                foreach (RadListBoxItem i in this.radListBox1.Items) 
                { 
                    if (this.radListBox1.SelectedItems.IndexOf(i) == -1) 
                    { 
                        Console.WriteLine("not selected"); 
                    } 
                    else 
                    { 
                        Console.WriteLine("selected"); 
                    } 
                }             
            } 

    A slight modification of the above code selects every row not being currently selected:

     private void radButton1_Click(object sender, EventArgs e) 
            { 
                foreach (RadListBoxItem i in this.radListBox1.Items) 
                { 
                    if (this.radListBox1.SelectedItems.IndexOf(i) == -1) 
                    { 
                        
                        this.radListBox1.SelectedItems.Add(i); 
                    } 
                    else 
                    { 
                        Console.WriteLine("selected"); 
                    } 
                }     
                 
            } 

    Do not hesitate to write me back if you have more questions.

    Best wishes,
    Nick
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  14. mathieu cupryk
    mathieu cupryk avatar
    35 posts
    Member since:
    Jul 2012

    Posted 19 Nov 2012 Link to this post

    just wondering how would you store this in a database table? the selected values.
  15. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 22 Nov 2012 Link to this post

    Hello Mathieu ,

    Thank you for writing.

    Please note that RadListBox is no longer part of our suite. Its ancestor is RadListControl.

    In regards to your question at hand, saving the selected index in a database is not something that the UI control should take care of. I would suggest that you address your question in the appropriate MSDN forums.

    I hope this helps.
     
    Kind regards,
    Stefan
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top
UI for WinForms is Visual Studio 2017 Ready