Search code examples
vb.netwinformspowerpacks

Are there alternatives to the Microsoft Visual Basic Power Packs?


For the last several years, I used the Microsoft Visual Basic Power Packs 3.0 to draw basic shapes onto my WinForms.

However, today when I had to set up my new PC for development, I cannot visit the Microsoft site for the download and it seems that all links that would previously point to the file for download has been removed. While I know Visual Basic is being "phased out" or ignored by Microsoft for future development, is there a way I can reinstate this functionality or find a different power pack that will do the same? I'd rather not redo the program I've been working on for years by hand into C#.)


Solution

  • You can use GDI+ to draw shapes. Here are various draw methods under the Graphics class:

    This is certainly isn't all inclusive, I left many of them out. You will need to determine which method you'll need to use from this list: https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics#methods

    Generally what you would do is handle the Paint event of the control you wish to draw on and then leverage the PaintEventArgs argument. Here is an example of drawing a simple rectangle on a form:

    ' vb.net
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        Dim r = New Rectangle(5, 5, 100, 100)
        Using rectangleBorder = New Pen(Color.Black, 1)
            e.Graphics.DrawRectangle(rectangleBorder, r)
        End Using
    End Sub
    
    // c#
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        var r = new Rectangle(5, 5, 100, 100);
        using (var rectangleBorder = new Pen(Color.Black, 1))
        {
            e.Graphics.DrawRectangle(rectangleBorder, r);
        }
    }