为什么我收到异常错误indexOutOfRangeException?

用户名

在Form1构造函数中,我有:

if (System.IO.File.Exists(keywords_path_file))
            {
                ListBoxLoadKeys(LocalyKeyWords, keywords_path_file);
            }
            else
            {
                fileExist = new StreamWriter(keywords_path_file);
                fileExist.Close();
                ListBoxLoadKeys(LocalyKeyWords, keywords_path_file);
            }

我使用一个断点,发现该文件存在:

C:\Users\bout0_000\AppData\Local\GatherLinks\GatherLinks\Keywords\Keywords.txt

文件内容为:

http://www.walla.co.il,walla
http://www.cnet.com,cnet
http://rotter.net/forum/scoops1/29961.shtml,rotter
http://vanessawest.tripod.com/crimescenephotos.html,VanessaWest
http://rotter.net/forum/scoops1/45227.shtml,scoops
https://www.google.com/search?q=live+cameras,live camera
https://www.google.com/search?q=rape+images&oq=+images&aqs=chrome..69i57.1661j0&sourceid=chrome&ie=UTF-8,hi
https://www.google.com/search?q=+images&um=1&ie=UTF-8&hl=en&tbm=isch&source=og&sa=N&tab=wi&ei=GqotUv2kA4OftAae94DoAg&biw=951&bih=457&sei=oaotUtDqM8WbtAag3IFg#hl=en&q=+and+&tbm=isch&um=1,chud
http://www.test.com,test

该文件包含9个键和关键字。左侧的按键右侧的关键字。

然后进入这种方法:

private void ListBoxLoadKeys(Dictionary<string, List<string>> dictionary, string FileName)
        {
            List<string> urls = new List<string>();
            using (StreamReader sr = new StreamReader(FileName))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    int i = line.Count();
                    tokens = line.Split(',');
                    dictionary.Add(tokens[0], tokens.Skip(1).ToList());
                    data.Add("Url: " + tokens[0] + " --- " + "Localy KeyWord: " + tokens[1]);
                    urls.Add(tokens[0]);
                }
            }
            listBox1.DataSource = data;
            listBox1.Tag = urls;
        }

在方法中,当它出现o时:listBox1.DataSource = data; 它跳跃并执行此事件:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listBox1.SelectedItem != null)
            {
                label4.Text = listBox1.SelectedItem.ToString();
                string startTag = "Url: ";
                string endTag = " ---";
                int startTagWidth = startTag.Length;
                int endTagWidth = endTag.Length;
                int index = 0;
                index = label4.Text.IndexOf(startTag, index);
                int start = index + startTagWidth;
                index = label4.Text.IndexOf(endTag, start + 1);
                string g = label4.Text.Substring(start, index - start);
                label4.Text = g;
                mainUrl = g;
            }
        }

最后,我看到data和listBox1.DataSource都包含9个项目。

毕竟,程序正在运行。我在列表框的右边看到项和关键字。一旦我单击listBox1 im中的键之一,就在行上得到了异常:

if (listBox1.SelectedItem != null)

在listBox1_SelectedIndexChanged事件中。

例外是:

指数数组的边界之外

System.IndexOutOfRangeException was unhandled
  HResult=-2146233080
  Message=Index was outside the bounds of the array.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.ListBox.ItemArray.GetItem(Int32 virtualIndex, Int32 stateMask)
       at System.Windows.Forms.ListBox.get_SelectedItem()
       at GatherLinks.Form1.listBox1_SelectedIndexChanged(Object sender, EventArgs e) in d:\C-Sharp\GatherLinks\GatherLinks-2\GatherLinks\GatherLinks\Form1.cs:line 543
       at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
       at System.Windows.Forms.ListBox.WmReflectCommand(Message& m)
       at System.Windows.Forms.ListBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
       at System.Windows.Forms.Control.WmCommand(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WmKillFocus(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ListBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: 

我知道异常的含义,但是我正在得到什么,我该如何解决?

例如,如果我在运行该程序时首先移动了箭头,则箭头在列表框的各个键之间上下移动,然后我单击其中一个,没有任何异常。当我运行程序并立即用鼠标单击listBox中的一个键时,首先出现异常。

在它们之间移动然后单击它们中的一个是可以的,但是单击它们中的一个首先是例外。

编辑**

我现在看到在将listBox1_SelectedIndexChanged事件移至istBox1鼠标事件之前,将其按下:

private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (Control.ModifierKeys == Keys.Control || ( Control.ModifierKeys == Keys.Control || e.Button == MouseButtons.Left))
            {
                listBox1.SelectionMode = SelectionMode.MultiExtended;
            }
            else if (e.Button == MouseButtons.Left)
            {
                listBox1.SelectionMode = SelectionMode.One;
            }
        }

做这行:

listBox1.SelectionMode = SelectionMode.MultiExtended;

然后返回到selectedindexchanged事件,并在行上抛出异常:

if (listBox1.SelectedItem != null)

那也是行号:543

拉尔斯科技

您不能这样做:

private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
  if (Control.ModifierKeys == Keys.Control || ( Control.ModifierKeys == Keys.Control || e.Button == MouseButtons.Left))
  {
    listBox1.SelectionMode = SelectionMode.MultiExtended;
  }
  else if (e.Button == MouseButtons.Left)
  {
    listBox1.SelectionMode = SelectionMode.One;
  }
}

