Page 1 of 2 12 LastLast
Results 1 to 40 of 44

Thread: Movable Dynamic Controls

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    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?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Movable Dynamic Controls

    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.      ' Creates a button (any control can be created like that)
    3.      ' and sets its properties
    4.      Dim btn As New Button With { _
    5.             .Width = 200, _
    6.             .Height = 20, _
    7.             .Left = 10, _
    8.             .Top = 10, _
    9.             .Text = "Click me"}
    10.  
    11.      ' Add the button to the form's control collection
    12.      Me.Controls.Add(btn)
    13.  
    14.      ' Wire the Click event
    15.      AddHandler btn.Click, AddressOf btn_click
    16.  
    17. End Sub
    18.  
    19. Private Sub btn_click(ByVal sender As Object, ByVal e As System.EventArgs)
    20.     MsgBox ("Button clicked")
    21. End Sub

    Too late, I guess

  7. #7

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    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

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    have a look at the move / resize controls link in my signature

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    Re: Movable Dynamic Controls

    Got it And how can I refer to a control that doesn't exist?

  11. #11

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    Re: Movable Dynamic Controls

    Quote Originally Posted by NickThissen View Post
    You can't refer to a control that doesn't exist. How does that even make sense?
    Well, to a dynamic control.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    Re: Movable Dynamic Controls

    Anyone? I want to refer the drag'n'drop properties to the dynamic control. How can I do it?

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    can you show us your code?

  15. #15
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

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

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    Re: Movable Dynamic Controls

    Quote Originally Posted by cicatrix View Post
    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.      ' Creates a button (any control can be created like that)
    3.      ' and sets its properties
    4.      Dim btn As New Button With { _
    5.             .Width = 200, _
    6.             .Height = 20, _
    7.             .Left = 10, _
    8.             .Top = 10, _
    9.             .Text = "Click me"}
    10.  
    11.      ' Add the button to the form's control collection
    12.      Me.Controls.Add(btn)
    13.  
    14.      ' Wire the Click event
    15.      AddHandler btn.Click, AddressOf btn_click
    16.  
    17. End Sub
    18.  
    19. Private Sub btn_click(ByVal sender As Object, ByVal e As System.EventArgs)
    20.     MsgBox ("Button clicked")
    21. 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?

  17. #17
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

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

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    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.

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    put that code in a button_click event

  20. #20
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    Quote Originally Posted by keystone_paul View Post
    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

  22. #22
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    try this:

    vb Code:
    1. Public Class Form5
    2.  
    3.     Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         'creates 2 rows of 5 buttons
    5.         For x As Integer = 1 To 10
    6.             AddNewButton(x)
    7.         Next
    8.     End Sub
    9.  
    10.     Private Sub AddNewButton(ByVal index As Integer)
    11.  
    12.         ' Creates a button (any control can be created like that)
    13.         ' and sets its properties
    14.         Dim btn As New Button With { _
    15.                .Width = 75, _
    16.                .Height = 20, _
    17.                .Left = CInt(If(index <= 5, 10 + ((index - 1) * 75), 10 + ((index - 6) * 75))), _
    18.                .Top = CInt(If(index <= 5, 10, 30)), _
    19.                .Tag = index, _
    20.                .Text = "Click me"}
    21.  
    22.         ' Add the button to the form's control collection
    23.         Me.Controls.Add(btn)
    24.  
    25.         ' Wire the Click event
    26.         AddHandler btn.Click, AddressOf btn_click
    27.  
    28.     End Sub
    29.  
    30.     Private Sub btn_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    31.         MsgBox("you clicked button" & DirectCast(sender, Button).Tag.ToString)
    32.     End Sub
    33.  
    34. End Class

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    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?

  25. #25
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    we need to see your code to see how far you've got

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    Re: Movable Dynamic Controls

    I've this:

    vb Code:
    1. Public Class PreviewFrm
    2.  
    3.     Dim worker As MoveResizeControl.controlHandler
    4.  
    5.     'Declarar controlo
    6.     Dim btn As New Button With { _
    7.        .Width = 200, _
    8.        .Height = 200, _
    9.        .Left = 10, _
    10.        .Top = 10, _
    11.        .Text = "Nova caixa de informação", _
    12.        .BackColor = Color.Transparent, _
    13.        .FlatStyle = FlatStyle.Popup}
    14.  
    15.     Private Sub PreviewFrm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    16.         worker.deselectAll()
    17.         Invalidate()
    18.     End Sub
    19.  
    20.  
    21.     Private Sub PreviewFrm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    22.         If worker.boundingRectangle <> Nothing Then
    23.             Dim p As New Pen(Color.Black, 1)
    24.             p.DashStyle = Drawing2D.DashStyle.Dash
    25.             e.Graphics.DrawRectangle(p, worker.boundingRectangle)
    26.         End If
    27.     End Sub
    28.  
    29.     Private Sub PreviewFrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    30.         ' Inicializar DLL do Resize e movimento de controlos dinâmicos
    31.         worker = New MoveResizeControl.controlHandler
    32.         worker.boundControls.Add(New MoveResizeControl.controlHandler.boundControl(Me, btn))
    33.         worker.bindControls()
    34.  
    35.  
    36.         Me.Width = My.Computer.Screen.Bounds.Width
    37.         Me.Height = My.Computer.Screen.Bounds.Height
    38.  
    39.     End Sub
    40.  
    41.     Private Sub btn_config_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_config.Click
    42.         ConfigDesignForm.Show()
    43.     End Sub
    44.  
    45.     Private Sub btn_additems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_additems.Click
    46.         Me.Controls.Add(btn)
    47.         btn.BringToFront()
    48.     End Sub
    49. 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?

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    try this:

    vb Code:
    1. Public Class PreviewFrm
    2.  
    3.     Dim worker As MoveResizeControl.controlHandler = Nothing
    4.  
    5.     Private Sub PreviewFrm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    6.         If worker IsNot Nothing Then
    7.             worker.deselectAll()
    8.             Invalidate()
    9.         End If
    10.     End Sub
    11.  
    12.  
    13.     Private Sub PreviewFrm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    14.         If worker IsNot Nothing Then
    15.             If worker.boundingRectangle <> Nothing Then
    16.                 Dim p As New Pen(Color.Black, 1)
    17.                 p.DashStyle = Drawing2D.DashStyle.Dash
    18.                 e.Graphics.DrawRectangle(p, worker.boundingRectangle)
    19.             End If
    20.         End If
    21.     End Sub
    22.  
    23.     Private Sub PreviewFrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    24.  
    25.         Me.Width = My.Computer.Screen.Bounds.Width
    26.         Me.Height = My.Computer.Screen.Bounds.Height
    27.  
    28.     End Sub
    29.  
    30.     Private Sub btn_config_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_config.Click
    31.         ConfigDesignForm.Show()
    32.     End Sub
    33.  
    34.     Private Sub btn_additems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_additems.Click
    35.         'Declarar controlo
    36.         Dim btn As New Button With { _
    37.            .Width = 200, _
    38.            .Height = 200, _
    39.            .Left = 10, _
    40.            .Top = 10, _
    41.            .Text = "Nova caixa de informação", _
    42.            .BackColor = Color.Transparent, _
    43.            .FlatStyle = FlatStyle.Popup}
    44.  
    45.         ' Inicializar DLL do Resize e movimento de controlos dinâmicos
    46.         worker = New MoveResizeControl.controlHandler
    47.         worker.boundControls.Add(New MoveResizeControl.controlHandler.boundControl(Me, btn))
    48.         worker.bindControls()
    49.  
    50.         Me.Controls.Add(btn)
    51.         btn.BringToFront()
    52.     End Sub
    53. End Class

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    Re: Movable Dynamic Controls

    Still crashing thanks anyway..

  30. #30
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    try this:

    vb Code:
    1. Private Sub btn_additems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_additems.Click
    2.     'Declarar controlo
    3.     Dim btn As New Button With { _
    4.        .Width = 200, _
    5.        .Height = 200, _
    6.        .Left = 10, _
    7.        .Top = 10, _
    8.        .Text = "Nova caixa de informação", _
    9.        .BackColor = Color.Transparent, _
    10.        .FlatStyle = FlatStyle.Popup}
    11.  
    12.  
    13.     Me.Controls.Add(btn)
    14.     btn.BringToFront()
    15.  
    16.     ' Inicializar DLL do Resize e movimento de controlos dinâmicos
    17.     If worker Is Nothing Then
    18.         worker = New MoveResizeControl.controlHandler
    19.     End If
    20.     worker.boundControls.Add(New MoveResizeControl.controlHandler.boundControl(Me, btn))
    21.     worker.bindControls()
    22.  
    23. End Sub

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

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

  32. #32
    Member
    Join Date
    Feb 2010
    Posts
    36

    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???

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    Re: Movable Dynamic Controls

    Quote Originally Posted by tearsculprit View Post
    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..

  34. #34
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: Movable Dynamic Controls

    Quote Originally Posted by SirPereira View Post
    Sorry, but if you don't mind create a new topic with your doubt..

    Opps, Sorry for that

  35. #35
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    SirPereira, if you want to zip + post your project, i'll try to get it up + running for you

  36. #36
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    Quote Originally Posted by tearsculprit View Post
    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?

  37. #37

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    71

    Re: Movable Dynamic Controls

    I hope you understand something of the code lol.

    http://rapidshare.com/files/36432551...tudio.zip.html

  38. #38
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: Movable Dynamic Controls

    Quote Originally Posted by .paul. View Post
    can you post a screenshot?
    Is it Ok if I post it here???

  39. #39
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Movable Dynamic Controls

    yeah post it in this thread. i'm sure sirpereira won't mind...

  40. #40
    Member
    Join Date
    Feb 2010
    Posts
    36

    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.

Page 1 of 2 12 LastLast

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