Hi,

I need to change size parameter of OvalShape, I have about 100 of them, named like OvalShape1, OvalShape1...... OvalShape100

Search the froum I noted this http://www.daniweb.com/software-development/csharp/threads/367949

This example works for Label, but throws error for OvalShape, following is my modified code that giving error;

For Count As Integer = 1 To 100
    Me.Controls("OvalShape" & Count).Size = New Size(20, 20)
Next

The error reporting is during run time, "Object reference not set to an instance of an object"
Can anyone PLEASE guide me where I am doing wrong.

M.Pathma

First off, is the control "OvalShape1" initialized?
Secondly, instead of looping through like that, do a

For ctl as Control in me.controls
   if typeof(ctl) is OvalShape andalso ctl isnot nothing then
      ctl.Size = new Size(20,20)
   end if
Next

Thirdly, if you're using .Net 3.5 and on you can use the Me.Controls.OfType(Of OvalShape) instead, like so:

For ctl as Control in me.controls.oftype(of OvalShape)
   if ctl isnot nothing then
      ctl.Size = new Size(20,20)
   end if
Next

There are two ways you could do it. If you have to do it ar run time then you can do

For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is OvalShape Then
        CType(ctrl,OvalShape).Size = New Size(20, 20)
    End If
Next

If you want to do it at design time you can load yourform.Designer.vb into any text editor and modify the statements in the source code (make a backup first).

commented: Beat me to it. +5

There are two ways you could do it. If you have to do it ar run time then you can do

For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is OvalShape Then
        CType(ctrl,OvalShape).Size = New Size(20, 20)
    End If
Next

If you want to do it at design time you can load yourform.Designer.vb into any text editor and modify the statements in the source code (make a backup first).

Hi,

Thanks for your hint, I tired it, there is error generating at "TypeOf ctrl Is OvalShape" the error is,

"Expression of type 'System.Windows.Forms.Control' can never be of type 'Microsoft.VisualBasic.PowerPacks.OvalShape'."

I have imported "Imports Microsoft.VisualBasic.PowerPacks"

Just for your information, I am using Visual Basic 2010 and Framework4.0.

What error I am doing, please guide.

M.Pathma

First off, is the control "OvalShape1" initialized?
Secondly, instead of looping through like that, do a

For ctl as Control in me.controls
   if typeof(ctl) is OvalShape andalso ctl isnot nothing then
      ctl.Size = new Size(20,20)
   end if
Next

Thirdly, if you're using .Net 3.5 and on you can use the Me.Controls.OfType(Of OvalShape) instead, like so:

For ctl as Control in me.controls.oftype(of OvalShape)
   if ctl isnot nothing then
      ctl.Size = new Size(20,20)
   end if
Next

Hi,

Thanks for your afford, but my luck, both example gives error,

The 1st example give "Expression of type 'System.Windows.Forms.Control' can never be of type 'Microsoft.VisualBasic.PowerPacks.OvalShape'." at "typeof(ctl) is OvalShape"

The 2nd Example gives error "'For' loop control variable cannot be of type 'System.Windows.Forms.Control' because the type does not support the required operators."
and the "In" is declared as Syntax Error.

Please guide what I am doing worng, I am using VB.2010, FrameWork4.0, Development PC is Win7.

Thanks for you consideration

M.Pathma

That is peculiar. I created a form with one button and four OvalShapes. When I break, I see that Me.Controls contains only two controls, a Button and a ShapeContainer. The ShapeContainer has no other controls in it. However, I can define a Click handler for an OvalShape and the sender parameter is of type OvalShape. So where exactly is OvalShape contained in? I did a little experimenting and came up with

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is ShapeContainer Then
                For Each shp As Shape In CType(ctrl, ShapeContainer).Shapes
                    CType(shp, Microsoft.VisualBasic.PowerPacks.OvalShape).Size = New Size(30, 30)
                Next
            End If
        Next

    End Sub

It may be awkward looking but it works. It occurred to me that there is an easier way to do this at design time. Just select all of the oval shapes and modify the size property. That will change all of them at the same time. For runtime changes, the above is all I can come up with. I hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.