Remove frame in focus from control button

I set the Button Winforms control properties so that they appear as a hyperlink on a web page. I formatted everything perfectly, except for the border of the FlatAppearance object. I have code that acts like pseudo-CSS (FormBackColor is a string constant.):

b.FlatStyle = FlatStyle.Flat b.BackColor = ColorTranslator.FromHtml(FormBackColor) b.ForeColor = Color.Blue b.Font = New Font(b.Font.FontFamily, b.Font.Size, FontStyle.Underline) b.Cursor = Cursors.Hand Dim fa As FlatButtonAppearance = b.FlatAppearance fa.BorderSize = 0 fa.MouseOverBackColor = b.BackColor fa.MouseDownBackColor = b.BackColor AddHandler b.MouseEnter, AddressOf ButtonMouseOver AddHandler b.MouseLeave, AddressOf ButtonMouseOut 

Here are the mouse / add-in functions as a reference to what happens:

 Public Shared Sub ButtonMouseOver(ByVal sender As Object, ByVal e As EventArgs) Dim b As Button = DirectCast(sender, Button) Dim fa As FlatButtonAppearance = b.FlatAppearance fa.BorderSize = 1 End Sub Public Shared Sub ButtonMouseOut(ByVal sender As Object, ByVal e As EventArgs) Dim b As Button = DirectCast(sender, Button) Dim fa As FlatButtonAppearance = b.FlatAppearance fa.BorderSize = 0 End Sub 

The code removes the border from the Button flat control except MouseOver, where I add a border of 1 pixel. In MouseLeave, I delete the border. This should show some visual feedback. This works great when the button has no focus. However, if I press the button by pressing the focus button, the mouse again and again shows that there are more than 1 pixel border around the button. I imagine that it combines a clear, clear border with 1 pixel with the traditional "Winform Button", so add a border border around the button.

How to disable / remove the "Winform Button", so add border border? Or, if I just check in ButtonMouseOver to check if the control has focus, being a condition for adding a border and just needs to be done with it? I would prefer to remove the automatic border from focus for any reason :)

+4
source share
3 answers

You can override the OnPaint event and recolor the drawn border with the background color of the form:

 AddHandler Button1.Paint, AddressOf ButtonPaint Private Sub ButtonPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Dim Btn = DirectCast(sender, Button) Using P As New Pen(Me.BackColor) e.Graphics.DrawRectangle(P, 1, 1, Btn.Width - 3, Btn.Height - 3) End Using End Sub 
+4
source

Another way to accomplish this is to inherit the Windows.Forms.Button class and override events. This prevents the need to handle these events for each button in your main program.

 Public Class BorderlessFlatButton Inherits Windows.Forms.Button Protected Overrides Sub OnCreateControl() MyBase.OnCreateControl() Me.FlatAppearance.MouseOverBackColor = Me.BackColor Me.FlatAppearance.MouseDownBackColor = Me.BackColor Me.FlatAppearance.BorderSize = 0 End Sub Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs) MyBase.OnMouseEnter(e) Me.FlatAppearance.BorderSize = 1 End Sub Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs) MyBase.OnMouseLeave(e) Me.FlatAppearance.BorderSize = 0 End Sub Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(pevent) Using P As New Pen(Me.BackColor) pevent.Graphics.DrawRectangle(P, 1, 1, Me.Width - 3, Me.Height - 3) End Using End Sub End Class 

Note. I am not 100% sure. " OnCreateControl " was the best event to use, but it worked in my testing.

+2
source

Derving from Button and setting the control style so that the control is not selected:

 Imports System.Windows.Forms Imports System.Drawing Public Class MyButton Inherits Button Public Sub New() InitializeComponent() Me.BackColor = Color.LightGray Me.FlatStyle = Windows.Forms.FlatStyle.Flat Me.FlatAppearance.BorderColor = SystemColors.ControlDarkDark Me.FlatAppearance.MouseDownBackColor = Color.Cyan Me.FlatAppearance.MouseOverBackColor = SystemColors.ControlDark Me.TabStop = False Me.SetStyle(ControlStyles.Selectable, False) End Sub End Class 
0
source

Source: https://habr.com/ru/post/1309807/


All Articles