在Form1的构造函数中,我有以下代码:
我使用断点并查看文件是否存在:
文件内容如下:
文件包含9个键和关键词。左边是键,右边是关键词。
然后进入这个方法:
在执行到这一行代码:listBox1.DataSource = data; 时,它会跳转并执行以下事件:
最终,我看到data和listBox1.DataSource都包含9个项目。
经过这一切,程序正在运行。 我在listBox的右侧看到了项目、键和关键字。 当我单击listBox1中的一个键时,就会在该行引发异常。
我知道异常的含义,但我为什么会遇到它,该如何解决?
例如,当我使用箭头向上和向下键在listBox中移动并点击其中一个键时,不会出现任何异常。但是,当我运行程序并立即使用鼠标单击listBox中的一个键时,就会出现异常。
在它们之间移动然后单击其中一个是可以的,但是首先单击其中一个会引发异常。
编辑**
我现在看到,在进入listBox1_SelectedIndexChanged事件之前,会出现此事件istBox1鼠标按下:
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;
}
在执行到这一行代码: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个项目。
经过这一切,程序正在运行。 我在listBox的右侧看到了项目、键和关键字。 当我单击listBox1中的一个键时,就会在该行引发异常。
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中移动并点击其中一个键时,不会出现任何异常。但是,当我运行程序并立即使用鼠标单击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行。
label4.Text.Substring(start, index - start);
这段代码有点可疑。 - Alessandro D'AndriaListBox
控件进行多线程操作。是这样吗? - Sriram Sakthivel