チェックリストボックスで項目をクリックすると、強調表示されます。このハイライト効果を防ぐにはどうすればよいですか?
SelectedIndexChanged イベントにフックして選択をクリアすることはできますが、ハイライトは引き続き発生し、ブリップが表示されます。実際、マウス クリックを押したままにし、チェック ボックス領域をクリックした後に決して離さない場合、マウス ボタンを離すまで、選択は強調表示されたままになります。私は基本的に、このハイライト効果を完全に取り除きたいと思っています。
チェックリストボックスで項目をクリックすると、強調表示されます。このハイライト効果を防ぐにはどうすればよいですか?
SelectedIndexChanged イベントにフックして選択をクリアすることはできますが、ハイライトは引き続き発生し、ブリップが表示されます。実際、マウス クリックを押したままにし、チェック ボックス領域をクリックした後に決して離さない場合、マウス ボタンを離すまで、選択は強調表示されたままになります。私は基本的に、このハイライト効果を完全に取り除きたいと思っています。
以下を使用します。
private void checkedListBox1__SelectedIndexChanged(object sender, EventArgs e)
{
checkedListBox1.ClearSelected();
}
これは、まだ点線のビットを取得することとは別にそれを行います。
this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;
チェックボックスをクリックすることはできませんが...そのため、次のような操作を行う必要があります。
private void checkedListBox1_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemRectangle(i).Contains(checkedListBox1.PointToClient(MousePosition)))
{
switch (checkedListBox1.GetItemCheckState(i))
{
case CheckState.Checked:
checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
break;
case CheckState.Indeterminate:
case CheckState.Unchecked:
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
break;
}
}
}
}
それがあなたの後にするものではない場合..あなたはいつでもあなた自身のものを作ることができます。そのかなり単純なコントロール。
SelectionMode を None に設定すると、Click イベントを実装する必要があるなど、いくつかの奇妙な問題が発生します。SelectionMode を single に設定したままにして、CheckedListBox を OnDrawItem だけでオーバーライドするクラスを作成できます。選択した外観をオフにするには、選択状態をオフにして、必要な色に設定する必要があることに注意してください。ここで行ったように、コントロールから元の色を取得できます。これは私が思いついたものであり、あなたが望むように表示することから始める必要があります.
public partial class EnhancedCheckedListBox : CheckedListBox
{
/// <summary>Overrides the OnDrawItem for the CheckedListBox so that we can customize how the items are drawn.</summary>
/// <param name="e">The System.Windows.Forms.DrawItemEventArgs object with the details</param>
/// <remarks>A CheckedListBox can only have one item selected at a time and it's always the item in focus.
/// So, don't draw an item as selected since the selection colors are hideous.
/// Just use the focus rect to indicate the selected item.</remarks>
protected override void OnDrawItem(DrawItemEventArgs e)
{
Color foreColor = this.ForeColor;
Color backColor = this.BackColor;
DrawItemState s2 = e.State;
//If the item is in focus, then it should always have the focus rect.
//Sometimes it comes in with Focus and NoFocusRect.
//This is annoying and the user can't tell where their focus is, so give it the rect.
if ((s2 & DrawItemState.Focus) == DrawItemState.Focus)
{
s2 &= ~DrawItemState.NoFocusRect;
}
//Turn off the selected state. Note that the color has to be overridden as well, but I just did that for all drawing since I want them to match.
if ((s2 & DrawItemState.Selected) == DrawItemState.Selected)
{
s2 &= ~DrawItemState.Selected;
}
//Console.WriteLine("Draw " + e.Bounds + e.State + " --> " + s2);
//Compile the new drawing args and let the base draw the item.
DrawItemEventArgs e2 = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, s2, foreColor, backColor);
base.OnDrawItem(e2);
}
}
クールな追加で、Hath からの回答のすべてのコードを
checkedListBox1_MouseMove(object sender, MouseEventArgs e)
マウスボタンがスイッチにビットインした場合に追加します
case CheckState.Checked:
if (e.Button == MouseButtons.Right)
{
checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
}
break;
と
case CheckState.Unchecked:
if (e.Button == MouseButtons.Left)
{
checkedListBox1.SetItemCheckState(i, CheckState.Checked);
}
break;
左ボタンを押したままマウスを動かし、右ボタンで強調表示を解除すると、強調表示された項目がチェックされます