Click to See Complete Forum and Search --> : any mistake?


Nasty2
July 18th, 2005, 11:11 AM
on form click im creating a panel with 11 labels in it:
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:
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:
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

DSJ
July 18th, 2005, 11:44 AM
Move

Dim newLabel As New Label


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

Nasty2
July 18th, 2005, 11:56 AM
thanks for your reply.

So how exactly do i create 11 labels?

Nasty2
July 18th, 2005, 12:00 PM
ok thanks..... it works fine after i move the declaration of the label into the for loop.

thanks again

Nasty2
July 18th, 2005, 12:09 PM
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

DeepButi
July 19th, 2005, 04:50 AM
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:
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