System.ArgumentOutOfRangeException [英] System.ArgumentOutOfRangeException

查看:470
本文介绍了System.ArgumentOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共静态字符串prev_SalesPerson ="" ;;

        private void listView1_SelectedIndexChanged(object sender,EventArgs e)

        {

            if(listView1.Items.Count == 0)

            {

                MessageBox.Show("  Salesperson Master ==> 13 : 没有销售员记录");

               转到endofproc;

            }


            



            MessageBox.Show(" Message is =>" + listView1.SelectedIndices [0] .ToString());
$


            textBox_SalespersonName.Text = listView1.SelectedItems [0] .SubItems [0] .Text;

            Salesperson.prev_SalesPerson = listView1.SelectedItems [0] .SubItems [0] .Text;
$


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; for(int i = 0; i< listView1.Items.Count; ++ i)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(listView1.Items [i] .SubItems [0] .Text.Trim()== textBox_SalespersonName.Text)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //MessageBox.Show(listView1.SelectedIndices [0] .ToString());

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; listView1.Items [i] .SubItems [0] .BackColor = Color.FromArgb(255,0,0,255);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; listView1.Items [i] .SubItems [0] .BackColor = Color.FromArgb(255,255,255,128);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; endofproc:; $
  &NBSP; &NBSP; &NBSP; }

请参阅上面的代码


我想要实现的是


当用户从中选择一条记录时listview1,所选列表项的背景颜色变为蓝色,并且在用户"导出列表视图1"之后。 ,背景色 记录应该坚持为蓝色。


但是当焦点转移到表单中的另一个文本框时,上面的代码无法实现这一点,而且它会给出 


错误消息是=>


System.Windows.Forms.dll <中出现未处理的"System.ArgumentOutOfRangeException"类型异常br />
其他信息:InvalidArgument =值'0'对'index'无效。


问候


Manoj Gokhale



解决方案

肥皂盒: 


要保护其他人不响应,请不要使用gotos。我最后一次使用代码中的goto回到了汇编语言时代。从来没有(有些人可能会争辩)goto是正确答案或无法在其他地方解决的案例。 Goto可以大大改变你的代码行为
。用返回替换goto,你已经完成了同样的事情,但它更清晰,更易读。


返回问题:


你做了一些可能错误的假设。但首先,无论何时发布错误,请确定它发生的确切代码行(调试器告诉您)以及异常详细信息。它会加快这个过程。


你假设因为有一个选定的索引必须有一个选定的项目,并且该项目至少有1个子项目。这可能是也可能不是。目前尚不清楚为什么你在静态领域坚持这一点,因为可能
不是正确的解决方案。 


试图理解你的代码,它看起来你正在抓住ListView中第一个选定项目的第一个子项目。然后,您枚举列表视图中的所有项目,查找具有相同名称的项目(可以有多于1个匹配的项目吗?)。
对于每个项目,您要设置子项目的背景颜色。


这里有一个更简单的代码,用于将当前所选LV项目(如果有)的背景颜色设置为不同颜色。但请注意,您所做的一切都是更改所选项目的背景颜色。 LV已经支持以不同的颜色显示所选项目
。所以我不确定你在这里想要完成什么。设置列表视图的FullRowSelect将选择整个项目。 

 private void listView1_SelectedIndexChanged(object sender,EventArgs e)
{
var listView =发件人为ListView;

//重置所有项目
foreach(listView.Items.OfType中的var项目< ListViewItem>())
{
if(item.Selected)
item.BackColor = Color.Blue;
else
item.BackColor = listView.BackColor;
};
}




public static string prev_SalesPerson = "";
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.Items.Count == 0)
            {
                MessageBox.Show("  Salesperson Master ==> 13  :  There are no Salesperson records ");
                goto endofproc;
            }

            

            MessageBox.Show("Message is => " + listView1.SelectedIndices[0].ToString());

            textBox_SalespersonName.Text = listView1.SelectedItems[0].SubItems[0].Text;
            Salesperson.prev_SalesPerson = listView1.SelectedItems[0].SubItems[0].Text;

            for (int i=0; i< listView1.Items.Count; ++i)
            {
                if (listView1.Items[i].SubItems[0].Text.Trim() == textBox_SalespersonName.Text)
                {
                    //MessageBox.Show(listView1.SelectedIndices[0].ToString());
                    listView1.Items[i].SubItems[0].BackColor = Color.FromArgb(255, 0, 0, 255);
                }
                else
                {
                    listView1.Items[i].SubItems[0].BackColor = Color.FromArgb(255, 255, 255, 128);
                }                
            }
                        
            endofproc:;
        }

Please see the code above

what I am trying to achieve is

When the user selects a record from the listview1, the backcolor of the listitem selected is made blue and after the user "navigates out of the listview1" , the backcolor  of the record should persist to be blue.

But this is not achieved by the above code when the focus is shifted to another text box in the form moreover it gives 

the error message is =>

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Additional information: InvalidArgument=Value of '0' is not valid for 'index'.

regards

Manoj Gokhale

解决方案

Soapbox: 

To save everyone else from responding, do not use gotos. The last time I used a goto in code was back in my assembly language days. There is never (some may argue) a case where a goto is the correct answer or cannot be solved elsewhere. Goto can dramatically change the behavior of your code. Replace that goto with a return and you've accomplished the same exact thing but it is far clearer and more readable.

Return to question:

You're making some assumptions that could be wrong. But firstly, whenever you post an error please identify the exact line of code it occurs on (the debugger tells you that) along with the exception details. It will speed up the process.

You are making an assumption that because there is a selected index that there must be a selected item and that the item has at least 1 subitem. This may or may not be true. It isn't clear why you're persisting that in a static field though as that probably isn't the correct solution. 

Trying to understand your code, it looks like you're grabbing the first subitem from the first selected item in a ListView. You then enumerate all the items in the listview looking for items with that same name (can there be more than 1 matching item??). For each item you're setting the background color of the subitem.

Here's simpler code that sets the background color of the currently selected LV item (if any) to a different color. But note that all you're effectively doing is changing the background color of the selected item. LV already supports showing selected items in a different color. So I'm not sure what you're trying to accomplish here. Setting the list view's FullRowSelect would select the entire item. 

private void listView1_SelectedIndexChanged ( object sender, EventArgs e )
{
    var listView = sender as ListView;
                        
    //Reset all items 
    foreach (var item in listView.Items.OfType<ListViewItem>())
    {
        if (item.Selected)
            item.BackColor = Color.Blue;
        else
            item.BackColor = listView.BackColor;
    };
}


这篇关于System.ArgumentOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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