Add Control during runtime
-
{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
-
{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
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 SubYou'll probably have to write functions to return values of how many labels you'll need as well as where to position them.
-
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 SubYou'll probably have to write functions to return values of how many labels you'll need as well as where to position them.
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.
-
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.
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
-
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
-
{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
-
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?
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 IntegerDo 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