|
-
Mar 10th, 2010, 04:27 PM
#1
Thread Starter
Lively Member
Movable Dynamic Controls
Hey guys,
here is the deal, imagine that i've a fullscreen form, and when clicking on a button, it will create a new control, a control that could be selected and moved.
I know that they are dynamic controls, but anyone know how to make them with the specific things i've said? A kind of dynamic controls allowing drag & drop to another position...
Regards
-
Mar 10th, 2010, 04:47 PM
#2
Re: Movable Dynamic Controls
1) Create an instance of the control
2) Add instance to the controls collection of the form.
3) Add handlers using....AddHandler to handle dragging and dropping.
4) Set properties.
That third one can get tricky, depending on where you want to be able to drop it. You stated that you just want it on the form, so one thing you could do is to add handlers for the MouseDown and MouseUp events. On MouseDown, record some information such as the control and the mouse position. On MouseMove, alter the Top and Left properties so that the control follows the mouse around (move the control by the same amount as the mouse has moved). There would be a variety of effects that could be created depending on how you tracked the mouse movement, but I was thinking along the lines of moving the control as long as the mouse button is down, and releasing the control on MouseUp.
My usual boring signature: Nothing
 
-
Mar 10th, 2010, 04:58 PM
#3
Thread Starter
Lively Member
Re: Movable Dynamic Controls
It will be over a Windows Media Player Control.
Should I make a class to it?
Imagine that I want every time I press a button, it creates a PictureBox.
What then?
-
Mar 10th, 2010, 05:06 PM
#4
Re: Movable Dynamic Controls
I haven't worked with Windows Media Player and don't even have a good idea as to what it is. Therefore, anything I say may be utterly irrelevant.
Oddly, just in the last week I had cause to write a function to create pictureboxes dynamically. The handlers are relatively few, and the properties are equally meager, but it suited my needs. Here's what the function looked like:
Code:
Private Function CreatePB(ByVal top As Integer, ByVal left As Integer, ByVal height As Integer, ByVal width As Integer, ByVal name As String) As PictureBox
Dim pb = New PictureBox
pb.Name = name
pb.Top = top
pb.Left = left
pb.Width = width
pb.Height = height
pb.AllowDrop = True
pb.BackColor = Color.LightGray
pb.SizeMode = PictureBoxSizeMode.StretchImage
AddHandler pb.MouseDown, AddressOf pbMouseDown
AddHandler pb.DragDrop, AddressOf pbDragDrop
AddHandler pb.DragEnter, AddressOf pbDragEnter
Return pb
End Function
All the pictureboxes created like this used the same event handlers for drag and drop functionality.
One thing to keep in mind is that you would want to cap the maximum number of picture box controls, or you will have them all over the place, which will become a nightmare.
My usual boring signature: Nothing
 
-
Mar 10th, 2010, 05:10 PM
#5
Re: Movable Dynamic Controls
There's (at least) two threads in the codebank that show you how to move and resize controls during run-time.
With my version, all you need to do is add the posted class to your project, and create new instances of it while passing the control you want to become movable + resizable:
Code:
Dim moveButton As New MoveControl(Button1)
Then you can set properties such as which directions it is resizable from (possibly none).
There's another thread about the same thing but I haven't really tried it. Not sure how that works.
-
Mar 10th, 2010, 05:10 PM
#6
Re: Movable Dynamic Controls
vb.net Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Creates a button (any control can be created like that) ' and sets its properties Dim btn As New Button With { _ .Width = 200, _ .Height = 20, _ .Left = 10, _ .Top = 10, _ .Text = "Click me"} ' Add the button to the form's control collection Me.Controls.Add(btn) ' Wire the Click event AddHandler btn.Click, AddressOf btn_click End Sub Private Sub btn_click(ByVal sender As Object, ByVal e As System.EventArgs) MsgBox ("Button clicked") End Sub
Too late, I guess
-
Mar 10th, 2010, 05:23 PM
#7
Re: Movable Dynamic Controls
 Originally Posted by cicatrix
 Too late, I guess
That, and how is your button movable?
-
Mar 10th, 2010, 05:56 PM
#8
Thread Starter
Lively Member
Re: Movable Dynamic Controls
Sorry guys, but I've tried to find the code that NickThissen was talking about, but I couldn't find it.
Can you give me a hand?
Thanks
-
Mar 10th, 2010, 09:11 PM
#9
Re: Movable Dynamic Controls
have a look at the move / resize controls link in my signature
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 11th, 2010, 02:13 AM
#10
Thread Starter
Lively Member
Re: Movable Dynamic Controls
Got it And how can I refer to a control that doesn't exist?
-
Mar 11th, 2010, 04:16 AM
#11
Re: Movable Dynamic Controls
My thread was also in my signature:
http://www.vbforums.com/showthread.php?p=3509289
You can't refer to a control that doesn't exist. How does that even make sense?
-
Mar 11th, 2010, 01:45 PM
#12
Thread Starter
Lively Member
Re: Movable Dynamic Controls
 Originally Posted by NickThissen
