Imports System.Drawing.Drawing2D
'https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-inherit-from-existing-windows-forms-controls?view=netframeworkdesktop-4.8
Public Class UserControl1
Inherits System.Windows.Forms.Label
Private _radius As Int32 = 20 'Radius of the Corner Curve
Private _opacity As Int32 = 125 'Opacity of the Control
Private _backColor As System.Drawing.Color = Color.Black 'Back Color of Control
Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim rect As Rectangle = Me.ClientRectangle 'Drawing Rounded Rectangle
rect.X = rect.X + 1
rect.Y = rect.Y + 1
rect.Width -= 2
rect.Height -= 2
Using bb As GraphicsPath = GetPath(rect, _radius)
Using br As Brush = New SolidBrush(Color.FromArgb(_opacity, _backColor))
e.Graphics.FillPath(br, bb)
End Using
End Using
End Sub
'Returns the GraphicsPath to Draw a RoundedRectangle
Protected Function GetPath(ByVal rc As Rectangle, ByVal r As Int32) As GraphicsPath
Dim x As Int32 = rc.X, y As Int32 = rc.Y, w As Int32 = rc.Width, h As Int32 = rc.Height
r = r << 1
Dim path As GraphicsPath = New GraphicsPath()
If r > 0 Then
If (r > h) Then r = h
If (r > w) Then r = w
path.AddArc(x, y, r, r, 180, 90)
path.AddArc(x + w - r, y, r, r, 270, 90)
path.AddArc(x + w - r, y + h - r, r, r, 0, 90)
path.AddArc(x, y + h - r, r, r, 90, 90)
path.CloseFigure()
Else
path.AddRectangle(rc)
End If
Return path
End Function
End Class
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
This am looking into the rounded label idea.
Made more progress than I thought I could, but seems to not be aligned well?
Added a region statement
I draw a red outline and a blue background
The red should go all round the blue but does not.
Any ideas how to fix that?
Is in the paint event?
Complete user control so far.
Code:
Imports System.Drawing.Drawing2D
'https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-inherit-from-existing-windows-forms-controls?view=netframeworkdesktop-4.8
Public Class UserControl1
Inherits System.Windows.Forms.Label
Private _radius As Int32 = 20 'Radius of the Corner Curve
Private _opacity As Int32 = 125 'Opacity of the Control
Private _backColor As Color = Color.Blue 'Back Color of Control
Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' https://learn.microsoft.com/en-us/dotnet/api/system.drawing.region.-ctor?view=net-8.0
Dim rect As Rectangle = Me.ClientRectangle 'Drawing Rounded Rectangle
rect.X = rect.X + 1
rect.Y = rect.Y + 1
rect.Width -= 4
rect.Height -= 4
Using bb As GraphicsPath = GetPath(rect, _radius)
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 2), bb)
Using br As Brush = New SolidBrush(Color.FromArgb(_opacity, _backColor))
e.Graphics.FillPath(br, bb)
End Using
Dim Region As New Region(bb)
Me.Region = Region
End Using
End Sub
'Returns the GraphicsPath to Draw a RoundedRectangle
Protected Function GetPath(ByVal rc As Rectangle, ByVal r As Int32) As GraphicsPath
Dim x As Int32 = rc.X, y As Int32 = rc.Y, w As Int32 = rc.Width, h As Int32 = rc.Height
r = r << 1
Dim path As GraphicsPath = New GraphicsPath()
If r > 0 Then
If (r > h) Then r = h
If (r > w) Then r = w
path.StartFigure()
'top left
path.AddArc(x, y, r, r, 180, 90)
'Top Right
path.AddArc(x + w - r, y, r, r, 270, 90)
'bottom right
path.AddArc(x + w - r, y + h - r, r, r, 0, 90)
'bottom left
path.AddArc(x, y + h - r, r, r, 90, 90)
path.CloseFigure()
Else
path.AddRectangle(rc)
End If
Return path
End Function
End Class
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
Plus would like to know why the green round label using a different method is so badly shaped?
Done this way, put label50 on a form
In form load this
Code:
Dim gp As GraphicsPath = GetPath(Label50.ClientRectangle, 20)
Label50.Region = New Region(gp)
Label50.BackColor = Color.Green
Which is running this and I can't make it look right as only some of the corners are somewhat roundish.
Code:
Private Function GetPath(ByVal rc As Rectangle, ByVal r As Int32) As GraphicsPath
Dim x As Int32 = rc.X, y As Int32 = rc.Y, w As Int32 = rc.Width, h As Int32 = rc.Height
r = r << 1
Dim path As New GraphicsPath
If r > 0 Then
path.StartFigure()
If (r > h) Then r = h
If (r > w) Then r = w
path.StartFigure()
'top left
path.AddArc(x, y, r, r, 180, 90)
'Top Right
path.AddArc(x + w - r, y, r, r, 270, 90)
'bottom right
path.AddArc(x + w - r, y + h - r, r, r, 0, 90)
'bottom left
path.AddArc(x, y + h - r, r, r, 90, 90)
path.CloseFigure()
Else
path.AddRectangle(rc)
End If
Return path
End Function
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
This mod makes it look a little better, still the left corner is more heavily showing the red line.
The thickness I changed from 2 to 4, corner radius to 30
And paint the red line after filling region with blue
Also playing with elliptical buttons but the edges are not very smooth
Code:
Private _radius As Int32 = 30 'Radius of the Corner Curve
Code:
Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' https://learn.microsoft.com/en-us/dotnet/api/system.drawing.region.-ctor?view=net-8.0
Dim rect As Rectangle = Me.ClientRectangle 'Drawing Rounded Rectangle
'rect.X = rect.X ' - 10
'rect.Y = rect.Y '- 10
'rect.Width -= 2
'rect.Height -= 2
Using bb As GraphicsPath = GetPath(rect, _radius)
Using Region As New Region(bb)
Me.Region = Region
Using br As Brush = New SolidBrush(Color.FromArgb(_opacity, _backColor))
e.Graphics.FillPath(br, bb)
End Using
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 4), bb)
End Using
End Using
End Sub
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
Originally Posted by .paul.
You should set the Region just once instead of every time your control repaints...
Is it repainting a lot of times?
vs2022 was crashing a lot on this usercontrol.
Then something strange, control refused to work any more and the image was painted with a big red exception.
I deleted the usercontrol and grabbed another from toolbox.
And this one is working, much better looking too, I find that totally unexplainable.
Here is my updated code so far and what it looks like in the designer
Code:
Public Class UserControl1
Inherits System.Windows.Forms.Label
Private _radius As Int32 = 20 'Radius of the Corner Curve
Private _opacity As Int32 = 125 'Opacity of the Control
Private _backColor As Color = Color.Blue 'Back Color of Control
Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' https://learn.microsoft.com/en-us/dotnet/api/system.drawing.region.-ctor?view=net-8.0
Dim rect As Rectangle = Me.ClientRectangle 'Drawing Rounded Rectangle
rect.X = rect.X + 1
rect.Y = rect.Y + 1
rect.Width -= 4
rect.Height -= 4
Using bb As GraphicsPath = GetPath(rect, _radius)
Using br As Brush = New SolidBrush(Color.FromArgb(_opacity, _backColor))
e.Graphics.FillPath(br, bb)
End Using
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 4), bb)
Dim Region As New Region(bb)
Me.Region = Region
End Using
End Sub
'Returns the GraphicsPath to Draw a RoundedRectangle
Protected Function GetPath(ByVal rc As Rectangle, ByVal r As Int32) As GraphicsPath
Dim x As Int32 = rc.X, y As Int32 = rc.Y, w As Int32 = rc.Width, h As Int32 = rc.Height
r = r << 1
Dim path As GraphicsPath = New GraphicsPath()
If r > 0 Then
If (r > h) Then r = h
If (r > w) Then r = w
path.StartFigure()
'top left
path.AddArc(x, y, r, r, 180, 90)
'Top Right
path.AddArc(x + w - r, y, r, r, 270, 90)
'bottom right
path.AddArc(x + w - r, y + h - r, r, r, 0, 90)
'bottom left
path.AddArc(x, y + h - r, r, r, 90, 90)
path.CloseFigure()
Else
path.AddRectangle(rc)
End If
Return path
End Function
End Class
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
This one will not set a fill color, that way can just set your own label back color in the designer, which is better.
Border on this one is white, could be any rgb color, rim color thickness here set to 4
I don't see paint executing more than once.
Anyone has ideas, how to do it differently or better?
In the designer it appears as it does when running. Make code changes to user control it changes how it appears in the designer.
Code:
Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' https://learn.microsoft.com/en-us/dotnet/api/system.drawing.region.-ctor?view=net-8.0
Dim rect As Rectangle = Me.ClientRectangle 'Drawing Rounded Rectangle
' rect.X = rect.X + 1
' rect.Y = rect.Y + 1
'rect.Width -= 4
'rect.Height -= 4
Debug.Print("Hello")
Using bb As GraphicsPath = GetPath(rect, _radius)
'Using br As Brush = New SolidBrush(Color.FromArgb(_opacity, _backColor))
'e.Graphics.FillPath(br, bb)
'End Using
'e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 4), bb) 'red
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 255, 255), 10), bb) 'white
Dim Region As New Region(bb)
Me.Region = Region
End Using
End Sub
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
If you simply do this for any named label, you can get rounded corners and green background
Code:
Private Sub frmLogon_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim gp As GraphicsPath = GetPath(Label50.ClientRectangle, 20)
Label50.Region = New Region(gp)
Label50.BackColor = Color.Green
Then this sub here makes the corners
Code:
Private Function GetPath(ByVal rc As Rectangle, ByVal r As Int32) As GraphicsPath
Dim x As Int32 = rc.X, y As Int32 = rc.Y, w As Int32 = rc.Width, h As Int32 = rc.Height
r = r << 1
Dim path As New GraphicsPath
If r > 0 Then
path.StartFigure()
If (r > h) Then r = h
If (r > w) Then r = w
path.StartFigure()
'top left
path.AddArc(x, y, r, r, 180, 90)
'Top Right
path.AddArc(x + w - r, y, r, r, 270, 90)
'bottom right
path.AddArc(x + w - r, y + h - r, r, r, 0, 90)
'bottom left
path.AddArc(x, y + h - r, r, r, 90, 90)
path.CloseFigure()
Else
path.AddRectangle(rc)
End If
Return path
End Function
The right sided corners look less rounded than the left side.
The text does not center well even though told to center in the label properties
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
picture of label50, the green label
Right side corners are they different?
Text not centered.
EDIT, I found out the right side of label50 is running into the left side of the combo box causing the curve to flatten out.
The resizer module can't properly resize the label50 green control text properly, so unless that can be dealt with, wont be using that label50 code outside of the usercontrol.
added a picture that shows what happens to green label text versus yellow label text (the usercontrol) when form is super stretched
Last edited by sdowney1; Jun 22nd, 2024 at 05:09 AM.
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
I have a resizer module that resizes all the 'controls'
When I resize the form, the green label50 gets very messed up, text slides over and the corners do odd things.
But the yellow usercontrol label resizes perfectly.
Which means I will need to use the usercontrol If I want it to look good.
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
Moved this code from form load to form resize and now it looks fine like the yellow label
Means, you don't have to use a user control
Any ideas how to draw the border outside the user control? Will look into it next.
Code:
Private Sub frmLogon_Resize(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Resize
clsElastic.ResizeAllControls(Me)
Dim gp As GraphicsPath = GetPath(Label50.ClientRectangle, 20)
Label50.Region = New Region(gp)
Label50.BackColor = Color.Green
End Sub
Re: rounded corner label test look terrible, can we get rid of the rectangle behind i
Originally Posted by .paul.
What is the advantage of not using an extended control? Encapsulation is the preferred and recommended OOP method.
Just if you don't want to delete all your labels and start over is all I can think.
You can just draw path the existing label.
What I am wondering is how you can create a new property for a usercontrol?
Like radius and color for the pen draw?
Right now it is hard coded into the code right here
Code:
Public Class UserControlLabel
Inherits System.Windows.Forms.Label
Private _radius As Int32 = 10 'Radius of the Corner Curve
' Private _opacity As Int32 = 125 'Opacity of the Control
'Private _backColor As Color = Color.Blue 'Back Color of Control
I commented out backcolor as it is a label property in the usercontrol properties window, just set it as you wish outside the control
But the radius and draw pen color, would be nice to set that some other way.
I also renamed it to usercontrollabel
And it can be tranparent and let an image shine through too.
Here one is transparent and one is red
I am also thinking I can use a transparent label as a frame using the draw pen, and have other labels grouped inside the frame
The green label is not a usercontrol, just a get path radius corner modification of a normal label control with a different radius number.
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
Trying to make the property pages work, made some progress but never recognizes the value in the user control
I can create a description
I can change the value, like put in 5
The value though here is always 10
In my thinking, it would return 5?
ASFAIK, no value of 10 is set anywhere in the code, so how can it say it has 10?
Code:
?Anumber
10
?_rad
10
the code I am trying
Code:
''' <summary>
''' Returns "ANumber"
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Private _rad As Integer
Public Property ANumber() As Integer
Get
Return _rad
End Get
Set(ByVal value As Integer)
_rad = value
End Set
End Property
properties screenshot of ANumber with 5, it takes whatever number you want
What do I need to do here? I am stuck at the moment.
Last edited by sdowney1; Jun 23rd, 2024 at 09:51 AM.
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
Made more progress, finally got a 5 out of it!
Code:
?_rad
5
?anumber
0
by this here
Code:
Public Property ANumber() As Integer
Get
'This will assign the initial value
' ANumber = 5
End Get
Set(ByVal value As Integer)
' Assign the value when it is changed in the properties window
_rad = value
End Set
End Property
I spent 2 hours looking at all sorts of crappy code examples and fruitless discussions between other people, then I found this site with a clear explanation of how to do it. Seems to have worked.
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
I just rolled back vs2022 to earlier version due to too many crashes.
I can't set the property page for 'ANumber' in the designer, it shows a 5, only takes a 5, as type in 20, it reverts to 5.
Something changed. My code did not. It used to let me, so obviously it's being flakey.
I can though do it programmatically like this to set the radius.
How do you enable a designer property to accept a value typed into a property page for a control?
''' <summary>
''' Returns "ANumber"
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
'''
Public _radius As Integer
Public Property ANumber() As Integer
Get
'This will assign the initial value
ANumber = 5
End Get
Set(ByVal value As Integer)
' Assign the value when it is changed in the properties window
_radius = value
End Set
End Property
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
Code:
''' <summary>
''' Returns _radius
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
'''
Private _radius As Integer = 5 'This will assign the initial value
Public Property ANumber() As Integer
Get
Return _radius
End Get
Set(ByVal value As Integer)
' Assign the value when it is changed in the properties window, OR in code
_radius = value
End Set
End Property
Last edited by .paul.; Jun 23rd, 2024 at 12:35 PM.
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
Originally Posted by .paul.
Code:
''' <summary>
''' Returns _radius
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
'''
Private _radius As Integer = 5 'This will assign the initial value
Public Property ANumber() As Integer
Get
Return _radius
End Get
Set(ByVal value As Integer)
' Assign the value when it is changed in the properties window, OR in code
_radius = value
End Set
End Property
Thanks.
Ok, I just tried this too, and it is working, leaving out the other stuff.
Seems the rollback of vs2022 has been stable.
It was acting very flakey, not working like it should and doing odd things.
Private _radius As Integer
Public Property ANumber() As Integer
Get
Return _radius
End Get
Set(value As Integer)
'Assign the value when it is changed in the properties window
_radius = value
End Set
End Property
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
yes, working, a lot of things happening today!
Now will give a try at coding in a pen property color.
I plan to change the property names to something better, this is just testing.
EDITING found it here
I also found, after adding a Public Property, if I shut down and restart vs2022, the public property appears in the property page
Dim _PenColor As Color
Public Property AMandatoryColor() As Color
Get
Return _PenColor
End Get
Set(ByVal value As Color)
_PenColor = value
End Set
End Property
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
Got it working, the Pen color will only appear on the control when program runs
but color is selected in the designer property page.
Using it this way, individual labels can be created with different border colors and radius.
Another property I will do is the pen thickness.
It's really fun when you get things working.
I wonder if that border pen color appear in the designer for the label, right now it is white.
Be nice to see it change to red when selecting the color property before the program executes.
Code:
Private _radius As Integer = 5
Private _PenColor As Color
Public Property ANumber() As Integer
Get
Return _radius
End Get
Set(value As Integer)
'Assign the value when it is changed in the properties window
_radius = value
End Set
End Property
Public Property AMandatoryColor() As Color
Get
Return _PenColor
End Get
Set(ByVal value As Color)
_PenColor = value
End Set
End Property
Code:
Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' https://learn.microsoft.com/en-us/dotnet/api/system.drawing.region.-ctor?view=net-8.0
Dim rect As Rectangle = Me.ClientRectangle 'Drawing Rounded Rectangle
'Debug.Print("Hello")
Using bb As GraphicsPath = GetPath(rect, _radius)
Dim Region As New Region(bb)
Me.Region = Region
'Using br As Brush = New SolidBrush(Color.FromArgb(_opacity, _backColor))
'e.Graphics.FillPath(br, bb)
'End Using
'e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 4), bb) 'red
'e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 255, 255), 5), bb) 'white
'e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 0, 0, 0), 10), bb) 'black
e.Graphics.DrawPath(New Pen(_PenColor, 10), bb) 'white
End Using
End Sub
Re: [RESOLVED] rounded corner label test look terrible, can we get rid of the rectang
Quess what, it is working in the designer window. Make a change in property pages, go back click the form, and everything updates on the usercontrol.
I now have penwidth, pencolor, radius working in the prop pages.
Code:
Public Property APenWidth() As Integer
Get
Return _PenWidth
End Get
Set(ByVal value As Integer)
_PenWidth = value
End Set
End Property
''' <summary>
''' Returns "The radius for the corners."
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<Description("The radius for the corners.")>
Public Property ANumber() As Integer
Get
Return _radius
End Get
Set(value As Integer)
'Assign the value when it is changed in the properties window
_radius = value
End Set
End Property
''' <summary>
''' Returns "The color of the pen rim to draw."
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<Description("The color of the pen rim to draw.")>
Public Property AMandatoryColor() As Color
Get
Return _PenColor
End Get
Set(ByVal value As Color)
_PenColor = value
End Set
End Property
''' <summary>
''' Returns "The width of the pen to draw."
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<Description("The width of the pen to draw.")>
Public Property APenWidth() As Integer
Get
Return _PenWidth
End Get
Set(ByVal value As Integer)
_PenWidth = value
End Set
End Property
Last edited by sdowney1; Jun 23rd, 2024 at 05:06 PM.