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. Visual Basic
  4. Add Control during runtime

Add Control during runtime

Scheduled Pinned Locked Moved Visual Basic
csharphelptutorial
7 Posts 3 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.
  • B Offline
    B Offline
    bamnet
    wrote on last edited by
    #1

    {vb .net 2003} I'd like to add controls (specifically labels) to a form when its resized, to have the most number of labels possible that fit on the screen. I haven't had much luck finding a tutorial or code snippet that does this. Controls would be created using a for or while loop, so i need some way to easily create lots of them. I have the resize event down fine, its the dynamic creation that I'd like help on. Thanks in advance, BAM

    D B 2 Replies Last reply
    0
    • B bamnet

      {vb .net 2003} I'd like to add controls (specifically labels) to a form when its resized, to have the most number of labels possible that fit on the screen. I haven't had much luck finding a tutorial or code snippet that does this. Controls would be created using a for or while loop, so i need some way to easily create lots of them. I have the resize event down fine, its the dynamic creation that I'd like help on. Thanks in advance, BAM

      D Offline
      D Offline
      Dave Sexton
      wrote on last edited by
      #2

      bamnet wrote:

      have the most number of labels possible that fit on the screen.

      bamnet wrote:

      create lots of them

      Personally, I think that's a bad idea. the more controls you have on a form the longer it'll take to show - consider users with lower-end machines. The code you'd be needing would look something like this (out my head)

      Dim MyLabel As Label 'declare at modular level

      Private Sub CreateLabels()
      'create a new instance of a label
      MyLabel = New Label
      'add formatting, positioning, event handlers, etc. here
      Me.Controls.Add(MyLabel) 'add the label to the form.
      End Sub

      You'll probably have to write functions to return values of how many labels you'll need as well as where to position them.

      B 1 Reply Last reply
      0
      • D Dave Sexton

        bamnet wrote:

        have the most number of labels possible that fit on the screen.

        bamnet wrote:

        create lots of them

        Personally, I think that's a bad idea. the more controls you have on a form the longer it'll take to show - consider users with lower-end machines. The code you'd be needing would look something like this (out my head)

        Dim MyLabel As Label 'declare at modular level

        Private Sub CreateLabels()
        'create a new instance of a label
        MyLabel = New Label
        'add formatting, positioning, event handlers, etc. here
        Me.Controls.Add(MyLabel) 'add the label to the form.
        End Sub

        You'll probably have to write functions to return values of how many labels you'll need as well as where to position them.

        B Offline
        B Offline
        bamnet
        wrote on last edited by
        #3

        I don't have to consider users with lower end machines very much. The application is going to be used on one or maybe 2 machines maximum. The idea is to create a few labels on a panel on the lefthand side based on the screen resolution. In my previous draft of this program everything was hardcoded and changing the resolution and size of the application meant rewriting it. The most labels would be 10 max.. not too bad I think. I've tried that code an I can't get it to display the label, I know the spot on the form is clear and set visible to true.

        D 1 Reply Last reply
        0
        • B bamnet

          I don't have to consider users with lower end machines very much. The application is going to be used on one or maybe 2 machines maximum. The idea is to create a few labels on a panel on the lefthand side based on the screen resolution. In my previous draft of this program everything was hardcoded and changing the resolution and size of the application meant rewriting it. The most labels would be 10 max.. not too bad I think. I've tried that code an I can't get it to display the label, I know the spot on the form is clear and set visible to true.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You added the labels to the form. They're probably sitting behind your panel control. Add them to the Panel's Controls collection instead.

          Dave Kreskowiak Microsoft MVP - Visual Basic

          B 1 Reply Last reply
          0
          • D Dave Kreskowiak

            You added the labels to the form. They're probably sitting behind your panel control. Add them to the Panel's Controls collection instead.

            Dave Kreskowiak Microsoft MVP - Visual Basic

            B Offline
            B Offline
            bamnet
            wrote on last edited by
            #5

            I agree, I have to add them to the panel. Me.PanelL.Controls.Add(newButton) accomplished this task

            1 Reply Last reply
            0
            • B bamnet

              {vb .net 2003} I'd like to add controls (specifically labels) to a form when its resized, to have the most number of labels possible that fit on the screen. I haven't had much luck finding a tutorial or code snippet that does this. Controls would be created using a for or while loop, so i need some way to easily create lots of them. I have the resize event down fine, its the dynamic creation that I'd like help on. Thanks in advance, BAM

              B Offline
              B Offline
              bamnet
              wrote on last edited by
              #6

              The next part is doing this with a loop. I can create one element and add it to the panel, but how would I create x number of them?

              D 1 Reply Last reply
              0
              • B bamnet

                The next part is doing this with a loop. I can create one element and add it to the panel, but how would I create x number of them?

                D Offline
                D Offline
                Dave Sexton
                wrote on last edited by
                #7

                Looping is easy, the problem I think you'll encounter is not adding more than one label but determining how many too add. Modifying the code above slightly to demonstrate the loop -

                Dim MyLabel As Label 'declare at modular level
                'pass the value of the number of labels you'll need to
                'the CreateLabels sub
                Private Sub CreateLabels(ByVal NumberOfLabels as Integer)
                Dim i As Integer

                Do Until i = NumberOfLabels
                  'create a new instance of a label
                  MyLabel = New Label
                  'add formatting, positioning, event handlers, etc. here
                  Me.PanelL.Controls.Add(MyLabel) 'add the label to the panel
                  i += 1 'increment i so as to avoid an infinite loop
                 Loop
                

                End Sub

                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