labels and panels in VB .NET code
labels and panels in VB .NET code
labels and panels in VB .NET code
labels and panels in VB .NET code
labels and panels in VB .NET code
labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code
labels and panels in VB .NET code labels and panels in VB .NET code
labels and panels in VB .NET code
Go Back  Xtreme Visual Basic Talk > > > labels and panels in VB .NET code


Reply
 
Thread Tools Display Modes
  #1  
Old 02-06-2014, 12:21 AM
aging warrior aging warrior is offline
Newcomer
 
Join Date: Feb 2014
Posts: 4
Unhappy labels and panels in VB .NET code


I'm trying to create a program that will display a row of 10 panels across the top of the screen. Within each panel is a set of 3 small labels, each of which will display data calculated elsewhere in the program.

Now I know I could simply format this within the designer, but I wanted to attempt painting this format within the VB .NET 2003 code as a self-educational exercise for future use.

When I run the program, the panels appear correctly across the top of the form. But after days of trying everything imaginable, I am unable to get any of the labels to appear. All I get are 10 empty panels. Could someone please offer some guidance? Thanks in advance for the help.

For the sake of expediency, I've included one of the labels' setup code. The code for the other 2 labels is virtually identical. What is shown below would be the first of the label code to be executed.

Two subs (executed in the order shown) appear below:


Public Sub panelsetup()

objectsetupcounter = 1
panelx = 16
panely = 8


Do Until objectsetupcounter = 11

Dim panelname As New Panel
panelname.Name = "basicpanel" & objectsetupcounter
panelname.Location = New Point(panelx, panely)
panelx += 132
panelname.BackColor = SystemColors.Control
panelname.BorderStyle = BorderStyle.FixedSingle
panelname.Size = New Size(104, 64)

Me.Controls.Add(panelname)

objectsetupcounter += 1

Loop

End Sub
--------------------------------------------------------------------------

Public Sub basicnumbersetup()

objectsetupcounter = 1

Do Until objectsetupcounter = 11
Dim labelname As New Label
labelname.Name = "basicnumber" & objectsetupcounter
labelname.Location = New Point(0, 0)
labelname.Text = objectsetupcounter
labelname.BackColor = SystemColors.ActiveCaptionText
labelname.BorderStyle = BorderStyle.FixedSingle
labelname.Size = New Size(32, 30)
labelname.TextAlign = ContentAlignment.MiddleCenter
labelname.Font = New System.Drawing.Font("microsoftsanserif", 9)

panelname.Name = "basicpanel" & objectsetupcounter

panelname.Controls.Add(labelname)


objectsetupcounter += 1

Loop

End Sub
Reply With Quote
  #2  
Old 02-06-2014, 02:21 AM
PlausiblyDamp's Avatar
PlausiblyDamplabels and panels in VB .NET code PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You might need to set the visibility of the items to true if you want them to show up.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 02-06-2014, 06:38 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

I'm not an expert here but i did notice that you
had your label back color set to a very dark color
and the label text was set to black by default. This
prevented the text from displaying. All i got was black
squares for labels.

Next was the issue of having two sub to display panels
and labels. The panels displayed fine but only the last
label would display in the last panel.

Anyway, i combined both subs into one sub and now
everything works.

I don't know if this is what you were looking for, so i
hope i was of some help.

Code:
 Public Sub panelsetup()

        objectsetupcounter = 1
        panelx = 16
        panely = 8


        Do Until objectsetupcounter = 11

            panelname = New Panel
            panelname.Name = "basicpanel" & objectsetupcounter
            panelname.Location = New Point(panelx, panely)
            panelx += 132
            panelname.BackColor = SystemColors.Control
            panelname.BorderStyle = BorderStyle.FixedSingle
            panelname.Size = New Size(104, 64)

            Me.Controls.Add(panelname)

            labelname = New Label
            labelname.AutoSize = False
            labelname.Name = "basicnumber" & objectsetupcounter
            labelname.Location = New Point(0, 0)
            labelname.Size = New Size(30, 30)
            labelname.BackColor = Color.AntiqueWhite   'SystemColors.ActiveCaptionText
            labelname.BorderStyle = BorderStyle.FixedSingle
            labelname.TextAlign = ContentAlignment.MiddleCenter
            labelname.Font = New System.Drawing.Font("microsoftsanserif", 9)
            labelname.Text = objectsetupcounter

            panelname.Controls.Add(labelname)
            objectsetupcounter += 1

        Loop

    End Sub
Reply With Quote
  #4  
Old 02-06-2014, 07:29 AM
CharlieMay's Avatar
CharlieMay CharlieMay is offline
Regular
 
Join Date: Sep 2009
Location: Indiana
Posts: 89
Default

I see one problem in your basicnumbersetup code You're using panelname.Name and panelname.controls.add but I don't see where you control what panelname is.

Perhaps a better loop in the basicnumbersetup would be

