简体   繁体   中英

How to reference a dynamically created OvalShape with a string variable in vb.net

I have created 40 or so OvalShapes from MS power packs and when the user clicks them they send an ID to separate function that is supposed to change the color of the clicked oval. Unfortunately the Controls method seems to not work.

Controls.Item(Dummy).fillcolor = Color.Red gives me an error saying "FillColor is not a member of 'System.Windows.Forms.Control'" where Dummy is the string containing the name of the control.

I'm fairly new to VB.NET so I'm not sure if there's another way to reference things on the form via a string besides using Controls . Google hasn't really helped on this matter too much, all I found was a way to search through all shapes using the CType on all controls that match the oval type which isn't helpful when I just want to change one control...

Edit: I am looking to be able to do something like the following:

For i = 1 to 40
     OvalName = "Oval" & i
     if Ovali = then do something
Next

I am not sure how you are adding your OvalShapes or what type of container you are using. In order to add them to a Windows Form Control you will need to use the shapeContainer as mentioned by Slaks. In this example I am creating a shapeContainer and adding it to the Form, then I am using the shapeContainers.Shapes.Add Method to add the oval to the ShapeCollection Class . I also am attaching an eventhandler to the Click Event of the Ovals so that I can access the calling Shape to change its fill color through the sender object of the EventHandler. See if this will work for you.

Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
    Dim offset As Integer = 0
    Dim OvalContainer As New ShapeContainer

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        OvalContainer.Size = New Size(Me.Width, 50)
        Me.Controls.Add(OvalContainer)
        OvalContainer.Location = New Point(0, 0)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oval As New OvalShape()

        oval.Size = New Size(30, 40)
        oval.Location = New Point(offset, 0)
        oval.FillStyle = FillStyle.Solid
        oval.FillColor = Color.Transparent
        oval.BorderColor = Color.Black
        oval.BorderWidth = 2
        AddHandler oval.Click, AddressOf ShapeClick
        OvalContainer.Shapes.Add(oval)

        offset += 40
    End Sub

    Private Sub ShapeClick(sender As Object, e As EventArgs)

        Dim oval As OvalShape = DirectCast(sender, OvalShape)
        If oval.FillColor.Equals(Color.Red) Then
            oval.FillColor = Color.Blue
        Else
            oval.FillColor = Color.Red
        End If

    End Sub

End Class

Edit per OP's clarification

When you create your ovals add oval.Name = "oval" & index this will add the name property that will enable the following code to work.

You can iterate through the Shapes Collection like this( this is based off of my above example ):

For Each o As OvalShape In OvalContainer.Shapes
    If o.Name = "oval1" Then o.FillColor = Color.Azure
Next

or you can search for the exact Oval that you are looking for by using the ShapeContainer.Shapes.IndexOfKey Method

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim index As Integer = OvalContainer.Shapes.IndexOfKey("oval1")
    If index >= 0 Then
        DirectCast(OvalContainer.Shapes(index), OvalShape).FillColor = Color.Purple
    End If
End Sub

您需要将控件转换为OvalShape以便您可以访问其属性:

DirectCast(Controls(Dummy), OvalShape).FillColor = Color.Red
Dim MyOval As PowerPacks.OvalShape

For Each ThisControl As PowerPacks.Shape In ShapeContainer1.Shapes
    If TypeOf (ThisControl) Is PowerPacks.OvalShape Then
        MyOval = DirectCast(ThisControl, PowerPacks.OvalShape)
        If MyOval.Name.ToString = ("p36") Then
            MyOval.BackColor = Color.Black
        End If
    End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM