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

GetSelect items in RadListBox

13 Answers 548 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.
Ingimar
Top achievements
Rank 1
Ingimar asked on 16 Oct 2008, 09:16 PM
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.

13 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 17 Oct 2008, 06:27 PM
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.
0
Ingimar Andresson
Top achievements
Rank 1
answered on 20 Oct 2008, 07:26 AM
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


0
Nick
Telerik team
answered on 20 Oct 2008, 04:23 PM
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.
0
Ingimar Andresson
Top achievements
Rank 1
answered on 20 Oct 2008, 04:38 PM
This did the trick :-) ....it was to close to see ;-)

Thank you for all the help.

Ragards

//Ingimar
0
Ingimar Andresson
Top achievements
Rank 1
answered on 20 Oct 2008, 08:51 PM
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
0
Ingimar Andresson
Top achievements
Rank 1
answered on 21 Oct 2008, 08:50 AM
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 :-)


0
Nick
Telerik team
answered on 21 Oct 2008, 11:39 AM
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.
0
SethSpearman
Top achievements
Rank 1
answered on 18 Dec 2008, 02:46 AM
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
0
Nick
Telerik team
answered on 18 Dec 2008, 04:46 PM
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.
0
SethSpearman
Top achievements
Rank 1
answered on 18 Dec 2008, 06:50 PM
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
0
Nick
Telerik team
answered on 19 Dec 2008, 03:43 PM
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.
0
mathieu cupryk
Top achievements
Rank 1
answered on 20 Nov 2012, 04:12 AM
just wondering how would you store this in a database table? the selected values.
0
Stefan
Telerik team
answered on 22 Nov 2012, 12:01 PM
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.
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Ingimar
Top achievements
Rank 1
Answers by
Nick
Telerik team
Ingimar Andresson
Top achievements
Rank 1
SethSpearman
Top achievements
Rank 1
mathieu cupryk
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or