I have a checkbox with no text specified. Now 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.
Related
This is about ComboBox used in Winforms. I need to stop the selected item being heighlighted. I know I can get it done if I set the style of the combobox to ComboBoxStyle.DropDownList.
But I'm looking for a solution where I don't have to use that. Instead, at the moment what I have done is using ComboBoxStyle.DropDown.
I don't have any other option, because if I set it to DropDown, I have to deal with some other issue in my code. It's due to something else which I cannot avoid.
Can someone suggest an alternative pls ?
use the following code in your form's Paint event.
private void myForm_Paint(object sender, PaintEventArgs e)
{
comboBox1.SelectionLength = 0;
}
or pass focus to another control in your combo box selected index changed event:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Focus();
}
Like in web page using css we can show a div on mouse enter or hover, in the same way i want to show a panel on mouse enter event of a button, but i am unable to do this. I am trying like this.
private void btn2_MouseEnter(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = System.Drawing.Color.MistyRose; //this is executed on mouse enter
Point locationOnForm = btn.FindForm().PointToClient(
btn.Parent.PointToScreen(btn.Location));
Panel pnl = new Panel();
Label lbl = new Label();
lbl.Text = "anything";
pnl.Controls.Add(lbl);
pnl.Location = new Point(locationOnForm.X, locationOnForm.Y);
pnl.Size = new Size(500, 500);
pnl.BackColor = Color.SkyBlue;
pnl.Visible = true;
pnl.Show();
}
I am not getting how to solve this. I want to know that
1) Is this the right approach or there is any other way of doing this?
2) If this is ok then what is the mistake i am doing here ?
Thanks.
Don't create the panel on mouse enter, rather have the panel created already then just show and hide it.
private void button1_MouseEnter(object sender, EventArgs e)
{
panel1.Show();
}
You have to add the panel to the Form controls
Form1.Controls.Add(pnl);
If you plan to have a panel hover over the button like a <div> in Web, you will have to call
BringToFront() to ensure that the panel does not appear behind the button or other controls on the form -
pnl.BringToFront();
Also like the previous answer, it may be better to have a panel placed on the form already and just set visible to true or false as well as the panel location otherwise you may end up adding multiple panels to the Form controls.
If you plan on just showing plain text in the panel, it may be easier to use Tooltip control -
MSDN - Tooltip Control
I creating a custonmized box class (inherits from ComboBox). I don't want the text box to react to right mouse clicks. I can get rid of the context menu by setting this to null in ApplyTemplate, but right mouse clicks move the cursor. I tried hooking up PreviewMouseRightButtonDown in ApplyTemplate and setting Handled to True, but the event still gets through which is strange as it seems to work for the left click.
The cursor actually moves when the mouse button is released, so you want mark the MouseRightButtonUp event as handled. You could override OnMouseRightButtonUp:
protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
{
base.OnMouseRightButtonUp(e);
e.Handled = true;
}
Or you could attach a class handler to the MouseRightButtonUp event to mark it as handled:
static MyComboBox()
{
EventManager.RegisterClassHandler(
typeof(MyComboBox),
MouseRightButtonUpEvent,
new MouseButtonEventHandler(MyComboBox_MouseRightButtonUp));
}
private static void MyComboBox_MouseRightButtonUp(
object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
That will also prevent the context menu from being created without you having to set it to null explicitly.
I have a UserControl in my WPF application.
I want to call a click event and do some things when the user clicked the UserControl.
The problem is- the UserControl doesn't have a click event.
I searched on the web and found that you can use the MouseLeftButtonUp event.
I tried it- but it doesn't respond to my clicks.
You didn't write what you are trying to do but if you need a click event maybe you are writing some kind of button (the Button class is actually "something you can click" with the visual representation in a control template you can replace)
If you need a button with complex content inside - put your user control inside a button
If you need a button that doesn't look like a button write a custom control template for button
If you need a button with extra functionality subclass button, add the extra data/behavior in code and put the display XAML in a style.
I think for your needs PreviewMouseLeftButtonUp(Down) event is more suitable. Then you need to handle ClickCount for counting amount of clicks and then raise your own event, on which other controls will know, that your control is clicked. There are much more methods on handling click event. You should look at this msdn article and this
UPDATE to handle both Click and DoubleClick
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_myCustomUserControl.MouseLeftButtonUp += new MouseButtonEventHandler(_myCustomUserControl_MouseLeftButtonUp);
_myCustomUserControl.MouseDoubleClick += new MouseButtonEventHandler(_myCustomUserControl_MouseDoubleClick);
}
bool _doubleClicked;
void _myCustomUserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
_textBlock.Text = "Mouse left button clicked twice";
_doubleClicked = true;
e.Handled = true;
}
void _myCustomUserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_doubleClicked)
{
_doubleClicked = false;
return;
}
_textBlock.Text = "Mouse left button clicked once";
e.Handled = true;
}
}
To test this example name your control as _myCustomUserControl and add a TextBlock named _textBlock to your MainWindow.xaml
Why not just use MouseDown?
Put the event in the User Control and simply do this:
private void MyControl_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
MessageBox.Show("Clicked!");
}
}
I have a windows form with a toolstip in it with several buttons.
When the mouse is over a button of the toolstip then the toolstrip button starts to flash... looks like it gets and loses focus every second.
That results for the click to do nothing if the user click at the time that the button has no focus therefore the user has to click the button again and again util he gets the timing correct.
Does anyone knows anything about this?
I rally need some answers as soon as possible...
Thank you very much
I have found the reason...
The toolstrips in windows forms have by default the tooltips set to Auto and if the tooltip opens on the taskbar then the toolstrip loses focus.
The solution to this is to either disable the tooltips or to set it to manual and show the tooltip at another place.
Here is the code for showing the tool tip above the item manually:
private readonly ToolTip currentToolTip = new ToolTip();
private void ToolStripItem_MouseEnter(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Show(item.ToolTipText, item.Owner, item.Bounds.X, -20);
}
private void ToolStripItem_MouseLeave(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Hide(item.Owner);
}
You have to add the event handlers to all your ToolStripItems and set the ToolStrips' ShowItemToolTips to false.