[RESOLVED] Changing colors of an Object and inserting into another Object???
I'm dynamically creating controls and inserting them into a Panel. My problem is that I'm creating a Checkbox control (setting various properties) and then a Label control (setting various properties). I wish for the text for each of the controls to be Black and Red respectively. They are to be right beside each other with a blank space separating them. The code I have provided does this if the for loop contains 1 iteration. If their are more than 1 iteration, the Red Asterisk doesn't show up. I've tried adding spaces between each occurrence of the checkbox and it still won't show up. Anyway, here is my code:
Code:
If resp Is Nothing Then
Dim asterisk As New Label
asterisk.Text = "*"
For Each row As DataRow In ds2.Tables(0).Rows
Dim chk As New CheckBox
chk.AutoSize = True
chk.Top = 7
chk.BackColor = pnl1.BackColor
chk.ForeColor = Color.Black
chk.TextAlign = ContentAlignment.MiddleLeft
chk.Text = row("labelName")
chk.Left = tmpWidth
Me.pnl2.Controls.Add(chk)
If row("required") = "Y" Then
asterisk.Top = chk.Top
asterisk.Left = chk.Width
asterisk.ForeColor = Color.Red
Me.pnl2.Controls.Add(asterisk)
tmpWidth = tmpWidth + chk.Width + 25
Else
tmpWidth = tmpWidth + chk.Width
End If
AddHandler chk.Click, AddressOf CheckBox1_Click
Next
End If
Re: Changing colors of an Object and inserting into another Object???
sooo.... you need a label with an asterisk in it and a check box... for each row? right?
Seems to me then, your label would need to be created inside your loop... not outside...
-tg
Re: Changing colors of an Object and inserting into another Object???
If the For Loop has 1 iteration....the code does what it is supposed to do. The problem is when there are more than 1 iterations of the Loop. I think it has to do with the spacing between each CheckBox. The Asterisk is placed on the right side of the checkbox....so, it would seem that the subsequent checkboxes are overlaying the position of the Asterisk....but I can't be sure of that because I have tried incrementing the space between each checkbox and the asterisk still won't appear.
Re: Changing colors of an Object and inserting into another Object???
I'd suggest that you use a TableLayoutPanel. You simply set the number of columns to two and just alternately add the controls. The TLP will automatically grow to the appropriate number of rows.
Re: Changing colors of an Object and inserting into another Object???
As techgnome suggests the label should be defined in the loop and also
Code:
asterisk.Left = chk.Width
should be
Code:
asterisk.Left = chk.Right
Here is my test case (which is butchered from your code, so you cannot directly replace your code with this)...
Code:
Dim tmpwidth As Integer = 0
Dim asterisk As Label
For i As Integer = 0 To 5
asterisk = New Label
asterisk.AutoSize = True
asterisk.Text = "*"
Dim chk As New CheckBox
chk.AutoSize = True
chk.Top = 7
chk.BackColor = Me.BackColor
chk.ForeColor = Color.Black
chk.TextAlign = ContentAlignment.MiddleLeft
chk.Text = i.ToString
chk.Left = tmpwidth
Me.Controls.Add(chk)
If "Y" = "Y" Then
asterisk.Top = chk.Top
asterisk.Left = chk.Right
asterisk.ForeColor = Color.Red
Me.Controls.Add(asterisk)
tmpwidth = tmpwidth + chk.Width + 25
Else
tmpwidth = tmpwidth + chk.Width
End If
Next
EDIT: If you use a TLP as JM suggests then you don't need to worry about the positions.
Re: Changing colors of an Object and inserting into another Object???
Thanks guys...that worked!