How do you make random shapes on a form like a lines, circles, squares, rectangles, and triangles also when you clikc the button again it should stop the shapes from appearing on the form so if i click lines cicrles and squares the form is full of random lines circles and squares when i clikc them again the the lines, circles, and sqaures stop. Does anyone know the coding for this?
15 Replies - 6976 Views - Last Post: 05 January 2007 - 02:06 PM
Replies To: Help with making random Shapes
#2
Re: Help with making random Shapes
Posted 02 January 2007 - 09:21 PM
There are many ways to do as you ask, what have you got so far? Show me your code.
#3
Re: Help with making random Shapes
Posted 02 January 2007 - 09:42 PM
Use line and shape controls. Use control arrays so that you can create more shapes at runtime. Use the Rnd function to generate random numbers. Use these numbers to set the number of shapes, their size and position.
#4
Re: Help with making random Shapes
Posted 03 January 2007 - 06:45 AM
Louisda16th, on 2 Jan, 2007 - 09:42 PM, said:
Use line and shape controls. Use control arrays so that you can create more shapes at runtime. Use the Rnd function to generate random numbers. Use these numbers to set the number of shapes, their size and position.
Or you meant to add:
Make invisible object over drawings to capture click event?
#5
Re: Help with making random Shapes
Posted 03 January 2007 - 07:19 AM
NO NO NO.
You don't use a single hidden Object. It only triggers the event of the object, and not of the actual shape.
We need to create controls dynamically by using a control array.
A control array is a set of similar objects that can be accessed by a subscript.
Use the Load() and Unload() Functions to create and destroy objects dynamically.
You can also use the <form>.Controls.Add() Function
Click here to understand how this is done.
It's written in such a manner that beginners find it easier to comprehend.
You don't use a single hidden Object. It only triggers the event of the object, and not of the actual shape.
We need to create controls dynamically by using a control array.
A control array is a set of similar objects that can be accessed by a subscript.
Use the Load() and Unload() Functions to create and destroy objects dynamically.
You can also use the <form>.Controls.Add() Function
Click here to understand how this is done.
It's written in such a manner that beginners find it easier to comprehend.
#6
Re: Help with making random Shapes
Posted 03 January 2007 - 07:47 AM
born2c0de, on 3 Jan, 2007 - 07:19 AM, said:
NO NO NO.
You don't use a single hidden Object. It only triggers the event of the object, and not of the actual shape.
We need to create controls dynamically by using a control array.
A control array is a set of similar objects that can be accessed by a subscript.
Use the Load() and Unload() Functions to create and destroy objects dynamically.
You can also use the <form>.Controls.Add() Function
Click here to understand how this is done.
It's written in such a manner that beginners find it easier to comprehend.
You don't use a single hidden Object. It only triggers the event of the object, and not of the actual shape.
We need to create controls dynamically by using a control array.
A control array is a set of similar objects that can be accessed by a subscript.
Use the Load() and Unload() Functions to create and destroy objects dynamically.
You can also use the <form>.Controls.Add() Function
Click here to understand how this is done.
It's written in such a manner that beginners find it easier to comprehend.
I actually never got the idea why to do that maybe you would help me understand. I would'f urged (suppressed
Dim WithEvents tekst As Label
Dim kujud() As Shape
Dim xvisible As Boolean
Dim x As Integer
Private Sub Command1_Click()
ReDim kujud(-1) As Shape
End Sub
Private Sub Form_Load()
Randomize
x = InputBox("Insert nr.")
ReDim kujud(Val(x))
For i = 1 To x
Set kujud(i) = Form1.Controls.Add("vb.shape", "kuju_" & i, Form1)
With kujud(i)
.Shape = (Rnd() * 50) \ 10 + 1
.Width = (Rnd() * 5000) \ 5
.Height = (Rnd() * 5000) \ 5
.Top = (Rnd() * 5000) \ 5
.Left = (Rnd() * 5000) \ 5
End With
Next i
Set tekst = Form1.Controls.Add("vb.label", "tekst" & i, Form1)
With tekst
.Top = 0
.Left = 0
.Width = Me.Width
.Height = Me.Height
.visible = True
End With
End Sub
Private Sub tekst_Click()
xvisible = Not xvisible
For i = 1 To x
kujud(i).visible = xvisible
Next i
End Sub
This code would generate x number of elements of random type and size, and by clicking on them you can make them all visible/invisible
This post has been edited by m2s87: 03 January 2007 - 08:53 AM
#7
Re: Help with making random Shapes
Posted 03 January 2007 - 08:48 AM
DMN
I actually forgot while writing what i was doing
Actually now i think he wanted something like
Dim WithEvents tekst As Label
Dim WithEvents stopper As timer
Dim kujud() As Shape
Dim makemore As Boolean
Dim x As Integer
Private Sub Form_Load()
Set stopper = Form1.Controls.Add("vb.timer", "stopper", Form1)
With stopper
.Interval = 600
.Enabled = True
End With
Set tekst = Form1.Controls.Add("vb.label", "tekst" & i, Form1)
With tekst
.Top = 0
.Left = 0
.Width = Me.Width
.Height = Me.Height
.visible = True
.BackStyle = 0
End With
End Sub
Private Sub tekst_Click()
makemore = Not makemore
End Sub
Private Sub Stopper_Timer()
If makemore = False Then
Randomize
x = x + 1
ReDim kujud(Val(x))
For i = x To x
Set kujud(i) = Form1.Controls.Add("vb.shape", "kuju_" & i, Form1)
With kujud(i)
.Shape = (Rnd() * 50) \ 10 + 1
.Width = (Rnd() * 5000) \ 5
.Height = (Rnd() * 5000) \ 5
.Top = (Rnd() * 5000) \ 5
.Left = (Rnd() * 5000) \ 5
.visible = True
End With
Next i
End If
End Sub
#8
Re: Help with making random Shapes
Posted 04 January 2007 - 02:29 PM
Ok this is how it is. Im using Visual Basic 5 at school and i made random lines,circles,and triangles, now i would need help making a square and rectangle. Im not sure how the coding went for the Line but it goes like this
Line(x1,y1)-(x2,y2)QBColor©
then i had some stuff like d=Int(upperbound-lowerbound+1)*Rnd-1))
something like that. All I would need help in now is how to make a square and rectangle
Line(x1,y1)-(x2,y2)QBColor©
then i had some stuff like d=Int(upperbound-lowerbound+1)*Rnd-1))
something like that. All I would need help in now is how to make a square and rectangle
#9
Re: Help with making random Shapes
Posted 04 January 2007 - 03:27 PM
Use this function
And when you want random size square, just
and if you need a rectangle then just
Note: you need a Picture to draw in, so add Picture1 to the form
Hope it helps
Public Sub joonista_ristkylik(ByVal x1, ByVal y1, ByVal x2, ByVal y2) Picture1.Line (x1, y1)-(x1, y2), vbBlue Picture1.Line (x1, y1)-(x2, y1), vbBlue Picture1.Line (x2, y2)-(x1, y2), vbBlue Picture1.Line (x2, y2)-(x2, y1), vbBlue End Sub
And when you want random size square, just
randomize a=rnd()*2000+100 call joonista_ristkylik(0,0,a,a)
and if you need a rectangle then just
randomize a=rnd()*2000+100 h=rnd()*2000+100 call joonista_ristkylik(0,0,a,h)
Note: you need a Picture to draw in, so add Picture1 to the form
Hope it helps
#10
Re: Help with making random Shapes
Posted 04 January 2007 - 04:15 PM
Just one question what is Public Sub joonista_ristkylik? havent seen that before
#11
Re: Help with making random Shapes
Posted 04 January 2007 - 04:30 PM
BigD, on 4 Jan, 2007 - 04:15 PM, said:
Just one question what is Public Sub joonista_ristkylik? havent seen that before
As the name says, its a public (means you can get access to it where ever it is) procedure called (in en:) draw_rectangle. It is a code snippet that draws a rectangle, you just need to specify the coordinates.
#12
Re: Help with making random Shapes
Posted 04 January 2007 - 04:52 PM
Im really absolutly sure that your coding works but i need a simple way because im sure my VB teacher will notice i didnt make this. My code looked something like this
Dim a,b,c,d,e,f,g,x as integer
Dim upperbound,lowerbound as integer
Dim x1,y1,x2,y2 as integer
Private Sub cmdexit_click()
end
end sub
Private sub cmdclear_click()
refresh
end sub
Private Sub cmddraw_click()
x=x+1
If x= 1 then
cmdraw.caption=Stop
tmrdraw.interval=100
else
cmddraw.caption=Draw Line
tmrdraw.interval=0
x=0
end if
end sub.....................Im not sure if this is right becuz i dont have the notes with me lol
Private Sub tmrdraw_Timer()
there is too much here for me to memorize
i had stuff like
a=Int((upperbound-lowerbound+1)*Rnd-1))
b-g would say the same thing
the i had like
f=c
Line(x1.y1)-(x2,y2),QBColor©
Line(x1.y1)-(x2,y2),QBColor©
Line(x1.y1)-(x2,y2),QBColor©
end sub
Im not sure if this correct how i typed it but at school my random lines work i just need something like this that makes squares
Dim a,b,c,d,e,f,g,x as integer
Dim upperbound,lowerbound as integer
Dim x1,y1,x2,y2 as integer
Private Sub cmdexit_click()
end
end sub
Private sub cmdclear_click()
refresh
end sub
Private Sub cmddraw_click()
x=x+1
If x= 1 then
cmdraw.caption=Stop
tmrdraw.interval=100
else
cmddraw.caption=Draw Line
tmrdraw.interval=0
x=0
end if
end sub.....................Im not sure if this is right becuz i dont have the notes with me lol
Private Sub tmrdraw_Timer()
there is too much here for me to memorize
i had stuff like
a=Int((upperbound-lowerbound+1)*Rnd-1))
b-g would say the same thing
the i had like
f=c
Line(x1.y1)-(x2,y2),QBColor©
Line(x1.y1)-(x2,y2),QBColor©
Line(x1.y1)-(x2,y2),QBColor©
end sub
Im not sure if this correct how i typed it but at school my random lines work i just need something like this that makes squares
#13
Re: Help with making random Shapes
Posted 04 January 2007 - 05:10 PM
I guess what you want is the drawing function. You do need to code it yourself, but i guess it could look like:
Private Sub tmrdraw_Timer() Randomize a=50+Int((upperbound-lowerbound+1)*Rnd-1)) b=50+Int((upperbound-lowerbound+1)*Rnd-1)) x=50 y=50 f=c Line (x, y)-(x, b),QBColorİ Line (x, y)-(a, y),QBColorİ Line (a, b)-(x, b),QBColorİ Line (a, b)-(a, y),QBColorİ end sub
#14
Re: Help with making random Shapes
Posted 04 January 2007 - 05:21 PM
So if i use the code you gave me when i click draw triangle it should make random triangles appear all over the form?
#15
Re: Help with making random Shapes
Posted 05 January 2007 - 06:26 AM
VB5 is quite old. You should move to VB6. Or better: VB.NET 2005
|
|

New Topic/Question
Reply




MultiQuote





|