H
ham-z
I have written the following Win app in VB.NET 2003 . The class is simply
picture boxes that behave in a random order after they have been
instantiated and added to a form. When I create 15 or more instances of my
class, the whole program runs slowly in a way that I have to close the
program. I have tried to create a new thread for each class, but that throws
an exception , because a separated thread can't be added to a form from a
child class.
My code is this ( please tell me if there are better ways for doing it)
coplile with :
vbc [filename]
/r:system.dll,system.windows.forms.dll,system.drawing.dll,microsoft.visualbasic.dll
/t:winexe /main:form1
'----------------------------------- Start of CODE
Imports System.Math
imports Microsoft.VisualBasic
imports System.drawing
imports system.windows.forms
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(384, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
CreateOBJ()
End Sub
Sub CreateOBJ()
Dim myRec As Rec = New Rec(Me.CreateGraphics, Me)
End Sub
End Class
'--------------------------------------------------- Class Rec
Public Class Rec
Dim Gr As Graphics
Dim X, Y As Integer
Dim frm As Form
WithEvents TIMER As New Timer
Dim bmp As Bitmap
Dim PB As PictureBox
Public Sub New(ByVal g As Graphics, ByVal form As Form)
X = Rnd() * 100
Y = Rnd() * 200
Gr = g
Me.frm = form
ini()
justAddX = True
justAddY = True
End Sub
' We could use the graphics object here to draw on the form. But since
GDI+
' lacks the ideal performance, we use picture boxes and add them to the
' spacified form instead of using the drawing object:
Sub ini()
TIMER.Interval = Rnd() * 10 + 1
TIMER.Enabled = True
PB = New PictureBox
With PB
.BackColor = Color.FromArgb(Rnd() * 150, Rnd() * 200, Rnd() *
230)
.Left = X
.Top = Y
.Height = 6
.Width = 6
End With
frm.Controls.Add(PB)
End Sub
' Use these two boolean to know when to add or substarct numbers
' the movement of this class object.
Dim justAddX, justAddY As Boolean
Private Sub TIMER_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TIMER.Tick
MoveMe()
End Sub
' CuurentNum X , Y are the numbers added to the speed of X ,Y
' which creates a kind of change when the Rec hits the walls:
Dim CurrentNumX As Integer = 1
Dim CurrentNumY As Integer = 1
Sub MoveMe()
If X > (frm.Width - PB.Width) - 10 Then
justAddX = False
CurrentNumX = Rnd() * 50
End If
If Y > (frm.Height - PB.Height) - 30 Then
justAddY = False
CurrentNumY = Rnd() * 40
End If
If X < 0 Then
justAddX = True
CurrentNumX = Rnd() * 80
End If
If Y < 0 Then
justAddY = True
CurrentNumY = Rnd() * 30
End If
If justAddX Then
X += CurrentNumX
Else
X -= CurrentNumX
End If
If justAddY Then
Y += CurrentNumY
Else
Y -= CurrentNumY
End If
PB.Left = X
PB.Top = Y
End Sub
End Class
'------------------------------------------------------- End of
CODE------------
picture boxes that behave in a random order after they have been
instantiated and added to a form. When I create 15 or more instances of my
class, the whole program runs slowly in a way that I have to close the
program. I have tried to create a new thread for each class, but that throws
an exception , because a separated thread can't be added to a form from a
child class.
My code is this ( please tell me if there are better ways for doing it)
coplile with :
vbc [filename]
/r:system.dll,system.windows.forms.dll,system.drawing.dll,microsoft.visualbasic.dll
/t:winexe /main:form1
'----------------------------------- Start of CODE
Imports System.Math
imports Microsoft.VisualBasic
imports System.drawing
imports system.windows.forms
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(384, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
CreateOBJ()
End Sub
Sub CreateOBJ()
Dim myRec As Rec = New Rec(Me.CreateGraphics, Me)
End Sub
End Class
'--------------------------------------------------- Class Rec
Public Class Rec
Dim Gr As Graphics
Dim X, Y As Integer
Dim frm As Form
WithEvents TIMER As New Timer
Dim bmp As Bitmap
Dim PB As PictureBox
Public Sub New(ByVal g As Graphics, ByVal form As Form)
X = Rnd() * 100
Y = Rnd() * 200
Gr = g
Me.frm = form
ini()
justAddX = True
justAddY = True
End Sub
' We could use the graphics object here to draw on the form. But since
GDI+
' lacks the ideal performance, we use picture boxes and add them to the
' spacified form instead of using the drawing object:
Sub ini()
TIMER.Interval = Rnd() * 10 + 1
TIMER.Enabled = True
PB = New PictureBox
With PB
.BackColor = Color.FromArgb(Rnd() * 150, Rnd() * 200, Rnd() *
230)
.Left = X
.Top = Y
.Height = 6
.Width = 6
End With
frm.Controls.Add(PB)
End Sub
' Use these two boolean to know when to add or substarct numbers
' the movement of this class object.
Dim justAddX, justAddY As Boolean
Private Sub TIMER_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TIMER.Tick
MoveMe()
End Sub
' CuurentNum X , Y are the numbers added to the speed of X ,Y
' which creates a kind of change when the Rec hits the walls:
Dim CurrentNumX As Integer = 1
Dim CurrentNumY As Integer = 1
Sub MoveMe()
If X > (frm.Width - PB.Width) - 10 Then
justAddX = False
CurrentNumX = Rnd() * 50
End If
If Y > (frm.Height - PB.Height) - 30 Then
justAddY = False
CurrentNumY = Rnd() * 40
End If
If X < 0 Then
justAddX = True
CurrentNumX = Rnd() * 80
End If
If Y < 0 Then
justAddY = True
CurrentNumY = Rnd() * 30
End If
If justAddX Then
X += CurrentNumX
Else
X -= CurrentNumX
End If
If justAddY Then
Y += CurrentNumY
Else
Y -= CurrentNumY
End If
PB.Left = X
PB.Top = Y
End Sub
End Class
'------------------------------------------------------- End of
CODE------------