Creating a user control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thorgal

    #1

    Creating a user control

    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.GetAll eCellen
    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(u cCel)
    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

    #2
    Re: Creating a user control


    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.GetAll eCellen
    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(u cCel)
    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

    Comment

    • Phill W.

      #3
      Re: Creating a user control

      Thorgal wrote:
      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.

      Comment

      • thorgal

        #4
        Re: Creating a user control


        "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kschreef in bericht
        news:eo306n$knv $1@south.jnrs.j a.net...
        Thorgal wrote:
        >
        > 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

        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








        Comment

        • Phill W.

          #5
          Re: Creating a user control

          thorgal wrote:
          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(u cCel)
          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.

          Comment

          • Thorgal

            #6
            Re: Creating a user control


            Phill W. schreef:
            thorgal wrote:
            >
            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(u cCel)
            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.

            Comment

            • Phill W.

              #7
              Re: Creating a user control

              Thorgal wrote:
              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.

              Comment

              • thorgal

                #8
                Re: Creating a user control


                "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kschreef in bericht
                news:eo5fou$cd1 $1@south.jnrs.j a.net...
                Thorgal wrote:
                >
                >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.
                Can you explain the Timer part please

                Thanks


                Comment

                • rowe_newsgroups

                  #9
                  Re: Creating a user control

                  Something like this?

                  Public Class ucCel

                  Sub New()
                  ' Do your contruction logic first

                  Dim t as Timer = New Timer()
                  AddHandler t.Tick, New EventHandler(Ad dressOf HandleTimer_Tic k)
                  t.Interval = 5000
                  t.Start()
                  End Sub

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

                  End Class

                  Thanks,

                  Seth Rowe


                  thorgal wrote:
                  "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kschreef in bericht
                  news:eo5fou$cd1 $1@south.jnrs.j a.net...
                  Thorgal wrote:
                  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.
                  >
                  Can you explain the Timer part please
                  >
                  Thanks

                  Comment

                  Working...