1

As you can see from the code provided below as well as the title, that I'm having trouble trying to figure out how to move this dynamically embeded borderless form in VB.NET.

Private Sub AddWidget_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWidget.Click
        Dim WidgetForm As Form
        WidgetForm = New Form()
        WidgetForm.ShowInTaskbar = False
        WidgetForm.TopMost = True
        WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        WidgetForm.BackgroundImage = Image.FromFile(Me.OpenFileDialog1.FileName)
        WidgetForm.BackgroundImageLayout = ImageLayout.Stretch
        WidgetForm.Show()

        opac = Me.OpacInt.Text
        WidgetForm.Opacity = opac

        sizew = Me.WidgetW.Text
        sizey = Me.WidgetY.Text
        WidgetForm.Size = New System.Drawing.Size(sizew, sizey)
    End Sub

I've done this method (code below) a couple times in the past, and it always seems to work for moving a form, but I'm unsure on how to apply this for the dynamically embeded form.

Any help would be greatly appreciated.

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Left Then
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - Me.Left
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top
    End If

    Timer1.Enabled = True
    Timer1.Interval = 2500
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    If drag Then
        Me.Top = Windows.Forms.Cursor.Position.Y - mousey
        Me.Left = Windows.Forms.Cursor.Position.X - mousex
    End If
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    Timer1.Enabled = False
    drag = False
End Sub
Michael Schwartz
  • 8,153
  • 14
  • 81
  • 144

1 Answers1

1

The dynamic form would perform as same as the normal form. First of all you need to add some events for the dynamic form when you create it

Public WidgetForm As Form

Private Sub AddWidget_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWidget.Click

    WidgetForm = New Form()
    WidgetForm.ShowInTaskbar = False
    WidgetForm.TopMost = True
    WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    WidgetForm.BackgroundImage = Image.FromFile(Me.OpenFileDialog1.FileName)
    WidgetForm.BackgroundImageLayout = ImageLayout.Stretch
    WidgetForm.Show()

    opac = Me.OpacInt.Text
    WidgetForm.Opacity = opac

    sizew = Me.WidgetW.Text
    sizey = Me.WidgetY.Text
    WidgetForm.Size = New System.Drawing.Size(sizew, sizey)

    'Add the event here
    AddHandler WidgetForm.MouseDown, AddressOf WidgetForm_MouseDown
    AddHandler WidgetForm.MouseMove, AddressOf WidgetForm_MouseMove
    AddHandler WidgetForm.MouseUp, AddressOf WidgetForm_MouseUp
End Sub

Then you can move the form by the events handler

Private Sub WidgetForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 
    If e.Button = Windows.Forms.MouseButtons.Left Then
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - CType(sender, Form).Left
        mousey = Windows.Forms.Cursor.Position.Y - CType(sender, Form).Top
    End If

    Timer1.Enabled = True
    Timer1.Interval = 2500
End Sub

Private Sub WidgetForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If drag Then
        CType(sender, Form).Top = Windows.Forms.Cursor.Position.Y - mousey
        CType(sender, Form).Left = Windows.Forms.Cursor.Position.X - mousex
    End If
End Sub

Private Sub WidgetForm_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 
    Timer1.Enabled = False
    drag = False
End Sub
Nick
  • 1,128
  • 7
  • 12
  • Thanks that helped, but now I'm stuck on another problem. I want to make the borderless forms close by double clicking on them, but using Me.Close() closes the app, and WidgetForm.Close() doesn't work as it says it's inaccessible due to it's protection level. – Michael Schwartz May 07 '12 at 04:32
  • 1
    Then you may need to move 'Dim WidgetForm As Form' outside the function and declare it as a public form 'Public Widget Form'. I have modified my answer as you may see this. – Nick May 07 '12 at 04:55
  • That makes sense, but if say 3 forms are added by clicking the button, and I double click 2 borderless form's to close. Only one closes, and the other two are still visible, and won't close. Is there anyway to fix this? – Michael Schwartz May 07 '12 at 05:13
  • AddHandler WidgetForm.DoubleClick, AddressOf WidgetForm_DoubleClick Private Sub WidgetForm_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) WidgetForm.Close() End Sub – Michael Schwartz May 07 '12 at 05:40
  • 1
    change `WidgetForm.Close()` to `CType(sender, Form).Close()` in your double click event. – Nick May 07 '12 at 05:46
  • lmao I feel so dumb. Guess this is my cue to go to sleep. lol Thanks Nick – Michael Schwartz May 07 '12 at 05:51
  • 1
    when you execute `WidgetForm = New Form`, the Widget will always be replaced by the last created form, so if you use `WidgetForm.Close()` here can only close the last form but not the one you created earlier – Nick May 07 '12 at 05:51
  • Got another problem bro. Can you help me out? - http://stackoverflow.com/questions/10478629/vb-net-display-animated-gif-as-background-in-dynamic-form#comment13555192_10478629 – Michael Schwartz May 07 '12 at 20:13