Results 1 to 6 of 6

Thread: [RESOLVED] Changing colors of an Object and inserting into another Object???

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [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
    Blake

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    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.
    Blake

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.
    Last edited by Bulldog; Jun 19th, 2007 at 07:36 PM.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Changing colors of an Object and inserting into another Object???

    Thanks guys...that worked!
    Blake

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