Hi
Is it possible to detect/differentiate if a user clicked IN the checkbox area (really the square) or anywhere outside?
It seems that if you click on the Text to the right (by default) of the checkbox, that the Checked stated is modified.
Hello Eric,
Thank you for posting . I am able to reproduce the behavior you described using UltraCheckEditor and with MS checkbox also .
As a workaround you can use the an UltraCheckEditor with no text and then stick an UltraLabel next to it.
Let me know if you have any question.
Regards,
Hi Divya
Thanks for the reply.
I already have the UltraCheckEditor with a blank text but the Autosize property is false and there is a big empty space to the right of the square itself.
Because the control is rendered dynamically at runtime (form is built from a list of specified control) I cannot really play with all the properties I normally would.
This is why I am asking if it is possible to detect that really the square part of the UltraCheckEditor has been clicked or if the click was done outside of its bounds.
Another option to try would be to derive a class from UltraCheckedEditor and then override OnMouseDown and OnMouseUp.
Set up a sample application for your reference.
WindowsFormsApp98.zip
It kind of work but not always.
When the first is first shown, if you click on the text part, it is not toggling (as expected). You click on square and it toggles (still expected). But if click again on the Text part, it toggles (NOT expected).
But your sample give me an idea and I merged it with some test I was doing.
I think this code is working correctly:
Private Sub UltraCheckEditor2_BeforeCheckStateChanged(sender As Object, e As CancelEventArgs) Handles UltraCheckEditor2.BeforeCheckStateChanged Dim chk = TryCast(sender, UltraCheckEditor) Dim mainElement As UIElement = chk.UIElement Dim position = PointToScreen(chk.Location) Dim location As New Point(MousePosition.X - position.X, MousePosition.Y - position.Y) Dim elementUnderTheMouse = mainElement.ElementFromPoint(location) If elementUnderTheMouse Is Nothing Then TextBox1.Text = "where did you click?" Else TextBox1.Text = elementUnderTheMouse.ToString() If TypeOf elementUnderTheMouse Is TextUIElement Then e.Cancel = True End If End If End Sub
The problem with this approach is that if the user is using the keyboard and the mouse just happens to be over the text area of the CheckBox, the keyboard (spacebar) will be cancelled. Probably not likely to happen very often, but not ideal. I tried out what you described and I get the same behavior and it seems like that's happening because there are some cases where the CheckBox gets toggled in response to the Click event. So you can fix the original approach up by just add a check in the OnClick, as well as OnMouseUp and OnMouseDown.
protected override void OnClick(EventArgs e) { var mainElement = this.UIElement; Point location = new Point(MousePosition.X, MousePosition.Y); location = this.PointToClient(location); var elementUnderTheMouse = mainElement.ElementFromPoint(location); if (elementUnderTheMouse is TextUIElement) return; base.OnClick(e); }
Ack. Pretty sure it's the DoubleClick that's doing it now. So let's refactor the code a bit and handle that, as well:
private bool IsMouseOverTextElement() { var mainElement = this.UIElement; Point location = new Point(MousePosition.X, MousePosition.Y); location = this.PointToClient(location); return this.IsMouseOverTextElement(location); } private bool IsMouseOverTextElement(Point location) { var mainElement = this.UIElement; var elementUnderTheMouse = mainElement.ElementFromPoint(e.Location); if (elementUnderTheMouse is TextUIElement) return true; return false; } protected override void OnMouseDown(MouseEventArgs e) { if (IsMouseOverTextElement(e.Location)) { this.Focus(); return; } base.OnMouseDown(e); } protected override void OnClick(EventArgs e) { if (IsMouseOverTextElement()) return; base.OnClick(e); } protected override void OnDoubleClick(EventArgs e) { if (IsMouseOverTextElement()) return; base.OnDoubleClick(e); }
sorry. I was a bit too quick.
if you click twice (not necessarily double-click) on the text part, it will toggle