TextBoxes and NumericUpDowns have the odd property of not allowing you to deselect them once they are selected. When my user selects a NumericUpDown and clicks else-where on the form, the NumericUpDown should be deselected.
Unfortunately, this is not the case. Currently I am just handling the MouseDown event of all other controls on the form (like the panels and actual form itself) and just calling the Focus method of a random label to remove the focus from the NumericUpDown. However, this cannot be applied to menu items or scrollbars.
There must be a better way to do this. The user may want to scroll the panel instead of the NumericUpDown and intuitively click the Panel and then use the scroll-wheel, but currently that would scroll the NumericUpDown instead, since it still has focus.
Thanks for reading.
Edit: Problem still unsolved.
Normally Panel Control is a Non-Focusable control. Therefore clicking on Panel will NOT remove focus from TextBox or NumericUpDown Countrol.
The workaround can be, place a button on panel and move it away from view for example setting its x = -100 and y = -100. Do NOT set visible = false.
Now whenever user clicks on Panel (Panel_Click event) set focus (Button.Focus()) to that button. In this way panel will be scrollable through scroll-wheel.
Enclose the numeric box within a panel of some sort and then do
panel1.MouseHover += new EventHandler(panel1_MouseHover);
private void panel1_MouseHover(object sender, EventArgs e)
{
if (numericUpDown1.Focused)
{
panel1.Focus();
}
}
I tested it and it works.!
Related
I have created an Outlook add in which at some point displays a windows form with four buttons present on it. I am trying to default the focus to the first button, however the visual "selected" border will not appear around the button whenever I default this button as the focused one on start.
Any ideas how I could achieve this?
You can use either of these options to set the focus on a control in Load event of the form:
this.ActiveControl = this.button1;
this.button1.Select();
this.Show(); this.button1.Focus();.
You can use the Control.Focus method in the Load event of the form to set the focus on a control only after the Visible property of the form is set to true.
After selection the button, the border of the button will be drawn in a way that shows it's the active control, but the focus cues will not be drawn.
As a quick and dirty fix, you can send a Tab, and a Shift + Tab to your form:
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("+{TAB}");
If you are interested to change the standard behavior of Button to see focus cues when you select button in code or using mouse, you can create your own button inheriting Button and override its ShowFocusCues to return Focused value. You can read more about it here:
public class MyCustomButton : Button
{
protected override bool ShowFocusCues
{
get { return this.Focused; }
}
}
I have a problem .. I have an error list form (works as validation summary screen) that displays validation of controls that require to save data but have no values.
This form opened when validation occurs on controls in another form that has tab control contains all controls that have validation.
The problem is when I double click on Error List form, I need cursor focus on tab control that have this control and focus on the control itself
The result : focus happened on tab control only .. but I need to focus on the control also
Use Control.Focus() in your tab selected event handler.
Call Focus() to focus on the next control.
Step 1 : You need to handle the Enter event of the TabPage Control to perform the operations when TabPage gains the focus.
Step 2: You can call Select() function on Required control to gain the Focus.
Try This: if you want to gain the Focus of TextBox control in TabPage2 use this code
tabPage2.Enter += new System.EventHandler(this.tabPage2_Enter);
private void tabPage2_Enter(object sender, EventArgs e)
{
textBox1.Select();
}
I think the trick is to set socus on the tab page first, then set focus on the actual control you want to focus on.
What I was seeing is if the tab page was already selected setting focus to the control works fine. However, if the tab was programmatically activated then setting focus on the control alone does not work.
So this works for me reliably:
// first select and focus the tab
TabsResult.SelectedTab = tabRequest;
TabsResult.SelectedTab.Focus();
// then focus the control
txtRequestUrl.Focus();
I've had an annoying issue with focus for controls on a ToolBar in WPF. My toolbar has a CheckBox and a TextBox control next to eachother. If you click and edit the text in the text box, then click the check box, the text box steals the focus back after clicking the check box, and the check box state is not changed when the user clicks on it.
I have similar issues with text boxes all over my application, but I believe this is the simplest case to explain, and I hope that the problem will be a common issue across all my text boxes.
Does anyone know what might be going on here?
Hook up a handler to the checkbox's Click event (or even the PreviewMouseUp event), and set the event's Handled property to 'true'.
Did you try that?:
private void MyCheckBox_Click(object sender, RoutedEventArgs e)
{
MyCheckBox.Focus();
}
I am trying to use a C# WinForms combobox in the following manner:
a) regular use: if the "Down arrow" button is pressed, the dropdown area opens and the user selects the desired item - no problem there..
b) advanced use: if the user clicks into the editable are of the dropdown control, another dialog with advanced selection mechanisms opens.
Currently, I am calling the advanced dialog in the combobox's onclick event handler. However, now the regular use of the DropDownbox is not usable any more. The advanced dialog is opend even if you click only on the dropdown arrow.
I couldn't find another likely event that I could use, so I assume that I have to check in the eventhandler if the user clicked into the control's editable part or on the control's button. Any ideas how to do that effectivly?
Thanks for all hints.
There are two events of interest; DropDown and Click. Handle DropDown in the "normal" way. For Click, you might actually try handling MouseDown, which includes coordinates. Turn those coordinates (which are based on the cursor's position on the entire screen) into the position relative to the control, and if the mouse is currently on the arrow portion of the control, simply exit and allow the default behavior to happen. Otherwise, show the more advanced dropdown.
I did it (not so beautiful but it works)
void comboBox_Click(object sender, EventArgs e)
{
// Calculate cursor position
Point pointCursorLocal = this.PointToClient(Cursor.Position);
// Calculate rectangle of working area.
Rectangle rectangle = this.comboBox.Bounds;
rectangle.Size = new Size(rectangle.Size.Width - 10, rectangle.Height);
// Check them
if (rectangle.Contains(pointCursorLocal))
// CLICK ON WORKING AREA ...
}
How to change dropdown button in a ComboBox control (C#, Windows Forms)? I have a custom button, and I want to use it in the ComboBox instead of the default dropdown button.
I think Hans Passant solution is the way...
From here:
http://social.msdn.microsoft.com/forums/en-US/winformsdesigner/thread/5d65f987-834c-465f-a944-622831d4cfb0
You can create a UserControl, drag a
ComboBox and a Button onto it, make
the Button right over the ComboBox's
arrow button to make the arrow button
invisible, handle the Button's Paint
event to draw an arrow on it, this can
be done by calling
ComboBoxRenderer.DrawDropDownButton()
method (Notice: this method has a
limit, it needs the visual style being
enabled on the OS) or by drawing an
icon on it, or just drawing a small
triangle on it.
Then handle the Click event of the button to show the ComboBox's
DropDown, this can be done by
something like this
private void button1_Click(object sender, EventArgs e)
{
this.comboBox1.DroppedDown = true;
}