Code:
    Public Sub basicnumbersetup()

        Dim objectsetupcounter As Integer = 1

        For Each panelname As Panel In Me.Controls.OfType(Of Panel)()
            Dim labelname As New Label
            labelname.Name = "basicnumber" & objectsetupcounter
            labelname.Location = New Point(0, 0)
            labelname.Text = objectsetupcounter.ToString
            labelname.BackColor = SystemColors.ActiveCaptionText
            labelname.BorderStyle = BorderStyle.FixedSingle
            labelname.Size = New Size(32, 30)
            labelname.TextAlign = ContentAlignment.MiddleCenter
            labelname.Font = New System.Drawing.Font("microsoftsanserif", 9)

            panelname.Name = "basicpanel" & objectsetupcounter

            panelname.Controls.Add(labelname)

            objectsetupcounter += 1

        Next

    End Sub
Notice that I use a for each panel in the form. This is what the OfType(Of T) is specifying. This ensures that you are only iterating through the controls on the form that are ofType panel.

Now since we've done this, panelname should point to the current panel in the loop allowing you to add the other controls to it.

Also, just because I have to ask...

You are calling these two subs from somewhere, right?
__________________
I thought I knew what I was doing...
...And then I turned on Option Strict
Reply With Quote
  #5  
Old 02-07-2014, 01:09 AM
aging warrior aging warrior is offline
Newcomer
 
Join Date: Feb 2014
Posts: 4
Thumbs up Thanks for your help.

First, thanks to PlausiblyDamp, VB_Alien and CharlieMay for replying. This is a heads-up group here.
After checking out PlausiblyDamp's suggestion (which made perfect sense), and trying out VB_Alien's and CharlieMay's code samples, the one that worked best for me was VB_Alien's in that I was able to eliminate one sub and also created made it possible for me to insert the code for the other two labels and handle the entire screen output in one sub.
I was very intrigued by CharlieMay's idea of using the Control Collection. But considering I've had no luck recently in getting control interations to work, I decided to proceed with VB_Alien's idea which is working beautifully.
Now I can proceed with the rest of this cursed program...one of those in which everything that could've gone wrong has.
But I'm going to tweak around with a simpler program to see if I can put CharlieMay's idea to use in the future.
Again, thanks to all of you for your help.
Reply With Quote
  #6  
Old 02-07-2014, 01:17 AM
aging warrior aging warrior is offline
Newcomer
 
Join Date: Feb 2014
Posts: 4
Default

VB_Alien:
Although your code sample is working nicely for me (and thanks), one question is still nagging at me.
Why did your code work where mine didn't? Was it because the panel setup and label setup were in different subs? What difference would that make? What tipped you off? Was this a scope issue? This is one mistake I really (x3) don't want to make again.
Reply With Quote
  #7  
Old 02-07-2014, 06:28 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

You had the first sub laying out all your panels first and
then in the second sub, you were trying to lay out all the
label controls inside the panels.

The problem i saw in that is that the second sub, didn't
know which panel to place the label in because you didn't
specify which panel the new label was to be placed in. There-fore,
all labels were being placed in the last panel.

In my adjustment of your code, there is only one loop to go
through and for every panel created, a label is being place in
it. Then after a new panel is created, the next label is being
placed in that one, since it is the currently active panel being
created.

I hope i explained that good enough.

Like i said, i'm no expert here. I just try to help where i can.
Reply With Quote
  #8  
Old 02-07-2014, 07:25 AM
CharlieMay's Avatar
CharlieMay CharlieMay is offline
Regular
 
Join Date: Sep 2009
Location: Indiana
Posts: 89
Default

You might also want to get into the habit of explicitly declaring your variables. Turning Option Strict On will force you to do this so that you don't end up getting some weird result because the compiler determined an improper type or you entered an incorrect value to the variable.

For example, in your code you have panelx = 16 and show no errors in your code.

If you were to then put panelx = "Hello World" you would still show no error but when you ran the program, an exception would occur forcing you to find and fix the problem.

In this case (unless you call panelsetup from the load event) the code would crash and you would receive an invalidCastException at run time on the panelname.location = New Point(panelx, panely)
The messagetext would show "Conversion from string "Hello World" to 'Integer' is not valid. This then forces you to dig through your code and find where you set one of the variables to an invalid type.

Had you had option strict on, then it would have immediately told you in the error window and made you fix the issue so that it wouldn't cause an issue later.

The same goes for your original code. Option Strict On would have notified you immediately that panelname is not declared. It may be inaccessible due to its protection level. Which is saying that in the current sub, you have not declared what panelname represents.

Since you were more than likely calling this from Form_load, the exception was buried and the program was allowed to run, giving you incorrect results. Had you wrapped your call to basicnumbersetup into a Try...Catch and displayed the exception message, you would have been directed to the two lines trying to use panelname in the basicnumbersetup sub.

