如何使用框架4.5解决C#.net中的IndexOutofRange [英] How to resolve IndexOutofRange in C#.net with framework 4.5

查看:166
本文介绍了如何使用框架4.5解决C#.net中的IndexOutofRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了包含5个元素的checkedlistbox,我想显示用户在运行时检查过的这样一个元素,所以当我检查5个checkedlistbox中的第3个元素时,它将通过一个名为IndexOutofRange的异常。但是我在3号索引处检查(选中)的元素为什么会发生以及如何解决这种不必要的异常呢?



相关代码:(从OP的评论更新--Suvendu)

  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();
}
其他
{
im = im - 1 ;
this .Text = checkedListBox2.SelectedItems [im] .ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString( ));
}
最后 {}
}

解决方案

您必须自己检查:您允许用户输入一个数字,您希望该数字在选中列表框的范围内。如果用户输入749并且您只有五个项目,那么是 - 您将超出范围。



请检查您的用户输入。向用户报告问题,以便他可以纠正它们,而不是盲目地继续处理不良信息!

  private   void  button3_Click( object  sender,EventArgs e)
{
int im;
if (!int.TryParse(textBox1.Text, out im))
{
MessageBox.Show( string .Format( \{0} \不是有效数字。,textBox1.Text);
return ;
}
如果(im == 0 || im > checkedListBox2.SelectedItems.Count)
{
MessageBox.Show( string .Format( \{0} \未引用项目,textBox1.Text);
return ;
}
this .Text = checkedListBox2.SelectedItems [im - 1 ]。ToString();
}


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)

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 { }
}

解决方案

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!

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();
   }


这篇关于如何使用框架4.5解决C#.net中的IndexOutofRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