Click to See Complete Forum and Search --> : drawing controls at run-time


Nasty2
May 31st, 2005, 01:23 PM
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:
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

DSJ
May 31st, 2005, 01:51 PM
You need to add

g.Controls.Add(l)

Nasty2
June 3rd, 2005, 02:36 PM
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?

coolbiz
June 3rd, 2005, 04:02 PM
Create g inside MouseDown() event.

Nasty2
June 3rd, 2005, 06:29 PM
all the code is already under the mouse down event

Nasty2
June 3rd, 2005, 06:35 PM
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:
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