点击第二次时ArgumentOutOfRangeException为的ListViewItem [英] ArgumentOutOfRangeException for ListViewItem when clicking 2nd time

查看:316
本文介绍了点击第二次时ArgumentOutOfRangeException为的ListViewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,当我点击第二次的方法给出了ArgumentOutOfRangeException错误,并表示索引0不是一个有效的指标。

在第一次点击它的作品!?

ListView控件FullRowSelect设置为true。

我看到这种情况发生时pressing ALT键和控制键。如果我点击没有pressing它给出任何错误或如果点击2次点击之前的空行它不给错误。这些密钥

有没有使用这些组合键,然后点击更多然后一次的方法吗?

 私人无效MultipleToText(对象发件人,MouseEventArgs E)
    {
        如果(li.SelectedItems.Count℃,)
            返回;
        INT SetIndex = li.FocusedItem.Index;
        如果(Control.ModifierKeys ==(Keys.Alt | Keys.Control))
        {
            ListViewItem的LVI = li.SelectedItems [0]; //错误有发生
            的DialogResult解析度= MessageBox.Show(lvi.SubItems [0]。文本+将被删除继续。?,,MessageBoxButtons.YesNo);
            如果(RES == DialogResult.No)
                返回;
            li.Items.Remove(li.SelectedItems [0]);
            RemoveThisItem(SetIndex);
        }
        否则,如果(Control.ModifierKeys == Keys.None)
        {
            ListViewItem的LVI = li.SelectedItems [0];
            的DialogResult RES = MessageBox.Show(更改的条目+ lvi.SubItems [0]。文本+,,MessageBoxButtons.YesNo?);
            如果(RES == DialogResult.No)
                返回;
            li.Items.Remove(li.SelectedItems [0]);
            的for(int i = 0; I< 17; ++ I)
                _textBox [I]。文本= lvi.SubItems [I]。文本;
            RemoveThisItem(SetIndex);
        }
    }


解决方案

您是因为得到一个错误的第一个if语句将永远即使evalute为true(并返回),因为计数将永远也不能低于0。由于这个原因,您的列表是空仍尝试删除第一个元素,抛出发生ArgumentOutOfRangeException。

您的if语句而应该检查它是否等于0,因为这样的:

 如果(li.SelectedItems.Count == 0)

什么也没有发生,当你只是Alt键/控制preSS任何东西,因为你不处理比其他任何按键preSS,

事件

  Control.ModifierKeys == Keys.None

意味着没有键pssed相对于除Alt键/控制什么是pressed $ P $。

The method below when I click second time gives the ArgumentOutOfRangeException error, and says index 0 is not a valid index.

In first click it works!?

ListView FullRowSelect set true.

I see this happens while pressing ALT and CONTROL keys. If I click without pressing those keys it gives no error or If click an empty row before clicking 2nd time it does not give the error.

Is there a way to use those keys combination and clicking more then once ?

    private void MultipleToText(object sender, MouseEventArgs e)
    {
        if (li.SelectedItems.Count <0)
            return;
        int SetIndex = li.FocusedItem.Index;
        if (Control.ModifierKeys == (Keys.Alt|Keys.Control))
        {
            ListViewItem lvi = li.SelectedItems[0];   // Error happens there 
            DialogResult res = MessageBox.Show( lvi.SubItems[0].Text + " will be deleted. Continue ?", "", MessageBoxButtons.YesNo);
            if (res == DialogResult.No)
                return;
            li.Items.Remove(li.SelectedItems[0]);
            RemoveThisItem(SetIndex);
        }
        else if (Control.ModifierKeys == Keys.None)
        {
            ListViewItem lvi = li.SelectedItems[0];
            DialogResult res = MessageBox.Show("Change the entry " +  lvi.SubItems[0].Text + " ?", "", MessageBoxButtons.YesNo);
            if (res == DialogResult.No)
                return;
            li.Items.Remove(li.SelectedItems[0]);
            for (int i = 0; i < 17; ++i)
                _textBox[i].Text = lvi.SubItems[i].Text;
            RemoveThisItem(SetIndex);
        }
    }

解决方案

You are getting an error because your first if statement will never evalute to true (and return) since count will never go below 0. Due to this, even if your list is empty it still tries to delete the first element, throwing an ArgumentOutOfRangeException.

Your if statement should instead check if it is equal to 0, as such:

 if (li.SelectedItems.Count == 0)

Nothing happens when you press anything except Alt/Control because you are not handling the event for any key press other than those,

 Control.ModifierKeys == Keys.None

Means that no keys are pressed as opposed to anything except Alt/Control being pressed.

这篇关于点击第二次时ArgumentOutOfRangeException为的ListViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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