开发者

highlight a win forms checkbox when cursor is inside it

I have a checkbox with no text specified. N开发者_Go百科ow whenever I tab down to this checkbox, it doesnot get highlighted.I even tried setting focus in checkbox_Enter() event. I checked for focus in this event and focus is there in this checkbox. How to get it highlighted so that user can know that the cursor is there in checkbox.


  • Try putting just a space into the Checkbox

Or

  • setting AutoSize to false
  • Setting the size of the Checkbox

Then there may be somewhere for WinForms to draw the focus ret.

Otherwise you have to to custom draw the Checkbox, or draw the focus rec round the Checkbox yourself.


Whatever you do it will not look that good, as users expect the focus rec to be round the label of the checkbox, and you wish to have a checkbox with no label.


I managed to do it by the below mentioned way

Use a panel.Push the checkbox inside that panel.Set the dimensions of a panel as such that it looks like a rectangle around the checkbox.In checkbox_enter() event set the border

BorderStyle.FixedSingle;

And in checkbox_Leave() event set the border again to

BorderStyle.None;

So this way it will tell user that focus in inside the checkbox.


To make the checkbox appear highlighted I had it change color on got focus and change back on lost focus.

this part is in the Form1.Designer.cs:

private void InitializeComponent()
    {...
    ckBox1.GotFocus += new System.EventHandler(checkBox_Highlight);
    ckBox1.LostFocus += new System.EventHandler(checkBox_EndHighlight);
    ckBox1.MouseHover += new System.EventHandler(checkBox_Highlight);
    ckBox1.MouseLeave += new System.EventHandler(checkBox_EndHighlight);
     }

This part is in the Form1:

 private void checkBox_Highlight(object sender, EventArgs e)
    {
        CheckBox control = (CheckBox)sender;
        control.FlatStyle = FlatStyle.Flat;
        control.ForeColor = Color.Blue;
    }

    private void checkBox_EndHighlight(object sender, EventArgs e)
    {
        CheckBox control = (CheckBox)sender;
        if (!control.Focused)
        {
            control.ForeColor = DefaultForeColor;
        }
    }


While tabbing, this puts a light blue shadow beneath the CheckBox on Enter and on Leave - at least on my Windows7:

    ...
    checkBox1.Enter += new System.EventHandler(check_Enter);
    checkBox1.Leave += new System.EventHandler(check_Leave);
    ...

    private void check_Enter(object sender, EventArgs e)
    {
        ((CheckBox)sender).BackColor = SystemColors.Highlight;
    }

    private void check_Leave(object sender, EventArgs e)
    {
        ((CheckBox)sender).BackColor = Color.Transparent;
    }


Actually the highlights comes on the text of the checkbox not the checkbox itself

So, if I was you, I would put any control in the background of my check box and give it the focus when my checkbox is focused, to have the same look of default controls highlights.

which will be shown to end users as checkbox highlights.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