C#のは、MouseHoverイベントハンドラ対リストボックスのMouseMoveをチェックします
-
20-09-2019 - |
質問
私はにCheckedListBox上のツールヒントとしてテキストファイルの内容を表示するには、次のMouseMove
イベントハンドラを使用していますし、各checkedListBoxItemにタグ付けされたテキスト・ファイル・オブジェクトがあります。
private void checkedListBox1_MouseMove(object sender, MouseEventArgs e)
{
int itemIndex = checkedListBox1.IndexFromPoint(new Point(e.X, e.Y));
if (itemIndex >= 0)
{
if (checkedListBox1.Items[itemIndex] != null)
{
TextFile tf = (TextFile)checkedListBox1.Items[itemIndex];
string subString = tf.JavaCode.Substring(0, 350);
toolTip1.ToolTipTitle = tf.FileInfo.FullName;
toolTip1.SetToolTip(checkedListBox1, subString + "\n... ... ...");
}
}
}
問題は、私のアプリケーションがあるためにCheckedListBoxに頻繁にマウスの動きをスローダウンされます。
は、別の方法として、私は思った、私はMouseHover
イベントとそのハンドラを使用する必要があります。しかし、私は私のmusePointerが現在あるcheckedListBoxItemいるかを見つけることができませんでした。このように:
private void checkedListBox1_MouseHover(object sender, EventArgs e)
{
if (sender != null)
{
CheckedListBox chk = (CheckedListBox)sender;
int index = chk.SelectedIndex;
if (chk != null)
{
TextFile tf = (TextFile)chk.SelectedItem;
string subString = tf.FileText.Substring(0, 350);
toolTip1.ToolTipTitle = tf.FileInfo.FullName;
toolTip1.SetToolTip(checkedListBox1, subString + "\n... ... ...");
}
}
}
ここでint index
が戻っている-1とchk.SelectedItem
はnull
を返してます。
この種の問題の解決策になることができますか?
解決
あなたはを使用することができMouseHoverイベントでCursor.Positionプロパティとクライアントの位置に変換し、それが内部に含まれているリスト項目かどうかを決定するためにIndexFromPoint()に渡す。
たとえばます。
Point ptCursor = Cursor.Position; ptCursor = PointToClient(ptCursor); int itemIndex=checkedTextBox1.IndexFromPoint(ptCursor); ... ...
これは、イベントパラメータでマウスの位置を与えられていないにも他のイベント、便利です。
他のヒント
問題はのSelectedItem <> checkedItem、選択手段は、他のバックグラウンドを持っているので、手段は左側にチェックしているチェックされます。
の代わりに
int index = chk.SelectedIndex;
あなたが使用する必要があります:
int itemIndex = checkedListBox1.IndexFromPoint(new Point(e.X, e.Y));
bool selected = checkedListBox1.GetItemChecked(itemIndex );
それを選択した場合、あなたが望むものを示して...
所属していません StackOverflow