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;
}
Related
I am new to C# and was wondering how groupbox.Controls[i] and/oror panel.Controls[i] works?
I have a panel with a bunch of buttons in it to represent a keyboard. I change the color of the keyboard button when the key is pressed.
However, when I do keyboardPanel.Controls[2].BackColor = Color.Red;, my spacebar becomes red.
My spacebar is button55 and at TabIndex 54. Why is it my spacebar that turns red?
The reason that yoor space-bar gets red is simple: keyboardPanel.Controls[2] is the space-bar. please pay attention that the buttons are not necessarily added to the keyboardPanel.Controls list in the order they are named. Meaning: keyboardPanel.Controls[1] is not necessarily button1 and also button55 is not necessarily keyboardPanel.Controls[55].
Now, If you want to extract buttons by name you should use this:
keyboardPanel.Controls.Find("Button55" , true);
where "button55" is the name of that control and true goes for the option to search all the children.
But I think there is a simpler way to change the color. Using the Sender:
private void button_Click(object sender, EventArgs e)
{
Control btn = sender as Control;
btn.BackColor = Color.Red;
}
and make this method as the event handler of all the buttons' click event.
EDIT:
If you really want to re-arrange the controls in the GroupBox you should visit the the designer. The simple way to get to the designer is to right-click on InitializeComponent() in the constructor method of your form and choose Go To Definition.
There you will find the order that controls are added. something like below:
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.button1);
I my case Controls[0] is button2, Controls[1] is button3 and so on. You can re-arrange them like below:
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button3);
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'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.!
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 ...
}