4

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

行を取得するときのメソッド: 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 im のキーの 1 つをクリックすると、次の行で例外が発生します。

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: 

例外の意味は知っていますが、何を取得していて、どうすれば解決できますか?

たとえば、キーの矢印を上下に動かしてプログラムを実行しているときに最初に移動すると、リストボックスのキー間を移動し、そのうちの1つをクリックすると、例外が発生しません。例外は、プログラムを実行しているときに表示され、すぐにリストボックスのキーの1つをマウスでクリックすることです。

それらの間を移動してからそれらの1つをクリックしても問題ありませんが、最初にそれらの1つをクリックすると例外が発生します.

編集**

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

4

3 に答える 3

2

あなたはこれを行うことはできません:

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;
}
于 2013-09-10T16:42:55.267 に答える
1

さて、これを少し調べたところ、問題はおそらくあなたが私たちに提供したコードではなく、他の何かにあると結論付けることができます. WinFormでのマルチスレッドは違法であり、さまざまな例外が発生するため、マルチスレッドが進行しているという提案は受け入れません。

user2760148 が行う必要があるのは、小さいながらも完全な例を提供することです。新しいプロジェクトを作成し、可能な限り最小限のコードを実装して同じ結果を得ます。

最初のステップは、リストボックスのデータをハードコーディングし、その mouseDown イベント ハンドラーを用意することです。次に、問題が見つかるまでゆっくりとコードを追加します。自分で問題を見つける可能性があります... 少なくとも、問題の原因となっているステップを見つけることができます。現在、この例には「ノイズ」が多すぎて、特定の問題を簡単に見つけることができません。

于 2013-09-10T16:22:40.450 に答える