トップ回答者
ListBox で 右クリックしてアイテムをひとつ選択させたい

質問
-
おせわになります、ずっちと申します。
現在、Visual Basic 2010 Express を用いて作成しております。
Form上にあるListBoxを右クリックしてContextMenuStripを表示させているのですが、
右クリックではアイテムの選択が変更されないことがわかりました。
ContextMenuStripではListBoxで右クリックしたアイテムに対する処理を記述しようとしていました。
右クリックでアイテムを選択させる方法をいろいろ考えていろいろ試してみたのですが、
どれもうまくいきませんでした。
どなたか良い方法はございませんでしょうか。
最悪、無理だとわかればほかのコントロールを使用しようと思います。
よろしくお願いいたします。
回答
-
ListBoxのMouseUpイベントで以下のようにされてみてはいかがでしょうか?
Private Sub listBox1_MouseUp(sender As Object, e As MouseEventArgs) If e.Button = System.Windows.Forms.MouseButtons.Right Then If listBox1.IndexFromPoint(e.Location) <> ListBox.NoMatches Then listBox1.SelectedItem = listBox1.Items(listBox1.IndexFromPoint(e.Location)) End If End If End Sub
★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://d.hatena.ne.jp/trapemiya/
すべての返信
-
ListBoxのMouseUpイベントで以下のようにされてみてはいかがでしょうか?
Private Sub listBox1_MouseUp(sender As Object, e As MouseEventArgs) If e.Button = System.Windows.Forms.MouseButtons.Right Then If listBox1.IndexFromPoint(e.Location) <> ListBox.NoMatches Then listBox1.SelectedItem = listBox1.Items(listBox1.IndexFromPoint(e.Location)) End If End If End Sub
★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://d.hatena.ne.jp/trapemiya/