C#のは、MouseHoverイベントハンドラ対リストボックスのMouseMoveをチェックします

StackOverflow https://stackoverflow.com/questions/1550037

質問

私はに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.SelectedItemnullを返してます。

この種の問題の解決策になることができますか?

役に立ちましたか?

解決

あなたはを使用することができ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 );

それを選択した場合、あなたが望むものを示して...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top