Sorry for the long-winded post, I just wanted to make an observation that I think will save you a lot of grief in future endeavors.
__________________
I thought I knew what I was doing...
...And then I turned on Option Strict
Reply With Quote
  #9  
Old 02-07-2014, 12:05 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

I saw that to. I just declared panelx and panely at the top
of the form. I assumed that aging warrior had also done that
or else, errors would keep popping up.
Reply With Quote
  #10  
Old 02-07-2014, 12:12 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Another thing you could do aging warrior is to
create control arrays, so that you can create
click events for them. Here is a little sample code
on how to do that.

Code:
Public Class Form1

    Private pnlTest() As System.Windows.Forms.Panel
    Private lblTest() As System.Windows.Forms.Label

    Private panelx As Integer = 0
    Private panely As Integer = 0
    Private ControlCount As Integer

 Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim shtIndex As Integer

        For shtIndex = 0 To ControlCount
            RemoveHandler pnlTest(shtIndex).Click, AddressOf pnlTest_Click
            RemoveHandler lblTest(shtIndex).Click, AddressOf lblTest_Click
        Next

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ControlCount = 4
        panelsetup()
        LabelSetup()
    End Sub

    Private Sub panelsetup()

        Dim shtIndex As Integer
        ReDim pnlTest(ControlCount)

        panelx = 16
        panely = 8

        For shtIndex = 0 To ControlCount
            pnlTest(shtIndex) = New System.Windows.Forms.Panel

            pnlTest(shtIndex).Name = "BasicPanel" & shtIndex
            pnlTest(shtIndex).Tag = shtIndex
            pnlTest(shtIndex).Size() = New Size(104, 64)
            pnlTest(shtIndex).Location = New Point(panelx, panely)
            pnlTest(shtIndex).BackColor = SystemColors.Control
            pnlTest(shtIndex).BorderStyle = BorderStyle.FixedSingle

            Me.Controls.Add(pnlTest(shtIndex))

            panelx += 132

            'Attach handler reference
            AddHandler pnlTest(shtIndex).Click, AddressOf pnlTest_Click
        Next

    End Sub

    Private Sub LabelSetup()

        Dim shtIndex As Integer
        ReDim lblTest(ControlCount)

        For shtIndex = 0 To ControlCount
            lblTest(shtIndex) = New System.Windows.Forms.Label

            lblTest(shtIndex).AutoSize = False
            lblTest(shtIndex).Name = "BasicLabel" & shtIndex
            lblTest(shtIndex).Location = New Point(0, 0)
            lblTest(shtIndex).Size = New Size(30, 30)
            lblTest(shtIndex).BackColor = Color.AntiqueWhite
            lblTest(shtIndex).BorderStyle = BorderStyle.None
            lblTest(shtIndex).TextAlign = ContentAlignment.MiddleCenter
            lblTest(shtIndex).Font = New System.Drawing.Font("microsoftsanserif", 9)
            lblTest(shtIndex).Text = shtIndex

            pnlTest(shtIndex).Controls.Add(lblTest(shtIndex))

            'Attach handler reference
            AddHandler lblTest(shtIndex).Click, AddressOf lblTest_Click
        Next

    End Sub
    Private Sub lblTest_Click(ByVal sender As System.Object, ByVal e As EventArgs)

        Me.Text = "Label(" & sender.text & ")"
    End Sub

    Private Sub pnlTest_Click(ByVal sender As System.Object, ByVal e As EventArgs)

        Me.Text = "Panel(" & sender.tag & ")"
    End Sub
End Class
I went back to using two subs instead of one, just so
that the code could be read more easily.

Now you can click on the panels or labels and get their
index number.

Last edited by VB_Alien; 02-08-2014 at 06:01 AM. Reason: Forgot to add some code
Reply With Quote
  #11  
Old 02-10-2014, 01:22 AM
aging warrior aging warrior is offline
Newcomer
 
Join Date: Feb 2014
Posts: 4
Default

Whew!

You sure gave me a lot of great information to study and digest. But it was all dead on and very informative. I really (x3) appreciate your assistance. Thanks for being so generous with your time and expertise. You're lightyears ahead of me in terms of knowledge and experience...and everything you've passed along will come in handy, I'm sure. I'll being watching the forums for your future posts. I hate to pass up a chance to learn something. Again, thanks for your help.
Reply With Quote
  #12  
Old 02-10-2014, 08:20 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Like i said before, i'm no expert. Some of the stuff i
learned, i learned from people like Gruff, passel, OnError,
PlausiblyDamp and so many others.

Thank-you for the compliment though and i was glad that
i could help you out.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
labels and panels in VB .NET code
labels and panels in VB .NET code
labels and panels in VB .NET code labels and panels in VB .NET code
labels and panels in VB .NET code
labels and panels in VB .NET code
labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code labels and panels in VB .NET code
labels and panels in VB .NET code
labels and panels in VB .NET code
 
labels and panels in VB .NET code
labels and panels in VB .NET code
 
-->