Set C# Button Style to another Button when hovered / clicked / unhovered - c#

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.

Related

How do I change the width of the combobox button?

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 Combo​Box​Renderer.
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);

Adding Click Handler On UserControl Breaks Click On Children

I have a user control that I'm trying to make draggable. The whole control should be draggable except when you click on buttons or text boxes. I'm handling the mousedown, mouseup and mousemove events on the usercontrol itself and I can drag by clicking anywhere. The only issue is now I can't click any buttons on the user control. Any clue what's going on?
Code is something like this:
<UserControl PreviewMouseLeftButtonDown="Popup_PreviewMouseLeftButtonDown" ....STUFF...>
<!-- CAN'T CLICK THIS -->
<Button />
<UserControl>
Code Behind:
public void Popup_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e)
{
mouseDown = true;
oldMousePosition = this.PointToScreen(e.GetPosition(this));
this.Popup.Child.CaptureMouse();
}
The issue arises when you use CaptureMouse() - this permanently captures all your mouse input on the window, and makes it so that you're unable to click on anything within the Window. I don't know if it's different for you, or if you checked, but it's not (just) that the Button is unclickable - it's that literally everything on the Window is unclickable.
You have to actually do something with the mouse after you've captured it, and then once you finish that, you have to return normal control by calling Mouse.Capture(null). For you, this would probably be best to do in your MouseUp() method.
However, this still leaves the child problem. I can't really think of any way you're going to be able to both capture all mouse click events on a parent control and allow them to get to the child control. I suppose you could check the mouse position against the button position, both relative to the UserControl, then route the click event to the Button every time, but this seems a little overelaborate. Is there a reason you can't just add a full-sized Grid to the UserControl with a lower ZIndex than the Button, and just use that to detect if a click was made inside the UserControl but not on the Button?

how to make visible cadre around buttons

how can i visible cadre around my buttons in c# forms. that whenever I click on it or it runs the cadr will be visible around it?
please get me the code example
private void button6_Click(object sender, EventArgs e)
{
}
If you mean the dotted line, that shows where the focus is, there is nothing you need to do; tab to the button (during runtime!) and it will show.
If you mean the change in appearence during the click, this also happens by itself.
If you want any other changes during the click, set and reset them during the MouseDown and MouseUp events, not the Click, as this will be too late.
If you are looking for still something else, please specify more precisely.
I assume you want to change the border appearance of button. Use the Button.FlatAppearance to change the border styles. But this will work only when the style is Flat.
Sample code:
button1.FlatStyle = FlatStyle.Flat;
button1.FlatAppearance.BorderSize = 2;
button1.FlatAppearance.BorderColor = Color.Red;

WinForm - TabStop Not Working

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.

SIngle and Double click events not working as expected in canvas

I have a Canvas which is present in a UserControl, I have attached the DoubleClick event to this UserControl like this
mainWindow.CanvasLayout.MouseDoubleClick +=
new MouseButtonEventHandler(CanvasLayout_MouseDoubleClick);
I am using this event handler to achieve full screen functionality.
Now, Canvas can have various controls placed inside it. Drag and drop functionality is implemented for these controls similar to this codeproject article.
Basically, I handle these events for a control -
this._dragSource.PreviewMouseLeftButtonDown +=
new MouseButtonEventHandler(DragSource_PreviewMouseLeftButtonDown);
this._dragSource.PreviewMouseMove +=
new System.Windows.Input.MouseEventHandler(DragSource_PreviewMouseMove);
this._dragSource.PreviewMouseLeftButtonUp +=
new MouseButtonEventHandler(DragSource_PreviewMouseLeftButtonUp);
Now, when user DoubleClicks on a control(present in canvas) both DoubleClick(Full Screen) and single Click(drag & drop) operations are performed, i.e. if user double clicks on control and change its mouse position quickly, control position is changed(its dragged and dropped to new position).
Is there any way I can prevent drag and drop operation when user double clicks on a control?
Got it, Instead of handling MouseDoubleClick event, I used PreviewMouseLeftButtonDown -
mainWindow.CanvasLayout.PreviewMouseLeftButtonDown
+= new MouseButtonEventHandler(CanvasLayout_PreviewMouseLeftButtonDown);
and
void CanvasLayout_PreviewMouseLeftButtonDown(object s, MouseButtonEventArgs e)
{
if (e.ClickCount > 1)
{
// Do double-click code
// Code for FullScreen
e.Handled = true;
}
}
What you need to do is, on mouse up start a timer the time should be retrieved from SystemInformation.DoubleClickTime, and do the click action on the timer tick (only if you didn't detect the double click in the mean time, obviously).
Likewise use SystemInformation.DragSize to prevent accidental drag&drop.
Note, the SystemInformation class is part of WinForms so you need to add a reference to System.Windows.Forms, this class will work just fine in WPF apps.

Categories