VB.net " sfButton1.Paint += sfButton1_Paint "

Hi All,

I'm trying to use the 'Rounded Rectangle Button' function of the SfButton in a WinForm app. The button are added programmatically however I can't get "sfButton1.Paint += sfButton1_Paint" to work as I get the following error,

"Public Event Paint As PaintEventHandler is an event, and cannot be called directly. Use a 'RaiseEvent' Statement to raise the event "

The code I'm using is;

    Public Function BuildTagButton() As SfButton

        Dim tag As New SfButton()

        tag.AutoSize = True

        tag.AutoSizeMode = AutoSizeMode.GrowAndShrink

          tag.Paint += sfButton1_Paint()

        Return tag

    End Function


    Public Sub sfButton1_Paint(ByVal sender As SfButton, ByVal e As PaintEventArgs)

        Dim radius As Integer = 5

        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias

        Dim rect As Rectangle = New Rectangle(sender.ClientRectangle.X + 1, sender.ClientRectangle.Y + 1, sender.ClientRectangle.Width - 2, sender.ClientRectangle.Height - 2)

        sender.Region = New Region(GetRoundedRect(rect, radius))

        rect = New Rectangle(rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2)

        e.Graphics.DrawPath(New Pen(Color.Red), GetRoundedRect(rect, radius))

    End Sub


    Private Function GetRoundedRect(ByVal bounds As Rectangle, ByVal radius As Integer) As GraphicsPath

        Dim diameter As Integer = radius * 2

        Dim size As Size = New Size(diameter, diameter)

        Dim arc As Rectangle = New Rectangle(bounds.Location, size)

        Dim path As GraphicsPath = New GraphicsPath()

        If radius = 0 Then

            path.AddRectangle(bounds)

            Return path

        End If

        path.AddArc(arc, 180, 90)

        arc.X = bounds.Right - diameter

        path.AddArc(arc, 270, 90)

        arc.Y = bounds.Bottom - diameter

        path.AddArc(arc, 0, 90)

        arc.X = bounds.Left

        path.AddArc(arc, 90, 90)

        path.CloseFigure()

        Return path

    End Function


Thanks Andy


3 Replies 1 reply marked as answer

DV Duraimurugan Vedagiri Syncfusion Team July 16, 2020 07:36 AM UTC

Hi Andy,

Thanks for contacting syncfusion support.

We have prepared the sample to added SfButton programmatically and draw the rounded rectangle border in Paint event. But we were unable to reproduce the reported issue.

Here is the sample that works as expected.

https://www.syncfusion.com/downloads/support/forum/156091/ze/Button1316331440.zip  



Regards,
Duraimurugan V.

Marked as answer

AN andy July 18, 2020 05:29 AM UTC

Hi, thanks for the sample code. 

The '+= sfButton1_Paint()' code is replaced with  ' AddHandler sfButton1.Paint, AddressOf SfButton1_Paint' and works

Thanks again

Andy




VR Vijayalakshmi Roopkumar Syncfusion Team July 20, 2020 11:22 AM UTC

Hi Andy,

Thank you for your update.

As already depicted in our sample, you need to use AddHandler to invoke the events in VB.NET. We are glad that you have addressed it at your end.

Please let us know if you need any other further assistance on this.

Regards,
Vijayalakshmi VR

Loader.
Up arrow icon