CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2005
    Posts
    176

    drawing controls at run-time

    hey guys, i got 2 problems,

    1. i want the user to click on a form and the app. must draw a GroupBox at that location with a Label inside it.
    All that shows on screen is the GroupBox...... i am bringing the label to the front but its still not showing.
    The code I tried out is as follows:
    Code:
        Private l As New Label
        Private g As New GroupBox
    
        Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            Dim q As System.Drawing.Point
    
            q.X = e.X
            q.Y = e.Y
    
            g.Location = q
            g.Text = ""
            Me.Controls.Add(g)
    
            l.Parent = g
            l.Dock = DockStyle.Fill
            l.Text = Me.TextBox1.Text
            l.TextAlign = ContentAlignment.MiddleCenter
            l.BringToFront()
            l.Visible = True
    
            Me.Controls.Add(l)
        End Sub
    2. How can I draw a line from one control to another? I would like the user to click on 2 controls...... a line will be created from the edge of one to the other.

    Thanks in advance for your advice

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

    Re: drawing controls at run-time

    You need to add

    g.Controls.Add(l)

  3. #3
    Join Date
    Feb 2005
    Posts
    176

    Re: drawing controls at run-time

    ok thanks..... u were of great help.

    Re question 1........ the app is now drawing the 2 controls on the form. but when the user clicks on the form the second time...... the controls created first are removed and added again at the new location.
    what i want is....... that the first controls are left where they are....... and others are placed at the second or third click position.

    i think id have to fix the declaration of the controls!! Am i right?
    What should i do?

  4. #4
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: drawing controls at run-time

    Create g inside MouseDown() event.
    Good Luck,
    -Cool Bizs

  5. #5
    Join Date
    Feb 2005
    Posts
    176

    Re: drawing controls at run-time

    all the code is already under the mouse down event

  6. #6
    Join Date
    Feb 2005
    Posts
    176

    Re: drawing controls at run-time

    oh sorry..... i misunderstood...... i did as u said and declared the label under mouse down event too and its working great.
    here is the code:
    Code:
        Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            Dim l As New Label
            Dim g As New GroupBox
    
            Dim q As New System.Drawing.Point
    
            q.X = e.X
            q.Y = e.Y
    
            g.Location = q
            g.Text = ""
            Me.Controls.Add(g)
    
            l.Parent = g
            l.Dock = DockStyle.Fill
            l.Text = Me.TextBox1.Text
            l.TextAlign = ContentAlignment.MiddleCenter
        End Sub

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