My combobox allows typing into the text portion so the only way to get the dropdown list is to click the button. However, since this will be used on touchscreen devices, it is hard to click it when it is this 'thin'.
Is there any way to increase the width of the combobox button?
One alternative to resizing the button would be to set the DroppedDown property to true inside the Click event. This will show the drop down list when the user clicks inside the edit area of the ComboBox, effectively extending the button area to the whole control:
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
If you want to customize dropdown button (the size of the arrow and the size of the button are completely in our control), there is a class called ComboBoxRenderer.
Here you have complete example.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.comboboxrenderer?view=netframework-4.7.2
It is supported from .Net2.0.
You should create a custom ComboBox control and call DrawDropDownButton of the comboboxrenderer in the paint event.
In the above link(example) arrowSize and arrowRectangle are two variables which helps in deciding your dropdown button size, along with below static function (of ComboBoxRenderer) call in overridden paint event.
ComboBoxRenderer.DrawDropDownButton(e.Graphics, arrowRectangle,arrowState);
Related
I'm creating a form and I want the text of a button to change depending on if any radio button in the whole form has been clicked on, additionally some radio buttons are in group boxes so do not affect other radio buttons. The only way I can think of to do this is to input code into every method that is called for when each radio button is clicked. Is there any other way of doing it?
you can create one method for click event for one radioButton only and make all radioButtons share this click event and i will suppose that you will make Button text like radioButton text which you clicked and inside this method write this line...
private void radioButton1_Click(object sender,EventArgs e) {
button1.Text = (RadioButton(sender)).Text;
}
I have tried to create few DropDownButtons inside a statusStrip in C# windown form.
It's normal that when I just move the cursor to the button, it will not drop down the items, it only drop down the items only when I clicked the DropDownButton.
However the issue is after I clicked on DropDownButton1, it drops down the items, but when I move the cursor to another DropDownButton2, the items of DropDownButton2 will not drop down!!
How can I set the items of DropDownButton2 automatically drop down when I move mouse after clicking DropDownButton1? Just like the general application?
Thanks all!
you can use the ComboBox.DroppedDown property,
set it to true on MouseHover event
Try something like this:
this.dropDown1.MouseHover += new System.EventHandler(this.dropDown1_MouseHover);
private void dropDown1_MouseHover(object sender, System.EventArgs e)
{
//Set the dropdown1 to dropped
dropDown1.DroppedDown = true;
//Set the other dropdown to undropped
dropDown2.DroppedDown = false;
}
For more info add more code!
How would I make two buttons appear the same when one is hovered over?
Picture of the buttons I want to be shown are here:
http://i.stack.imgur.com/b4P6B.png
How would I make the button with the green image in the center appear as the same style (colors, borders, etc...) when the Sign On one has been hovered over / clicked?
I'm using Windows Forms.
This can be done using event handlers on mouse over/out, but frankly the right choice is to make a usercontrol containing both buttons and use that instead of the two...
Simply add handler for MouseEnter event on your "sign on" button - all you have to do in this very handler then, is changing second button's styles (implementing MouseLeave might be useful too - to revert second button to it's original style).
Code sample:
this.ButtonSignOn.MouseEnter += this.ChangeOtherButton;
this.ButtonSingOn.MouseLeave += this.RevertOtherButtonChanges;
// later on
private void ChangeOtherButton(object sender, EventArgs e)
{
this.OtherButton.ForeColor = Colors.Red;
this.OtherButton.BackColor = Color.Blue;
// more styling ...
}
// mostly same stuff when reverting changes
You could refactor those 2 handlers into one, and simply passing colors, fonts and other styles as you go... but this should be enough anyways.
I have a WinForm with 3 group boxes, one with combo boxes, and two with radio buttons. I set all of them and their children controls to "TabStop = false", but when I cycle with TAB, the currently selected radio button in each of the last two group boxes gets focused.
If there's no way to change this behavior, what would be a good event to catch and move the focus away? I can't find an "OnFocus" event.
The solution is to set one method (code below) to handle the "Enter" event of every radio button in the form (if that's what you wish).
Actually, I only did it for the radio buttons of the first group box and it worked, the second group box's radio buttons don't get focus, even though their "Enter" events are not handled. This is not the behavior you would have expected.
private void radiobuttonXGroup1_Enter(object sender, EventArgs e)
{
SomeOtherControl.Focus();
}
In the *.Designer.cs file you edit every Enter event (for each radio button) to point to one event handler (the above method).
this.radiobutton1Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton2Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton3Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
Setting the TabStop to False on a RadioButton to prevent tabbing to the control works until you actully select the radio button without any additional overrides like suggested by #msergeant.
EDIT
The following code prevents the code from getting a tab key event:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
radioButton1.TabStop = false;
}
Radio buttons behave differently with respect to Tab from other controls in that they work in sets or groups based on setting the tab index or placing then radio buttons in a group box.
The MSDN documentation for RadioButton.TabStop states "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code". Which basically means, "This isn't going to work how you expect it to".
With that said, the Enter event will fire when the button receives the focus. You can try to use that to move focus to another control.
I have an application where I am trying to mimic the "soft description" textboxes like those found for the tags and title locations on this site.
The way I've done this is essentially to create my textbox, and depending on what happens when the mouse pointer enters or leaves the control, it updates the content of the textbox to get the effect.
The problem is what when my form is first shown, the mouse cursor immediately jumps into the first textbox, which removes the title telling the user what the textbox is for.
If I turn off AcceptTab on the textbox, then everything works as expected, but the user loses the ability to tab into the textbox.
Is there a way to turn off this automatic selection of the textbox?
Could you this.Focus() on the form itself, or on some label control?
Bit late but a perfect solution is to select the form on load of form.
Adding this line to the constructor will give the expecting result.
this.Select();
But while using multi thread controls like OpenFileDialog if u want to unfocus/deselect text-box this.Select() was not working so I selected a button in the form using.
button1.Select();
The TabIndex property controls what order things will tab in, and on load, focus goes to the first control (ordered by TabIndex) that has AcceptTab as true. You can change the ordering so that the control that you want the user focus to start in is lowest (and have tabs work cycle through controls as you'd expect).
Alternatively, as Jason suggested, you could simply call Focus() on whatever control or the form itself in the FormLoad event.
I used a variant on Jason's technique. First, I created a dummy textbox with tabindex 0. That way, when the form is shown, that textbox will be selected. Next, I made the dummy textbox have zero width, so that it has no visible component.
However, once the form is loaded, I don't want the user to be able to tab over to the "nonexistant" textbox. Therefore, I added these two bits:
//These functions prevent the textboxes from being implicitly selected.
private void dummyBox_Leave(object sender, EventArgs e)
{
dummyBox.TabStop = false;
}
private void Main_Enter(object sender, EventArgs e)
{
dummyBox.TabStop = true;
dummyBox.Select();
}
Where Main is the name of my form.
Hope this helps someone.
Billy3