How to create label programmatically but depends on DataSet count?

marksquall

Active member
Joined
Oct 17, 2009
Messages
28
Programming Experience
3-5
Dear members and administrators,

Hello guys. I hope you're all in good condition.

I have this little game program that has a high-score form that will display high score (top 5) coming from my Access file.

The result will be passed to a DataSet object named dsHighScore.

But I wonder, how would I create Label object/s programmatically? Because there are no guarantee that I will get top 5 high scores (what if there is only one player, and he played the game just once,so eventually if he loads the High Score Form, he is the only top high score player, right?). So if I only have three high scores extracted from the DataSet object, I will have only three Label in the form, that makes sense, I hope.

I hope someone could help me on this. Thank you.

Warm regards,

Mark Squall
 
Hi,

You could easily do this the way Herman has suggested but to add a few other options into the mix here are two other ideas for you:-

1) You could use a DataGridView and set its DataSource property to the top scores DataTable in your DataSet. That way the control dynamically displays all the top scores regardless of how many there are with zero additional coding.

2) You could use a ListView. After your DataTable has been added to the DataSet just iterate through the rows of the DataTable and add them to the ListView.

If you do want to create controls dynamically at runtime then you can easily do this by declaring a New control of whatever type you need and then add that control to the controls collection of the form. i.e:-

VB.NET:
Dim myLabel As New Label
With myLabel
  .Text = "Test Text"
  .Top = 10
  .Left = 10
End With
Me.Controls.Add(myLabel)

Hope that helps.

Cheers,

Ian
 
If you do want to use Labels then you can create them as IanRyder has suggested but rather than setting their Location explicitly, just add them to a FlowLayoutPanel or, more likely, a TableLayoutPanel and let it take care of the layout. If you create a TableLayoutPanel with one column and five rows then you can simply add up to five Labels to it and they will display in the column.
 
Eureka...got it! :)

Dear guys,

Hello, thank you so much for the information. I finally got it! But any way, here is the code I came up with. Using your idea IanRyder, this method will be called upon Form's Load event:

VB.NET:
[COLOR="#0000FF"]Private Sub[/COLOR] DisplayHighScores()
        [COLOR="#0000FF"]Dim[/COLOR] rowCount [COLOR="#0000FF"]As Integer [/COLOR]= 0
      
        rowCount = dsHighScore.Tables(0).Rows.Count

        [COLOR="#0000FF"]Dim[/COLOR] LblName(rowCount) [COLOR="#0000FF"]A[/COLOR]s Label
        

        [COLOR="#0000FF"]For[/COLOR] i [COLOR="#0000FF"]As Integer[/COLOR] = 1 [COLOR="#0000FF"]To[/COLOR] rowCount
            LblName(i - 1) = [COLOR="#0000FF"]New[/COLOR] Label
            [COLOR="#0000FF"]With[/COLOR] LblName(i - 1)
                .BringToFront()
                .Text = (dsHighScore.Tables(0).Rows(i - 1)("usersName")).ToString()
                .Location = [COLOR="#0000FF"]New[/COLOR] System.Drawing.Point(120, 30 * (5 + i))
                .ForeColor = System.Drawing.Color.Black
                .Font = New Font("Arial", 15, FontStyle.Bold)
                .TextAlign = ContentAlignment.MiddleLeft
                .Width = 160
                .Height = 35
            [COLOR="#0000FF"]End With[/COLOR]
                [COLOR="#0000FF"]Me[/COLOR].Controls.Add(LblName(i - 1))
        [COLOR="#0000FF"]Next[/COLOR]

If someone has a (much) better alternative/version to this, please, please do advice.

Again, thank you so much!


Respectfully Yours,

Mark Squall
 
Hi,

The code you have posted works fine but when adding controls to a controls collection you do not need to create an array of controls first. That is just wasted resources. I have also made a few enhancements to your variable declarations and loops to streamline your code. Have a look at this example and compare the difference with your own code:-

VB.NET:
Dim rowCount As Integer = dsHighScore.Tables(0).Rows.Count
   
For i As Integer = 0 To rowCount - 1
  Dim myLabel As New Label
  With myLabel
    .BringToFront()
    .Text = (dsHighScore.Tables(0).Rows(i)("usersName")).ToString()
    .Location = New System.Drawing.Point(120, 30 * (5 + i))
    .ForeColor = System.Drawing.Color.Black
    .Font = New Font("Arial", 15, FontStyle.Bold)
    .TextAlign = ContentAlignment.MiddleLeft
    .Width = 160
    .Height = 35
  End With
  Me.Controls.Add(myLabel)
Next

Hope that helps.

Cheers,

Ian
 
Superb.

Hello IanRyder,

Hi. I guess your code is more efficient than mine.

I never thought that you could use the same object again and again, what I mean is to use the same object to display different values.
I am always thinking of one-to-one: one player name (on DataSet row), one Label object: so if I have top twenty (20) high scores, I "need" array of 20 Label objects too.

But, like what I'd said, your code is efficient and straight to the point, and it's neat.

I tried to revise my code and it still outputs the same. Thank you for the advice in enhancing/revising my first code.

Thank you all. It really helps me a lot. Really cool! :cool:

Warmest regards,

Mark Squall
 
I never thought that you could use the same object again and again, what I mean is to use the same object to display different values.
That's because you can't. You're not using the word "object" properly. Each Label is an object so obviously you can't use the same object to display different objects. What you mean is the same variable to refer to multiple objects, but that's not really relevant because IanRyder's code doesn't use the same variable. The variable is declared inside the loop so it is a new variable every iteration. You could declare the variable outside the loop and then create the object inside the loop and assign it to the variable. Then you would be using one variable to refer to multiple objects:
VB.NET:
Dim myLabel As Label

For i As Integer = 0 To rowCount - 1
  myLabel = New Label
  With myLabel
 
Hi jmcilhinney.

But, is there any difference (memory usage, performance, etc.) between declaring variable outside the loop & declaring them inside?

Anyway, I see now what you mean. Thanks for clearing things for me, what I really meant is (really) is the variable...sorry for that. Cheers. :glee:
 
Last edited:
Back
Top