Button Position verändern

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von xXIXx.

    Button Position verändern

    Hallo Forum

    Ich habe eine kleine Frage : Wie kann ich die Position eines Buttons in der Form zufällig verändern ?

    Ich habs schon mit

    Button1.location.x = 12 zB. probiert doch das funktioniert irgendwie nicht.

    Ich bin halt noch Anfänger. ^^
    Hier eine kleine vollständige C&P-Demo :)

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private x As Integer = 0
    3. Private y As Integer = 0
    4. Private rdm As New Random
    5. Private btnRun As New Button
    6. Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7. Me.Controls.Add(btnRun)
    8. With btnRun
    9. .Height = 40
    10. .Width = 150
    11. .Text = "Catch me if you can !!!"
    12. AddHandler .LocationChanged, AddressOf Me.btn_LocationChanged
    13. AddHandler .MouseEnter, AddressOf Me.btn_MouseEnter
    14. AddHandler .Click, AddressOf Me.btn_Click
    15. End With
    16. End Sub
    17. Private Sub btn_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    18. x = rdm.Next(0, Me.Width - DirectCast(sender, Button).Width)
    19. rdm.Next()
    20. y = rdm.Next(0, Me.Height - DirectCast(sender, Button).Height)
    21. End Sub
    22. Private Sub btn_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
    23. DirectCast(sender, Button).Location = New Point(x, y)
    24. End Sub
    25. Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    26. MessageBox.Show("Erwischt !!!", DirectCast(sender, Button).Name, MessageBoxButtons.OK, MessageBoxIcon.Information)
    27. End Sub
    28. End Class