vb.netvisual-studio-2008vb6

How to address controls in Powerpack (OvalShape) programmatically, worked in VB6, not in VB.NET


I have some VB6 code that I am migrating to VB.NET. First to VS 2008 and then to VS 22.

I have some subs that address controls programmatically, to be somewhat efficient. I am a hobbyist programmer so not great at this, but my VB6 app works fine and the VB.NET migrated version almost works with a couple issues to sort out.

In VB6, controls could be addressed programmatically using Me.Controls(Name).Property. Code like this worked fine:

for i = 1 to 16
   Me.Controls("OvalShape" & i).FillColor = vbRed
next i

This would turn OvalShape1 - OvalShape16 red. Easy. And yes I know it could/should be a control array, but it isn't. I don't think that makes a difference in my case.

When converted to VB.NET by VS 2008, that gets flagged and does not work. The upgraded code looks like this:

for i = 1 to 16
   'UPGRADE_ISSUE: Control method Controls.FillColor was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
    CType(Me.Controls("OvalShape" & i), Object).FillColor = System.Drawing.Color.Blue
next i

I think the issue is that the OvalShape object is part of Microsoft Powerpack and not directly part of VB any more, so Me.Controls is not correct for addressing the control name.

I have tried and tried different things, and nothing seems to work.

OvalShape.FillColor = SystemDraawingColor.Blue

works fine, so when not trying to concatenate a string and a variable to produce the name, and just typing in the name, it does work.

What do I have to do to be able to use a loop in my code to work with different controls as the loop progresses, controls that are part of the Powerpack?

The migration notes say don't use shapes in your migration as they are not part of VB.NET. Really?


Solution

  • If you only need a simple shape without all the fill and line effects, it is actually easy to create your own in VB.NET. Once this code is compiled, the OvalShape automatically appears in the Toolbox:

    Public Class OvalShape
        Inherits System.Windows.Forms.Control
    
        Dim _fillColor As Color = Color.Transparent
        Property FillColor As Color
            Get
                Return _fillColor
            End Get
            Set
                _fillColor = Value
                Invalidate() ' Redraw the control when the fill color changes
            End Set
        End Property
    
        Dim _borderColor As Color = Color.Black
        Property BorderColor As Color
            Get
                Return _borderColor
            End Get
            Set
                _borderColor = Value
                Invalidate() ' Redraw the control when the border color changes
            End Set
        End Property
    
        Dim _borderWidth As Integer = 1
        Property BorderWidth As Integer
            Get
                Return _borderWidth
            End Get
            Set
                _borderWidth = Value
                Invalidate() ' Redraw the control when the border width changes
            End Set
        End Property
    
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            MyBase.OnPaint(e)
            If FillColor <> Color.Transparent Then
                e.Graphics.FillEllipse(New SolidBrush(FillColor),
                    0, 0, Width - 1, Height - 1)
            End If
            If BorderWidth <> 0 AndAlso BorderColor <> Color.Transparent Then
                Dim d As Integer = _borderWidth \ 2
                e.Graphics.DrawEllipse(
                    New Pen(BorderColor, BorderWidth), d, d, Width - _borderWidth, Height - _borderWidth)
            End If
        End Sub
    End Class
    

    Of course you could get crazy and implement the complete Shape class hierarchy with all the bells and whistles. But that's a little more work.