Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used a checkedlistbox which contains 5 elements and I want to display such a element that have been checked by the user at runtime, so When I checked 3rd element from out of 5 the checkedlistbox it will through a exception called IndexOutofRange. but the element which is checked (selected ) by me that exists at index no 3 then why it will be happen and how to resolve such unwanted exception?

Relevent Code: (updated from OP's comment --Suvendu)
C#
private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = checkedListBox2.SelectedIndex.ToString();

//button2.Text += " " + checkedListBox1.SelectedItems[Convert.ToInt16(textBox1.Text)];
}

private void button3_Click(object sender, EventArgs e)
{
   try
   {
     int im = Convert.ToInt16(textBox1.Text);
     if (im == 0)
     {
        //MessageBox.Show(checkedListBox2.SelectedItems[im].ToString());
        this.Text = checkedListBox2.SelectedItems[im].ToString();
     }
     else
     {
        im = im - 1;
        this.Text = checkedListBox2.SelectedItems[im].ToString();
     }
   }
   catch (Exception ex)
   { 
     MessageBox.Show(ex.ToString()); 
   }
   finally { }
}
Posted
Updated 30-Jul-15 22:05pm
v2
Comments
Suvendu Shekhar Giri 31-Jul-15 2:21am    
Share the relevant code block.
Arz Muhammad 31-Jul-15 3:40am    
private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = checkedListBox2.SelectedIndex.ToString();

//button2.Text += " " + checkedListBox1.SelectedItems[Convert.ToInt16(textBox1.Text)];
}

private void button3_Click(object sender, EventArgs e)
{
try
{

int im = Convert.ToInt16(textBox1.Text);
if (im == 0)
{
//MessageBox.Show(checkedListBox2.SelectedItems[im].ToString());
this.Text = checkedListBox2.SelectedItems[im].ToString();
}
else
{
im = im - 1;
this.Text = checkedListBox2.SelectedItems[im].ToString();
}
}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
finally { }
}
Richard MacCutchan 31-Jul-15 3:54am    
Use your debugger to see what is the value of your index at the time of the error, and compare that with the number of elements in the SelectedItems set.
CPallini 31-Jul-15 2:37am    
Wait a moment, I need to activate my super powers to have a look at your code by telepathy.
stibee 31-Jul-15 3:58am    
Did you use CheckedItems instead of SelectedItems once?

1 solution

You have got to check yourself: you are allowing a user to enter a number, which you hope is within the range of the checked list box. If the user enters "749" and you only have five items, then yes - you will be out of range.

So check your user input. Report problems to the user so he can correct them instead of blindly continuing to work with bad information!
C#
private void button3_Click(object sender, EventArgs e)
   {
   int im;
   if (!int.TryParse(textBox1.Text, out im))
       {
       MessageBox.Show(string.Format("\"{0}\" is not a valid number.", textBox1.Text);
       return;
       }
   if (im == 0 || im > checkedListBox2.SelectedItems.Count)
       {
       MessageBox.Show(string.Format("\"{0}\" is does not refer to an item", textBox1.Text);
       return;
       }
   this.Text = checkedListBox2.SelectedItems[im - 1].ToString();
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900