You can't refer to a control that doesn't exist. How does that even make sense?
Well, to a dynamic control.
-
Mar 13th, 2010, 01:02 PM
#13
Thread Starter
Lively Member
Re: Movable Dynamic Controls
Anyone? I want to refer the drag'n'drop properties to the dynamic control. How can I do it?
-
Mar 13th, 2010, 01:17 PM
#14
Re: Movable Dynamic Controls
can you show us your code?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 13th, 2010, 01:18 PM
#15
Re: Movable Dynamic Controls
You can access any dynamic controls after you've created them by either referring to the form's controls collection, or by creating your own collection for your dynamic picture boxes and adding each one to that collection as you create it (as well as to the form's controls collection).
-
Mar 14th, 2010, 03:25 PM
#16
Thread Starter
Lively Member
Re: Movable Dynamic Controls
 Originally Posted by cicatrix
vb.net Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Creates a button (any control can be created like that)
' and sets its properties
Dim btn As New Button With { _
.Width = 200, _
.Height = 20, _
.Left = 10, _
.Top = 10, _
.Text = "Click me"}
' Add the button to the form's control collection
Me.Controls.Add(btn)
' Wire the Click event
AddHandler btn.Click, AddressOf btn_click
End Sub
Private Sub btn_click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox ("Button clicked")
End Sub
 Too late, I guess
Well, Imagine that I want it to create everytime I press the button it will create a new instance of dynamic control (and therefore, sizeable and moveable), how could I do that? Creating individual instances?
-
Mar 14th, 2010, 03:48 PM
#17
Re: Movable Dynamic Controls
Well, Imagine that I want it to create everytime I press the button it will create a new instance of dynamic control (and therefore, sizeable and moveable), how could I do that? Creating individual instances?
How about
You can access any dynamic controls after you've created them by either referring to the form's controls collection, or by creating your own collection for your dynamic picture boxes and adding each one to that collection as you create it (as well as to the form's controls collection).
-
Mar 14th, 2010, 04:23 PM
#18
Thread Starter
Lively Member
Re: Movable Dynamic Controls
Well, that wasn't the answer for my question.
I asked how to create different instances of dynamic controls (forget the sizeable and moveable for now).
With that code referred in the last post, I can only create one instance of dynamic control.
-
Mar 14th, 2010, 04:25 PM
#19
Re: Movable Dynamic Controls
put that code in a button_click event
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2010, 04:28 PM
#20
Re: Movable Dynamic Controls
Well, that wasn't the answer for my question.
I asked how to create different instances of dynamic controls (forget the sizeable and moveable for now).
With that code referred in the last post, I can only create one instance of dynamic control.
If you have a new question you should really post it in a new thread, however every time you run cicatrix's code it will create a new control.
If you took it out into a separate sub then you can call it 10 times and get 10 new controls :
Code:
Private Sub Create10NewButtons
For X as integer = 1 to 10
AddNewButton
Next N
End Sub
Private Sub AddNewButton
' Creates a button (any control can be created like that)
' and sets its properties
Dim btn As New Button With { _
.Width = 200, _
.Height = 20, _
.Left = 10, _
.Top = 10, _
.Text = "Click me"}
' Add the button to the form's control collection
Me.Controls.Add(btn)
' Wire the Click event
AddHandler btn.Click, AddressOf btn_click
End Sub
Private Sub btn_click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox ("Button clicked")
End Sub
-
Mar 14th, 2010, 04:30 PM
#21
Re: Movable Dynamic Controls
 Originally Posted by keystone_paul
If you have a new question you should really post it in a new thread, however every time you run cicatrix's code it will create a new control.
If you took it out into a separate sub then you can call it 10 times and get 10 new controls :
Code:
Private Sub Create10NewButtons
For X as integer = 1 to 10
AddNewButton
Next N
End Sub
Private Sub AddNewButton
' Creates a button (any control can be created like that)
' and sets its properties
Dim btn As New Button With { _
.Width = 200, _
.Height = 20, _
.Left = 10, _
.Top = 10, _
.Text = "Click me"}
' Add the button to the form's control collection
Me.Controls.Add(btn)
' Wire the Click event
AddHandler btn.Click, AddressOf btn_click
End Sub
you'll only see 1 button though because they're all stacked up on top of each other
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2010, 04:33 PM
#22
Re: Movable Dynamic Controls
you'll only see 1 button though because they're all stacked up on top of each other
Indeed, although it will still create the 10 buttons.
-
Mar 14th, 2010, 04:51 PM
#23
Re: Movable Dynamic Controls
try this:
vb Code:
Public Class Form5
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'creates 2 rows of 5 buttons
For x As Integer = 1 To 10
AddNewButton(x)
Next
End Sub
Private Sub AddNewButton(ByVal index As Integer)
' Creates a button (any control can be created like that)
' and sets its properties
Dim btn As New Button With { _
.Width = 75, _
.Height = 20, _
.Left = CInt(If(index <= 5, 10 + ((index - 1) * 75), 10 + ((index - 6) * 75))), _
.Top = CInt(If(index <= 5, 10, 30)), _
.Tag = index, _
.Text = "Click me"}
' Add the button to the form's control collection
Me.Controls.Add(btn)
' Wire the Click event
AddHandler btn.Click, AddressOf btn_click
End Sub
Private Sub btn_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("you clicked button" & DirectCast(sender, Button).Tag.ToString)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2010, 05:22 PM
#24
Thread Starter
Lively Member
Re: Movable Dynamic Controls
Thanks for all the answers, and sorry about bein' noob in the community.
Well, now I've a "problem".
Maybe a logical problem, I will only have the dynamic control available when I click the button to create it, right? 'Cause in the running of the software I don't know if I want to create the control otherwise I click the button.
So, how can I add the handler for the DLL provided (http://www.vbforums.com/showthread.p...85#post3714785) for a control that haven't been added to the controls list?
-
Mar 14th, 2010, 05:27 PM
#25
Re: Movable Dynamic Controls
I'm not sure where you are going... if you mean how do you refer to a dynamic control after you've created it then I'll refer you again to my post above... either add it to a custom collection as you create it, or interrogate the form's Controls collection.
-
Mar 14th, 2010, 05:28 PM
#26
Re: Movable Dynamic Controls
we need to see your code to see how far you've got
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2010, 05:38 PM
#27
Thread Starter
Lively Member
Re: Movable Dynamic Controls
I've this:
vb Code:
Public Class PreviewFrm Dim worker As MoveResizeControl.controlHandler 'Declarar controlo Dim btn As New Button With { _ .Width = 200, _ .Height = 200, _ .Left = 10, _ .Top = 10, _ .Text = "Nova caixa de informação", _ .BackColor = Color.Transparent, _ .FlatStyle = FlatStyle.Popup} Private Sub PreviewFrm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown worker.deselectAll() Invalidate() End Sub Private Sub PreviewFrm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint If worker.boundingRectangle <> Nothing Then Dim p As New Pen(Color.Black, 1) p.DashStyle = Drawing2D.DashStyle.Dash e.Graphics.DrawRectangle(p, worker.boundingRectangle) End If End Sub Private Sub PreviewFrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Inicializar DLL do Resize e movimento de controlos dinâmicos worker = New MoveResizeControl.controlHandler worker.boundControls.Add(New MoveResizeControl.controlHandler.boundControl(Me, btn)) worker.bindControls() Me.Width = My.Computer.Screen.Bounds.Width Me.Height = My.Computer.Screen.Bounds.Height End Sub Private Sub btn_config_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_config.Click ConfigDesignForm.Show() End Sub Private Sub btn_additems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_additems.Click Me.Controls.Add(btn) btn.BringToFront() End Sub End Class
Well, now I've changed somethings. I declared the object first.
Now I've another problem. While "moving" or "resizing", it 'crashes'. Imagine that I'm moving it, it moves something like 10pixels, stay with the hand cursor, but it 'crashes' there. Don't happen anything more. It freezes. Suggestions?
-
Mar 14th, 2010, 05:44 PM
#28
Re: Movable Dynamic Controls
try this:
vb Code:
Public Class PreviewFrm
Dim worker As MoveResizeControl.controlHandler = Nothing
Private Sub PreviewFrm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If worker IsNot Nothing Then
worker.deselectAll()
Invalidate()
End If
End Sub
Private Sub PreviewFrm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If worker IsNot Nothing Then
If worker.boundingRectangle <> Nothing Then
Dim p As New Pen(Color.Black, 1)
p.DashStyle = Drawing2D.DashStyle.Dash
e.Graphics.DrawRectangle(p, worker.boundingRectangle)
End If
End If
End Sub
Private Sub PreviewFrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Width = My.Computer.Screen.Bounds.Width
Me.Height = My.Computer.Screen.Bounds.Height
End Sub
Private Sub btn_config_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_config.Click
ConfigDesignForm.Show()
End Sub
Private Sub btn_additems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_additems.Click
'Declarar controlo
Dim btn As New Button With { _
.Width = 200, _
.Height = 200, _
.Left = 10, _
.Top = 10, _
.Text = "Nova caixa de informação", _
.BackColor = Color.Transparent, _
.FlatStyle = FlatStyle.Popup}
' Inicializar DLL do Resize e movimento de controlos dinâmicos
worker = New MoveResizeControl.controlHandler
worker.boundControls.Add(New MoveResizeControl.controlHandler.boundControl(Me, btn))
worker.bindControls()
Me.Controls.Add(btn)
btn.BringToFront()
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2010, 05:53 PM
#29
Thread Starter
Lively Member
Re: Movable Dynamic Controls
Still crashing thanks anyway..
-
Mar 14th, 2010, 06:29 PM
#30
Re: Movable Dynamic Controls
try this:
vb Code:
Private Sub btn_additems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_additems.Click
'Declarar controlo
Dim btn As New Button With { _
.Width = 200, _
.Height = 200, _
.Left = 10, _
.Top = 10, _
.Text = "Nova caixa de informação", _
.BackColor = Color.Transparent, _
.FlatStyle = FlatStyle.Popup}
Me.Controls.Add(btn)
btn.BringToFront()
' Inicializar DLL do Resize e movimento de controlos dinâmicos
If worker Is Nothing Then
worker = New MoveResizeControl.controlHandler
End If
worker.boundControls.Add(New MoveResizeControl.controlHandler.boundControl(Me, btn))
worker.bindControls()
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2010, 06:37 PM
#31
Thread Starter
Lively Member
Re: Movable Dynamic Controls
Still nothing. Grrr, I've been trying to explore the code, but can't see where is it failing, or what's the error..
-
Mar 15th, 2010, 02:34 AM
#32
Member
Re: Movable Dynamic Controls
Hi Paul,
Referring to your Move and Resize Control, when I add a panel to it(all the control move into the panel), the border that show for resizing and moving the control tends to move out or does not fit exactly to the current position of the control selected.
What can I do, so that the border's position fit exactly to the selected control???
-
Mar 15th, 2010, 02:54 PM
#33
Thread Starter
Lively Member
Re: Movable Dynamic Controls
 Originally Posted by tearsculprit
Hi Paul,
Referring to your Move and Resize Control, when I add a panel to it(all the control move into the panel), the border that show for resizing and moving the control tends to move out or does not fit exactly to the current position of the control selected.
What can I do, so that the border's position fit exactly to the selected control???
Sorry, but if you don't mind create a new topic with your doubt..
-
Mar 15th, 2010, 08:58 PM
#34
Member
Re: Movable Dynamic Controls
 Originally Posted by SirPereira
Sorry, but if you don't mind create a new topic with your doubt.. 
Opps, Sorry for that
-
Mar 16th, 2010, 01:24 PM
#35
Re: Movable Dynamic Controls
SirPereira, if you want to zip + post your project, i'll try to get it up + running for you
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 16th, 2010, 01:26 PM
#36
Re: Movable Dynamic Controls
 Originally Posted by tearsculprit
Hi Paul,
Referring to your Move and Resize Control, when I add a panel to it(all the control move into the panel), the border that show for resizing and moving the control tends to move out or does not fit exactly to the current position of the control selected.
What can I do, so that the border's position fit exactly to the selected control???
can you post a screenshot?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 16th, 2010, 06:30 PM
#37
Thread Starter
Lively Member
Re: Movable Dynamic Controls
-
Mar 16th, 2010, 08:22 PM
#38
Member
Re: Movable Dynamic Controls
 Originally Posted by .paul.
can you post a screenshot?
Is it Ok if I post it here???
-
Mar 16th, 2010, 08:38 PM
#39
Re: Movable Dynamic Controls
yeah post it in this thread. i'm sure sirpereira won't mind...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 16th, 2010, 08:39 PM
#40
Member
Re: Movable Dynamic Controls
Add a Panel and all the control move inside the panel.

When running the application, the border does not fit to the control inside the panel.

I did make some changes to the value of Location = New Point() to make it fit well to the control...
But do not know if i did the right things...
Last edited by tearsculprit; Mar 16th, 2010 at 08:46 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|