我认为在MouseDown事件中更改该属性会破坏该窗口并再次重新创建它,这与在SelectedIndexChanged事件期间触发的内部信息搞混了。

只需注释掉该代码,然后在设计时确定ListBox控件应具有哪个SelectionMode。

您还应该确保获得正确的索引值,例如:

index = label4.Text.IndexOf(endTag, start + 1);
if (index > -1) {
  string g = label4.Text.Substring(start, index - start);
  label4.Text = g;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我收到异常错误indexOutOfRangeException?

来自分类Dev

为什么我收到Indy EIdOSSLUnderlyingCryptoError异常

来自分类Dev

为什么我收到此错误“线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:1”

来自分类Dev

为什么我收到错误消息:“线程“ main”中的异常java.lang.NullPointerException”

来自分类Dev

为什么我收到400错误的要求?

来自分类Dev

为什么我在'='上收到解析错误

来自分类Dev

为什么我收到此sql错误?

来自分类Dev

为什么我总是收到503错误?

来自分类Dev

为什么我收到“AttributeError”错误

来自分类Dev

为什么我收到错误“ClientConnectorError”?

来自分类Dev

为什么我收到此错误 TemplateDoesNotExist?

来自分类Dev

为什么我会收到类强制转换异常?

来自分类Dev

为什么我在DropDownListFor中的帖子中收到null异常?

来自分类Dev

为什么我在TempData中收到NullReference异常

来自分类Dev

为什么我在Android中收到Null Pointer异常

来自分类Dev

为什么我收到异常ArgumentOutOfRangeException:参数超出范围。

来自分类Dev

为什么我收到此 Python 脚本的连接拒绝异常?

来自分类Dev

为什么我收到找不到文件的异常?

来自分类Dev

为什么我没有收到询问超时异常?

来自分类Dev

为什么我会收到此异常 (gRCP)

来自分类Dev

为什么我在运行此代码时收到异常?

来自分类Dev

放心:为什么我会收到 IllegalStateException 异常?

来自分类Dev

为什么我总是收到错误消息或构建错误消息?

来自分类Dev

为什么我不断收到此错误“错误:意外类型”

来自分类Dev

分段错误 - 为什么我会收到此错误?

来自分类Dev

我看不到为什么我收到细分错误

来自分类Dev

为什么我总是收到位置参数错误?

来自分类Dev

为什么我在python中收到SQL语法错误

来自分类Dev

为什么我在角度2中收到“意外令牌<”错误?

Related 相关文章

  1. 1

    为什么我收到异常错误indexOutOfRangeException?

  2. 2

    为什么我收到Indy EIdOSSLUnderlyingCryptoError异常

  3. 3

    为什么我收到此错误“线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:1”

  4. 4

    为什么我收到错误消息:“线程“ main”中的异常java.lang.NullPointerException”

  5. 5

    为什么我收到400错误的要求?

  6. 6

    为什么我在'='上收到解析错误

  7. 7

    为什么我收到此sql错误?

  8. 8

    为什么我总是收到503错误?

  9. 9

    为什么我收到“AttributeError”错误

  10. 10

    为什么我收到错误“ClientConnectorError”?

  11. 11

    为什么我收到此错误 TemplateDoesNotExist?

  12. 12

    为什么我会收到类强制转换异常?

  13. 13

    为什么我在DropDownListFor中的帖子中收到null异常?

  14. 14

    为什么我在TempData中收到NullReference异常

  15. 15

    为什么我在Android中收到Null Pointer异常

  16. 16

    为什么我收到异常ArgumentOutOfRangeException:参数超出范围。

  17. 17

    为什么我收到此 Python 脚本的连接拒绝异常?

  18. 18

    为什么我收到找不到文件的异常?

  19. 19

    为什么我没有收到询问超时异常?

  20. 20

    为什么我会收到此异常 (gRCP)

  21. 21

    为什么我在运行此代码时收到异常?

  22. 22

    放心:为什么我会收到 IllegalStateException 异常?

  23. 23

    为什么我总是收到错误消息或构建错误消息?

  24. 24

    为什么我不断收到此错误“错误:意外类型”

  25. 25

    分段错误 - 为什么我会收到此错误?

  26. 26

    我看不到为什么我收到细分错误

  27. 27

    为什么我总是收到位置参数错误?

  28. 28

    为什么我在python中收到SQL语法错误

  29. 29

    为什么我在角度2中收到“意外令牌<”错误?

热门标签

归档