Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. [VB.NET 2008] How to get control on objects created at runtime

[VB.NET 2008] How to get control on objects created at runtime

Scheduled Pinned Locked Moved .NET (Core and Framework)
tutorialcsharpdatabasehelp
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    Hi, perhaps the subject is not so explanatory but now I try to explain with an example that well represents my problem. I have a UserControl with two NumericUpDown and a button, then I have a Form1 with a TabControl1 with just one page. Loading the form I add some pages to the TabControl and I put in every page my UserControl (called "base") in this way:

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim mypag As TabPage
    Dim myuc As base

    For i As Int32 = 0 To 10
      mypag = New TabPage
      myuc = New base
      myuc.Parent = mypag
      mypag.Text = "Pag" & i.ToString
      mypag.Name = "Pag" & i.ToString
      TabControl1.TabPages.Insert(1 + i, mypag)
    Next
    

    End Sub

    End Class

    Than with the button on the page of the TabControl I pass to the next page of the TabControl (and this is done) but I also want to put in the first NumericUpDown of the "next page" the sum of the value of the two NumericUpDown in the current page (the one with the button just pressed) and I don't know how to do this. The code in the button is:

    Public Class base

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim tmp, id As Int32

    'sum value
    tmp = NUD1.Value + NUD2.Value  'NUD1 and NUD2 are two NumericUpDown
    'index of the current page
    id = CType(Parent.Parent, TabControl).SelectedIndex
    'go to next page
    CType(Parent.Parent, TabControl).SelectedIndex += 1
    

    End Sub

    End Class

    I don't know how to "poit" to the NumericUpDown of a page different from the one in witch I am, even if I can get the index of that page (I think...) I tryed something like this:

    'this should be the next page
    CType(Parent.Parent, TabControl).TabPages(id + 1)
    'but this doesn't work:
    CType(Parent.Parent, TabControl).TabPages(id + 1).NUD1.Value
    'NUD1 is not a member of System.Windows.Forms.TabPage

    Can someone give me some advice? Thanks in advance

    L 1 Reply Last reply
    0
    • S steve_9496613

      Hi, perhaps the subject is not so explanatory but now I try to explain with an example that well represents my problem. I have a UserControl with two NumericUpDown and a button, then I have a Form1 with a TabControl1 with just one page. Loading the form I add some pages to the TabControl and I put in every page my UserControl (called "base") in this way:

      Public Class Form1

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim mypag As TabPage
      Dim myuc As base

      For i As Int32 = 0 To 10
        mypag = New TabPage
        myuc = New base
        myuc.Parent = mypag
        mypag.Text = "Pag" & i.ToString
        mypag.Name = "Pag" & i.ToString
        TabControl1.TabPages.Insert(1 + i, mypag)
      Next
      

      End Sub

      End Class

      Than with the button on the page of the TabControl I pass to the next page of the TabControl (and this is done) but I also want to put in the first NumericUpDown of the "next page" the sum of the value of the two NumericUpDown in the current page (the one with the button just pressed) and I don't know how to do this. The code in the button is:

      Public Class base

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim tmp, id As Int32

      'sum value
      tmp = NUD1.Value + NUD2.Value  'NUD1 and NUD2 are two NumericUpDown
      'index of the current page
      id = CType(Parent.Parent, TabControl).SelectedIndex
      'go to next page
      CType(Parent.Parent, TabControl).SelectedIndex += 1
      

      End Sub

      End Class

      I don't know how to "poit" to the NumericUpDown of a page different from the one in witch I am, even if I can get the index of that page (I think...) I tryed something like this:

      'this should be the next page
      CType(Parent.Parent, TabControl).TabPages(id + 1)
      'but this doesn't work:
      CType(Parent.Parent, TabControl).TabPages(id + 1).NUD1.Value
      'NUD1 is not a member of System.Windows.Forms.TabPage

      Can someone give me some advice? Thanks in advance

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Where are you creating the NumericUpDown controls? At that point, you'd like to keep a reference to it, at class-level.

      Public Class Form1: Form
      Private n1 as NumericUpDown

      Sub New()
      n1 = new NumericUpDown
      Controls.Add(n1)
      End Sub
      End Class

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

      S 1 Reply Last reply
      0
      • L Lost User

        Where are you creating the NumericUpDown controls? At that point, you'd like to keep a reference to it, at class-level.

        Public Class Form1: Form
        Private n1 as NumericUpDown

        Sub New()
        n1 = new NumericUpDown
        Controls.Add(n1)
        End Sub
        End Class

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

        S Offline
        S Offline
        steve_9496613
        wrote on last edited by
        #3

        The NumericUpDown controls are in the UserControl I called "base" (together with the button), not in Form1, and I created them in the design mode, not writing the code as in your example. In Form1 there is the TabControl in witch at runtime I put 10 pages, each one with a copy of "base" inside. I did this because I needed a series of screens all the same and this was the most convenient way because I had to design only one screen (and also the build is faster). The final result should be a series of tabbed pages all the same, the user begins to fill in the first, then the second and at that moment some fields on the page are automatically filled with the data entered on the previous page, and so on page after page. All the controls are in the UserControl so I think I have to manage this mechanism inside the UserControl (perhaps the button to pass to the next page is not the better place to put the code in because the user can go to the next page also by clicking the tab of the page but in the real page there are also other controls I can use like a CheckBox that must be checked to enable the other controls). My problem is that when the user is in the page N, I don't know how to reach the controls in page N-1 or N+1. If I simply design 10 pages in the TabControl, all the same, there is no problem because each control on each page is a single object with a unique name so I have no problem to address it, but this is not the most elegant solution..

        L 1 Reply Last reply
        0
        • S steve_9496613

          The NumericUpDown controls are in the UserControl I called "base" (together with the button), not in Form1, and I created them in the design mode, not writing the code as in your example. In Form1 there is the TabControl in witch at runtime I put 10 pages, each one with a copy of "base" inside. I did this because I needed a series of screens all the same and this was the most convenient way because I had to design only one screen (and also the build is faster). The final result should be a series of tabbed pages all the same, the user begins to fill in the first, then the second and at that moment some fields on the page are automatically filled with the data entered on the previous page, and so on page after page. All the controls are in the UserControl so I think I have to manage this mechanism inside the UserControl (perhaps the button to pass to the next page is not the better place to put the code in because the user can go to the next page also by clicking the tab of the page but in the real page there are also other controls I can use like a CheckBox that must be checked to enable the other controls). My problem is that when the user is in the page N, I don't know how to reach the controls in page N-1 or N+1. If I simply design 10 pages in the TabControl, all the same, there is no problem because each control on each page is a single object with a unique name so I have no problem to address it, but this is not the most elegant solution..

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          steve_9496613 wrote:

          and I created them in the design mode

          Topic title says "created at runtime", not the designer. If you're using the designer then you can give the controls a unique name indeed, and use that - which is the elegant solution; they'd have a reference on the form-level. There's no need to look them up through the "current" tabpage, as far as the form is concerned, all pages exist.

          steve_9496613 wrote:

          My problem is that when the user is in the page N, I don't know how to reach the controls in page N-1 or N+1.

          The same as you reach the tabpage;

          NumericUpDown1.Value = 1

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

          S 1 Reply Last reply
          0
          • L Lost User

            steve_9496613 wrote:

            and I created them in the design mode

            Topic title says "created at runtime", not the designer. If you're using the designer then you can give the controls a unique name indeed, and use that - which is the elegant solution; they'd have a reference on the form-level. There's no need to look them up through the "current" tabpage, as far as the form is concerned, all pages exist.

            steve_9496613 wrote:

            My problem is that when the user is in the page N, I don't know how to reach the controls in page N-1 or N+1.

            The same as you reach the tabpage;

            NumericUpDown1.Value = 1

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

            S Offline
            S Offline
            steve_9496613
            wrote on last edited by
            #5

            Sorry, I was not able to explain clearly. Now I try again. In the designer I create: 1 Form with 1 TabControl with 1 page 1 UserControl with 1 Button and 2 NumericUpDown (in my first post there is the code of the Form and the code of the UserControl) At runtime I add 10 pages to the TabControl with a copy of the UserControl in each page. My problem is in these 10 pages, for this reason I wrote "at runtime" in the topic title. As you said, I know that if I create all the pages in the designer there is no problem but if I create all the pages at runtime I have to "design" just one page and to write code just for this page (less code I write, fewer errors I do... and then, if I have to change something in the page, I have to change just one page, not 10) Is there a way to do what I want to do (hoping it is clear what I want to do...)?

            L 1 Reply Last reply
            0
            • S steve_9496613

              Sorry, I was not able to explain clearly. Now I try again. In the designer I create: 1 Form with 1 TabControl with 1 page 1 UserControl with 1 Button and 2 NumericUpDown (in my first post there is the code of the Form and the code of the UserControl) At runtime I add 10 pages to the TabControl with a copy of the UserControl in each page. My problem is in these 10 pages, for this reason I wrote "at runtime" in the topic title. As you said, I know that if I create all the pages in the designer there is no problem but if I create all the pages at runtime I have to "design" just one page and to write code just for this page (less code I write, fewer errors I do... and then, if I have to change something in the page, I have to change just one page, not 10) Is there a way to do what I want to do (hoping it is clear what I want to do...)?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              steve_9496613 wrote:

              In the designer I create: 1 Form with 1 TabControl with 1 page 1 UserControl with 1 Button and 2 NumericUpDown (in my first post there is the code of the Form and the code of the UserControl)
              At runtime I add 10 pages to the TabControl with a copy of the UserControl in each page.
              My problem is in these 10 pages, for this reason I wrote "at runtime" in the topic title.

              Good explanation :thumbsup: "NUD1", the name-property of the UpDown control, must be unique within the form. It'll generate a variable with that name, and that's what "NUD1" points to; it's not a property of the tabpage, it's the name of a single control. Ideally, you'd have your own type of tabpage, but I don't know whether the designer would accept that. Might make it impossible to show the page at all in the designer. The "easy" way is to loop trough all the controls on the current tabpage. There's a Controls-collection that you'd have to loop through, and check whether that's the control (of type numericupdown) that you're looking for. Not very elegant at all, is it? So, I'd suggest putting everything you want on that page in a single UserControl, and make it's values available over properties. That way you can instantiate the same control ten times, and still have all the logic and layout in a single place.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

              S 1 Reply Last reply
              0
              • L Lost User

                steve_9496613 wrote:

                In the designer I create: 1 Form with 1 TabControl with 1 page 1 UserControl with 1 Button and 2 NumericUpDown (in my first post there is the code of the Form and the code of the UserControl)
                At runtime I add 10 pages to the TabControl with a copy of the UserControl in each page.
                My problem is in these 10 pages, for this reason I wrote "at runtime" in the topic title.

                Good explanation :thumbsup: "NUD1", the name-property of the UpDown control, must be unique within the form. It'll generate a variable with that name, and that's what "NUD1" points to; it's not a property of the tabpage, it's the name of a single control. Ideally, you'd have your own type of tabpage, but I don't know whether the designer would accept that. Might make it impossible to show the page at all in the designer. The "easy" way is to loop trough all the controls on the current tabpage. There's a Controls-collection that you'd have to loop through, and check whether that's the control (of type numericupdown) that you're looking for. Not very elegant at all, is it? So, I'd suggest putting everything you want on that page in a single UserControl, and make it's values available over properties. That way you can instantiate the same control ten times, and still have all the logic and layout in a single place.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                S Offline
                S Offline
                steve_9496613
                wrote on last edited by
                #7

                Thanks for your reply. Try and try at the end I've found a solution that I'll show you, but I have some questions before.

                Eddy Vluggen wrote:

                "NUD1", the name-property of the UpDown control, must be unique within the form. It'll generate a variable with that name, and that's what "NUD1" points to; it's not a property of the tabpage, it's the name of a single control.

                ...and that was the problem: I put a NumericUpDown control in a UserControl and I give it an unique name (NUD1). When, at runtime, I create 10 instances of the UserControl, what are the unique names of the 10 NumericUpDown controls originated from NUD1? If I make the value of the NumericUpDown available over a property like this (if I'm not wrong):

                Public Property NUDVal() As Int32
                Get
                Return NUD1.Value
                End Get
                Set(ByVal value As Int32)
                NUD1.Value = value
                End Set
                End Property

                also this property has a unique name... until the UserControl is one, but when I create 10 instances of the UserControl? So I thought to lists of objects... and that is my solution. In the UserControl I created my two NumericUpDown controls at runtime, not in the designer, and I added them to an ArrayList. I also added a CheckBox, in the designer, to use the event "Click" for calculations that serve. In Form1, the main form, when I create the 10 instances of the UserControl, I also add the ArrayList of controls of every UserControl to another ArrayList declared in the form... here is the code:

                'User control
                Public Class base
                'list that will contain the controls
                Public Objs As ArrayList = New ArrayList
                'controls
                Private n1, n2 As NumericUpDown

                Sub New()
                Dim pos As Point

                ' Chiamata richiesta da Progettazione Windows Form.
                InitializeComponent()
                
                ' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
                n1 = New NumericUpDown
                pos.X = 209
                pos.Y = 90
                n1.Location = pos
                Controls.Add(n1)
                n2 = New NumericUpDown
                pos.X = 209
                pos.Y = 132
                n2.Location = pos
                Controls.Add(n2)
                'I add the controls to the list
                Objs.Add(n1)
                Objs.Add(n2)
                

                End Sub

                Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                'go to next page
                CType(Parent.Parent, TabControl).SelectedIndex += 1
                End Sub

                Private Sub CheckBox1_Click(ByVal sender As System.Obj

                L 1 Reply Last reply
                0
                • S steve_9496613

                  Thanks for your reply. Try and try at the end I've found a solution that I'll show you, but I have some questions before.

                  Eddy Vluggen wrote:

                  "NUD1", the name-property of the UpDown control, must be unique within the form. It'll generate a variable with that name, and that's what "NUD1" points to; it's not a property of the tabpage, it's the name of a single control.

                  ...and that was the problem: I put a NumericUpDown control in a UserControl and I give it an unique name (NUD1). When, at runtime, I create 10 instances of the UserControl, what are the unique names of the 10 NumericUpDown controls originated from NUD1? If I make the value of the NumericUpDown available over a property like this (if I'm not wrong):

                  Public Property NUDVal() As Int32
                  Get
                  Return NUD1.Value
                  End Get
                  Set(ByVal value As Int32)
                  NUD1.Value = value
                  End Set
                  End Property

                  also this property has a unique name... until the UserControl is one, but when I create 10 instances of the UserControl? So I thought to lists of objects... and that is my solution. In the UserControl I created my two NumericUpDown controls at runtime, not in the designer, and I added them to an ArrayList. I also added a CheckBox, in the designer, to use the event "Click" for calculations that serve. In Form1, the main form, when I create the 10 instances of the UserControl, I also add the ArrayList of controls of every UserControl to another ArrayList declared in the form... here is the code:

                  'User control
                  Public Class base
                  'list that will contain the controls
                  Public Objs As ArrayList = New ArrayList
                  'controls
                  Private n1, n2 As NumericUpDown

                  Sub New()
                  Dim pos As Point

                  ' Chiamata richiesta da Progettazione Windows Form.
                  InitializeComponent()
                  
                  ' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
                  n1 = New NumericUpDown
                  pos.X = 209
                  pos.Y = 90
                  n1.Location = pos
                  Controls.Add(n1)
                  n2 = New NumericUpDown
                  pos.X = 209
                  pos.Y = 132
                  n2.Location = pos
                  Controls.Add(n2)
                  'I add the controls to the list
                  Objs.Add(n1)
                  Objs.Add(n2)
                  

                  End Sub

                  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                  'go to next page
                  CType(Parent.Parent, TabControl).SelectedIndex += 1
                  End Sub

                  Private Sub CheckBox1_Click(ByVal sender As System.Obj

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  steve_9496613 wrote:

                  ...and that was the problem: I put a NumericUpDown control in a UserControl and I give it an unique name (NUD1). When, at runtime, I create 10 instances of the UserControl, what are the unique names of the 10 NumericUpDown controls originated from NUD1?

                  Good assesment; the others wouldn't have a name.

                  steve_9496613 wrote:

                  also this property has a unique name... until the UserControl is one, but when I create 10 instances of the UserControl?

                  You'd have 10 controls without a name (the usercontrol), but with a known property that points to the correct control.

                  steve_9496613 wrote:

                  Well... it works... although I have some difficulty in evaluate the elegance of this code...
                  What do you think about this solution?

                  It'll do; seen the technique before, usually when ex-VB programmers want an "array" of controls. It adds a single pointer to the list for each control, so it's not very resource-intensive.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups