CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: any mistake?

  1. #1
    Join Date
    Feb 2005
    Posts
    176

    [SOLVED] any mistake?

    on form click im creating a panel with 11 labels in it:
    Code:
    Private Sub DrawEntity(ByVal mouseDownPoint As System.Drawing.Point)
            Dim panelSize As New System.Drawing.Size(150, 250)
    
            Dim newPanel As New Panel
            newPanel.Location = mouseDownPoint
            newPanel.BorderStyle = BorderStyle.FixedSingle
            newPanel.Size = panelSize
            Me.Controls.Add(newPanel)
    
            Dim newLabel As New Label
            Dim x As Integer = 10
            For cntr As Integer = 0 To 10
                newLabel.Parent = newPanel
                newLabel.Dock = DockStyle.Top
                newLabel.Tag = x
                x = x - 1
                newLabel.TextAlign = ContentAlignment.MiddleCenter
    
                If cntr = 10 Then
                    newLabel.Font = New System.Drawing.Font("Times New Roman", 10, FontStyle.Bold)
                Else
                    newLabel.Font = New System.Drawing.Font("Times New Roman", 10, FontStyle.Regular)
                End If
    
                newPanel.Controls.Add(newLabel)
            Next
    I am then opening form 2 that has 11 textboxes on it..... when the user clicks a button, the text of the textboxes is put into an array:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ctl As Control
            Dim x As Integer
    
            For Each ctl In Me.Controls
                If ctl.GetType.ToString = "System.Windows.Forms.TextBox" Then
                    x = ctl.Tag
                    TableField(x) = ctl.Text
                End If
            Next
    End Sub
    I then close form2 and would like to get the text from the array and fill the labels that are in the panel. I currently have the following:
    Code:
    Dim ctl As Control
            Dim lab As System.Windows.Forms.Label
            For Each ctl In Me.Controls
                If ctl.GetType.ToString = "System.Windows.Forms.Panel" Then
                    For Each lab In ctl.Controls
                        lab.Text = TableField(lab.Tag)
                    Next
                End If
            Next
    The problem is that only the top label (Tag = 10) is getting filled up with text. I dont know if the problem is in the creation of the labels or when i fill them with text.

    Please help
    thanks
    Last edited by Nasty2; July 19th, 2005 at 01:27 AM.

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: any mistake?

    Move

    Dim newLabel As New Label


    into your For loop... you're only instaciating one and changing it's value.

  3. #3
    Join Date
    Feb 2005
    Posts
    176

    Re: any mistake?

    thanks for your reply.

    So how exactly do i create 11 labels?

  4. #4
    Join Date
    Feb 2005
    Posts
    176

    Re: any mistake?

    ok thanks..... it works fine after i move the declaration of the label into the for loop.

    thanks again

  5. #5
    Join Date
    Feb 2005
    Posts
    176

    Re: any mistake?

    sorry to be a pest..... but its working well now......
    except that if the user clicks on the form a second time to create another panel with another set of labels in it....... and sets the text for the labels...... the text of the first set of labels is changed again and becomes the same as the second set.

    the first set of labels should keep their text value

    Thanks again
    Last edited by Nasty2; July 18th, 2005 at 12:11 PM.

  6. #6
    Join Date
    Dec 2003
    Location
    St. Cugat - Catalunya
    Posts
    441

    Re: any mistake?

    Nasty2,
    you assign the values based on the Tag, so the second time you assign values to both sets of labels because you have two labels with Tag=0, two with Tag=1 ... and son on.

    To avoid this, modify the Tag once the label gets his value:
    Code:
    Dim ctl As Control
    Dim lab As System.Windows.Forms.Label
    For Each ctl In Me.Controls
        If ctl.GetType.ToString = "System.Windows.Forms.Panel" Then
            For Each lab In ctl.Controls
                If lab.Tag >= 0 Then
                    lab.Text = TableField(lab.Tag)
                    lab.Tag = -1
                End If
            Next
        End If
    Next
    or something similar.

    Another way: identify the panel number and set only the labels on the correct panel.

    Hope it helps
    Last edited by DeepButi; July 19th, 2005 at 04:55 AM.
    Did it help? rate it.

    The best conversation I had was over forty million years ago ... and that was with a coffee machine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured