I have questions are:
If I have one shape on the form, and when I run I want to have 5 shapes on my form. What can I do? Can someone help me?

Recommended Answers

All 12 Replies

In The Declaration Section Of Your Form (Or In A Module) Add

Dim CCount as Integer

Then in your form_load (or button, or wherever) add this:

CCount = CCount + 1
load shapename(CCount)
shapename.Visible = true

That should work. Keep In Mind You'll Probably Need To Change shapename to the name of your shape.

I have questions are:
If I have one shape on the form, and when I run I want to have 5 shapes on my form. What can I do? Can someone help me?

You need to get 4 more shape controls in design mode and keep them visible=false(hidden) and as of required in your code or Load event you can make them visible.

Control inheritance is not there in VB 6.0 , vb.net has this feature where you can inherit from single control to multiple etc.

=Sham

Sham,

I beg to differ. While VB.net does offer inheritance... creating a control array takes pages and more code. The actual term "control array" doesn't even account for anything in .NET. Try doing a search of google for "vb.net control arrays" and there are tons of pages that describe the differences between a VB6 Instantiation of a control, versus VB.Net's. In order to avoid any kind of debate regarding the issue of taking 1 control, and making multiple with it, I have attached a VB6 Project that at design time, has 1 shape. That shape's index property is set to 0. Then, The form load creates more instances of the shape, and positions them nicely next to each other.

Hello Comatose,

I am sorry for my half-knowledge.

Its nice code snippet and gave new information to me.

I never tried such code.

Thanks for the update and information.

Regards
[Sham] :)

My Pleasure! :)

Thanks alots.
Can I ask you more question about this?
What code should I do if I want to have one TextBox and one Command to set the number on TextBox and show it what ever I want?
And How I make row and col want it 5 col than go down one row?
I am sorry if you don't understand this question please ask me.
Thank you so much!

Sham,

I beg to differ. While VB.net does offer inheritance... creating a control array takes pages and more code. The actual term "control array" doesn't even account for anything in .NET. Try doing a search of google for "vb.net control arrays" and there are tons of pages that describe the differences between a VB6 Instantiation of a control, versus VB.Net's. In order to avoid any kind of debate regarding the issue of taking 1 control, and making multiple with it, I have attached a VB6 Project that at design time, has 1 shape. That shape's index property is set to 0. Then, The form load creates more instances of the shape, and positions them nicely next to each other.

I don't understand all of your question. I gather that you want to have a textbox, and a command button, and when they type a number into the textbox, and click the button, to display that number of shapes right?

Please someone help me?

Private Sub Command1_Click()
    Dim i As Integer, x As Long, y As Long

    Shape1(0).Visible = True
    x = Shape1(0).Left
    y = Shape1(0).Top
    If IsNumeric(Text1.Text) Then
        For i = 1 To CInt(Text1.Text)
        
            x = x + Shape1(0).Width
            If (i Mod 5) = 0 Then
                y = y + Shape1(0).Height
                x = Shape1(0).Left
            End If
            
            Load Shape1(i)
            Shape1(i).Visible = True
            Shape1(i).Left = x
            Shape1(i).Top = y
        Next i
    End If
End Sub

or you can do it in short way

Private Sub Command1_Click()
    Shape1(0).Visible = True
    If IsNumeric(Text1.Text) Then
        For i = 1 To CInt(Text1.Text)
            Load Shape1(i)
            Shape1(i).Visible = True
            Shape1(i).Left = Shape1(0).Left + (Shape1(0).Width * (i Mod 5))
            Shape1(i).Top = Shape1(0).Top + (Shape1(0).Height * Fix(i / 5))
        Next i
    End If
End Sub

Regard
Visal

This code is work only one time, when I do next time it doesn't work it said "Object already loaded". Can you help me again please?
Thanks!
seyha

Private Sub Command1_Click()
    Dim i As Integer, x As Long, y As Long

    Shape1(0).Visible = True
    x = Shape1(0).Left
    y = Shape1(0).Top
    If IsNumeric(Text1.Text) Then
        For i = 1 To CInt(Text1.Text)
        
            x = x + Shape1(0).Width
            If (i Mod 5) = 0 Then
                y = y + Shape1(0).Height
                x = Shape1(0).Left
            End If
            
            Load Shape1(i)
            Shape1(i).Visible = True
            Shape1(i).Left = x
            Shape1(i).Top = y
        Next i
    End If
End Sub

or you can do it in short way

Private Sub Command1_Click()
    Shape1(0).Visible = True
    If IsNumeric(Text1.Text) Then
        For i = 1 To CInt(Text1.Text)
            Load Shape1(i)
            Shape1(i).Visible = True
            Shape1(i).Left = Shape1(0).Left + (Shape1(0).Width * (i Mod 5))
            Shape1(i).Top = Shape1(0).Top + (Shape1(0).Height * Fix(i / 5))
        Next i
    End If
End Sub

Regard
Visal

Private Sub Command1_Click()

    If Shape1.UBound > 0 Then
        For i = 1 To Shape1.UBound
            Unload Shape1(i)
        Next i
    End If
    
    If IsNumeric(Text1.Text) Then
        Select Case CInt(Text1.Text)
        
        Case 0
            Shape1(0).Visible = False
        Case 1
            Shape1(0).Visible = True
        Case Is > 1
            For i = 1 To CInt(Text1.Text) - 1
                Load Shape1(i)
                Shape1(i).Visible = True
                Shape1(i).Left = Shape1(0).Left + (Shape1(0).Width * (i Mod 5))
                Shape1(i).Top = Shape1(0).Top + (Shape1(0).Height * Fix(i / 5))
            Next i
            
        End Select
    End If
    
End Sub

Regard
Visal

The reason you are getting this error, is because the for loop loads the new objects (in our case, shapes). So instead of a using a for loop like this:

For i = 1 To CInt(Text1.Text)
     Load Shape1(i)
     Shape1(i).Visible = True
     Shape1(i).Left = Shape1(0).Left + (Shape1(0).Width * (i Mod 5))
     Shape1(i).Top = Shape1(0).Top + (Shape1(0).Height * Fix(i / 5))
Next i

You could declare a form-wide, or global (or even static) variable, that will keep track of which element you are on. Let me show you what happens: You say for i =1 to cint(text1.text). That says loop from 1 to the number in the textbox. Let's pretend it's 5. So, it loads shape1(1), shape1(2), shape1(3), shape1(4) and shape1(5). right? Now they are loaded and on the form. When you click the button again, it does the for loop again, and tries to load shape1(1), shape1(2), etc, etc.... but they are already loaded! We loaded them the first time! So, we create a variable (one that will not change when the sub is complete, so a form-wide variable is good. In the declaration section of the form, do this:

public SCount as integer

and in the form load:

SCount = 1;

Now, We Can keep adding 1 to SCount every time the for loop cycles. This will keep track of where we leave off, so let's try this code in the button:

Shape1(0).Visible = True
    If IsNumeric(Text1.Text) Then
        For i = SCount To CInt(Text1.Text)
            Load Shape1(i)
            Shape1(i).Visible = True
            Shape1(i).Left = Shape1(0).Left + (Shape1(0).Width * (i Mod 5))
            Shape1(i).Top = Shape1(0).Top + (Shape1(0).Height * Fix(i / 5))
            SCount = SCount + 1
        Next i
    End If

Now, The for loop will start where SCount is (which will always be the last number loaded).

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.