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 ...
}
Related
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();
}
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.!
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;
}
I have a custom UserControl. I want to use it in a few different products, so I want something that can be implemented inside of the UserControl itself. I want to know when the user has clicked outside of the bounds of the UserControl so that I can hide it, similar to a ComboBox. How can I do that?
I tried handling the click event, but it only seems to fire if the click occured within the bounds of the control.
That's what the Capture property is designed to do. Set it to true and all mouse messages are routed to your control, even if it moves out of the window bounds. Check the e.Location property in the MouseDown event.
Hm, you may be able to accomplish what you want by listening to the GotFocus/LostFocus events. ComboBoxes give the drop downs focus when they open and close them when they lose focus.
do this
Select all controls on your form including form
In Property Window select MouseClick event
Now enter below Code in Common_MouseClick
Code:
if (!sender.Equals(yourControl))
{
yourControl.Visible=false;
}
I'm using NotifyIcon (System tray icon) in my WinForms application. I also have a ContextMenuStrip assigned to it. When user right clicks on NotifyIcon this ContextMenuStrip pops up.
These are the only events that NotifyIcon has.
Click
DoubleClick
MouseClick
MouseDoubleClick
MouseDown
MouseMove
MouseUp
This contextMenuStrip items (ToolStripMenuItem) are dynamically generated. I mean there are few default items like 'About','Exit','Help' etc.. but other items are dynamically generated and inserted into this menu when user right clicks on it. I'm generating items and inserting into contextMenuStrip in Click event handler
Now, I've two problems:
Problem is for an instant its showing the default menustrip and then my Click event handler executes and new update menu pops up. How can I avoid this? I don't want to see the default menu at all. In other words I need to override the default behavior.
Other problem is since I'm handling the Click event (because I didn't find RightClick event) the left button click also is handled by the same handler. I want to do different things (like show application windows) on left click and show dynamically generated contextMenuStrip on right click. How to acheive this?
Why are there two different events like Click & MouseClick? What else would we click with? Aren't these two interdependent. I mean when ever there is a MouseClick there is also a Click.
If you can point me to some examples. That would be great!
Item #1) The ContextMenuStrip has events that allows you to handle any dynamic creation of menu items before the menu is displayed. See the Opening and Opened events.
Item #2) Use the MouseEventArgs parameter to inspect the mouse-state when event was raised.
Item #3) Depending on the control, Click and MouseClick can be different. Take buttons for instance. When a button has focus, the "Click" event is raised when the user presses the Space or Enter key. In most cases, a MouseClick generates a Click event.
I'm the OP. I guess I've achieved it.
notifyIcon.MouseDown += new MouseEventHandler(notifyIcon_MouseDown);
and
static void notifyIcon_MouseDown(object sender, MouseEventArgs e)
{
NotifyIcon notifyIcon = sender as NotifyIcon;
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("Left Button Clicked"); // & do what ever you want
}
else
{
updateMenuItems(notifyIcon.ContextMenuStrip);
}
}