Creating a user control

  • Thread starter Thread starter Thorgal
  • Start date Start date
T

Thorgal

Hello,

I created a UserControl named ucCel. (This is for the moment just a
square with a label where I create a random number.

The problem is the following.

In my code in frmMain i try to create a various amount of ucCels on my
form like this.

Dim arlAlleCellen As New ArrayList
arlAlleCellen = clsCelDA.GetAlleCellen
Dim i As Integer = 0
Dim j As Integer = 0
For Each oCel As clsCel In arlAlleCellen
If i = 3 Then
j = 1
i = 0
End If
Dim ucCel As New ucCel(oCel)
Dim p As New Point
p.X = 0 + (ucCel.Width * i) + 50
p.Y = 0 + (ucCel.Height * j) + 50
ucCel.Location = p
pnlKleineCellen.Controls.Add(ucCel)
i = i + 1
Next

This works almost, but the random number i created in my ucCel is
somethimes the same, it is like it creates the Usercontrol to fast and
it can't create a random number fast enough. How can i solve thist
problem?
 
Thorgal schreef:
Hello,

I created a UserControl named ucCel. (This is for the moment just a
square with a label where I create a random number.

The problem is the following.

In my code in frmMain i try to create a various amount of ucCels on my
form like this.

Dim arlAlleCellen As New ArrayList
arlAlleCellen = clsCelDA.GetAlleCellen
Dim i As Integer = 0
Dim j As Integer = 0
For Each oCel As clsCel In arlAlleCellen
If i = 3 Then
j = 1
i = 0
End If
Dim ucCel As New ucCel(oCel)
Dim p As New Point
p.X = 0 + (ucCel.Width * i) + 50
p.Y = 0 + (ucCel.Height * j) + 50
ucCel.Location = p
pnlKleineCellen.Controls.Add(ucCel)
i = i + 1
Next

This works almost, but the random number i created in my ucCel is
somethimes the same, it is like it creates the Usercontrol to fast and
it can't create a random number fast enough. How can i solve thist
problem?


Or can i create a Thread for each UserControl???
and if so , how????

Thanks in advance
Peter
 
Thorgal said:
I created a UserControl named ucCel. (This is for the moment just a
square with a label where I create a random number.

So each ucCel contains a random number, presumably generated using
something like

Dim r as New Random
? = r.Next

Because the Random class is initialised based on current time, very
rapid creation of a number of them /could/ result in the same sequences
being produced.

Use a randomiser that is Shared across all instances of ucCel, so that
this one random number generator can follow its own sequence, no matter
how many instances you create, as in

Class ucCel
Inherits UserControl

Protected Shared Randomiser As New Random

Public Sub New()
_RandomNum = Randomiser.Next
End Sub

HTH,
Phill W.
 
Phill W. said:
So each ucCel contains a random number, presumably generated using
something like

Dim r as New Random
? = r.Next

Because the Random class is initialised based on current time, very rapid
creation of a number of them /could/ result in the same sequences being
produced.

Use a randomiser that is Shared across all instances of ucCel, so that
this one random number generator can follow its own sequence, no matter
how many instances you create, as in

Class ucCel
Inherits UserControl

Protected Shared Randomiser As New Random

Public Sub New()
_RandomNum = Randomiser.Next
End Sub


OK, that worked, but I stell get my cells next to each other without space
between them, how can i make sure, there's space between the cells, i that i
was doing that with following code:
And how can i create a thread for each cell?

p.X = 0 + (ucCel.Width * i) + 50
 
thorgal said:
OK, that worked, but I still get my cells next to each other without space
between them, how can i make sure, there's space between the cells

Your code should work (more or less) as given:

Dim i As Integer = 0
Dim j As Integer = 0
For Each oCel As clsCel In arlAlleCellen
If i = 3 Then
j = j + 1
i = 0
End If

Dim ucCel As New ucCel(oCel)
ucCel.Location = New Point( _
0 + (ucCel.Width * i) + 50 _
, 0 + (ucCel.Height * j) + 50 _
)
pnlKleineCellen.Controls.Add(ucCel)
i = i + 1
Next

Working through the code (and I'm guessing at a default size of, say
(100, 100), you would wind up with ouCel objects at these locations:

0 @ (50, 50), 1 @ (150, 50), 2 @ (250, 50),
3 @ (50, 150), 4 @ (150, 150), 5 @ (250, 150)
6 @ (50, 250), ...

I can't see why there would be no space between them.

Why do you feel the need for a Thread for each Cell?
What are they all doing?

Regards,
Phill W.
 
Phill W. schreef:
Your code should work (more or less) as given:

Dim i As Integer = 0
Dim j As Integer = 0
For Each oCel As clsCel In arlAlleCellen
If i = 3 Then
j = j + 1
i = 0
End If

Dim ucCel As New ucCel(oCel)
ucCel.Location = New Point( _
0 + (ucCel.Width * i) + 50 _
, 0 + (ucCel.Height * j) + 50 _
)
pnlKleineCellen.Controls.Add(ucCel)
i = i + 1
Next

Working through the code (and I'm guessing at a default size of, say
(100, 100), you would wind up with ouCel objects at these locations:

0 @ (50, 50), 1 @ (150, 50), 2 @ (250, 50),
3 @ (50, 150), 4 @ (150, 150), 5 @ (250, 150)
6 @ (50, 250), ...

I can't see why there would be no space between them.

Why do you feel the need for a Thread for each Cell?
What are they all doing?

Regards,
Phill W.


Found the solution: 0 + (ucCel.Width * i) + 50*i
That makes it seperated, without the *i they are just next to each
other instead of seperated

Each Cel represents an existing Cel. And I get from each cel every 5
seconds, the status and temperature from an OPC Server, so i would like
for
each Cel a different Thread so that i can manage each incomming
Temperature
and status as willing.

Hope you understand this, otherwise you can always ask for more
information.

Best regards
Peter D.
 
Thorgal said:
Found the solution: 0 + (ucCel.Width * i) + 50*i
That makes it seperated, without the *i they are just next to each
other instead of seperated

Well spotted.
Each Cel represents an existing Cel. And I get from each cel every 5
seconds, the status and temperature from an OPC Server, so i would like
for each Cel a different Thread so that i can manage each incomming
Temperature and status as willing.

You don't /need/ a Thread for this; a simple Timer within each
UserControl would do.

HTH,
Phill W.
 
Phill W. said:
Well spotted.


You don't /need/ a Thread for this; a simple Timer within each UserControl
would do.

HTH,
Phill W.

Can you explain the Timer part please

Thanks
 
Something like this?

Public Class ucCel

Sub New()
' Do your contruction logic first

Dim t as Timer = New Timer()
AddHandler t.Tick, New EventHandler(AddressOf HandleTimer_Tick)
t.Interval = 5000
t.Start()
End Sub

Private Sub HandleTimer_Tick(ByVal sender As Object, ByVal e As
EventArgs)
' Update the ucCel data here
End Sub

End Class

Thanks,

Seth Rowe
 
Back
Top