6
Answers

How to create an unmovable Button that can generate movable buttons.

Hello, Don't get confused because of the title. Yes, its true that my subject is a little bit confusing, but I really need help for this project. I'm trying to make a fixed or unmovable button that can generate movable buttons. 

I successfully made a button that can generate buttons. But unfortunately when I tried creating a function that can move buttons, It only move buttons that are already declared. 

Need your help badly. Thanks.

Answers (6)

4
Photo of Frogleg
NA 7.9k 33k 14y


try this
Dim buttonNumber As Integer = 0
Private Sub CreateDynamicButton()
' Create a Button object

Dim dynamicButton(buttonNumber) As Button
dynamicButton(buttonNumber) =
New Button
Dim b As String
b = buttonNumber
' Set Button properties
dynamicButton(buttonNumber).Location =
New Point(20, 150)
dynamicButton(buttonNumber).Height = 40
dynamicButton(buttonNumber).Width = 300
' Set background and foreground
dynamicButton(buttonNumber).BackColor = Color.Red
dynamicButton(buttonNumber).ForeColor = Color.Blue
dynamicButton(buttonNumber).Text = b
dynamicButton(buttonNumber).Name = buttonNumber
dynamicButton(buttonNumber).Font =
New Font("Georgia", 16)
AddHandler dynamicButton(buttonNumber).Click, AddressOf DynamicButton_Click
AddHandler dynamicButton(buttonNumber).MouseMove, AddressOf Button_MouseMove
' Add Button to the Form. Placement of the Button
' will be based on the Location and Size of button
Controls.Add(dynamicButton(buttonNumber))
buttonNumber += 1
End Sub
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show(
"Set Table " & sender.Text)
End Sub
0
Photo of Rester Delarmente
NA 7 4.8k 14y
That was good Mr.Milo, but to be exact, I have to put it inside tables or cells that it will be automatically positioned. for example like in flash games that you can drag and drop an object to a specific location and cell to have an exact position. though I have to make it in Visual studio 2005. thank you for your help and support. :)
0
Photo of Frogleg
NA 7.9k 33k 14y

Try this
Private oControlOffsetPoint As Point
Dim buttonNumber As Integer = 0
Dim isMoveable As New List(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CreateDynamicButton()
End Sub
Private Sub CreateDynamicButton()
' Create a Button object

Dim dynamicButton(buttonNumber) As Button
dynamicButton(buttonNumber) =
New Button
Dim b As String
b = buttonNumber
' Set Button properties
dynamicButton(buttonNumber).Location =
New Point(20, 150)
dynamicButton(buttonNumber).Height = 40
dynamicButton(buttonNumber).Width = 300
' Set background and foreground
dynamicButton(buttonNumber).BackColor = Color.Red
dynamicButton(buttonNumber).ForeColor = Color.Blue
dynamicButton(buttonNumber).Text = b
dynamicButton(buttonNumber).Name = buttonNumber
dynamicButton(buttonNumber).Font =
New Font("Georgia", 16)
AddHandler dynamicButton(buttonNumber).Click, AddressOf DynamicButton_Click
AddHandler dynamicButton(buttonNumber).MouseUp, AddressOf DynamicButton_MouseUp
AddHandler dynamicButton(buttonNumber).MouseMove, AddressOf Button_MouseMove
' Add Button to the Form. Placement of the Button
' will be based on the Location and Size of button
Controls.Add(dynamicButton(buttonNumber))
buttonNumber += 1
End Sub
Private Sub Button_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim sX As Integer = RectangleToClient(New Rectangle(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y, 0, 0)).Left - oControlOffsetPoint.X
Dim sY As Integer = RectangleToClient(New Rectangle(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y, 0, 0)).Top - oControlOffsetPoint.Y
If isMoveable.Contains(sender.Text) = False Then
If e.Button = Windows.Forms.MouseButtons.Left Then
sender.Location =
New Point(sX, sY)
sender.Left = sX
sender.Top = sY
End If
End If
End Sub

Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show(
"Set Table " & sender.Text)
End Sub
Private Sub DynamicButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Right Then
If isMoveable.Contains(sender.Text) Then
isMoveable.Remove(sender.Text)
Else
isMoveable.Add(sender.Text)
MessageBox.Show(
"Locked " & sender.Text)
End If
End If
End Sub
0
Photo of Rester Delarmente
NA 7 4.8k 14y
Thanks Milo,

but I have another problem, what will I do next to put those buttons in a table form. So that I can drag and drop them and locked and unlocked. They should have fixed places or cells and I can disable and enable the mousemove option. How can it be done?


rester.
0
Photo of Rester Delarmente
NA 7 4.8k 14y
Here you go, so far that was I've done.
It has an unmovable button and it can generates movable buttons. But the problem now, all generated buttons have the same name.




Public Class Form1
    Private oControlOffsetPoint As Point
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CreateDynamicButton()
    End Sub
    Public Function ChrW(ByVal buttonNumber As Integer) As Char
    End Function

    Private Sub CreateDynamicButton()
        ' Create a Button object 

        Dim dynamicButton(0) As Button
        dynamicButton(0) = New Button
        Dim buttonNumber As Integer = 0
        Dim b As String
        b = buttonNumber
        ' Set Button properties
        dynamicButton(buttonNumber).Location = New Point(20, 150)
        dynamicButton(buttonNumber).Height = 40
        dynamicButton(buttonNumber).Width = 300
        ' Set background and foreground
        dynamicButton(buttonNumber).BackColor = Color.Red
        dynamicButton(buttonNumber).ForeColor = Color.Blue
        dynamicButton(buttonNumber).Text = b
        dynamicButton(buttonNumber).Name = buttonNumber
        dynamicButton(buttonNumber).Font = New Font("Georgia", 16)
        AddHandler dynamicButton(buttonNumber).Click, AddressOf DynamicButton_Click
        AddHandler dynamicButton(buttonNumber).MouseMove, AddressOf Button_MouseMove
        ' Add Button to the Form. Placement of the Button
        ' will be based on the Location and Size of button
        Controls.Add(dynamicButton(buttonNumber))
        buttonNumber = (buttonNumber + 1)
    End Sub
    Private Sub Button_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim sX As Integer = RectangleToClient(New Rectangle(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y, 0, 0)).Left - oControlOffsetPoint.X
        Dim sY As Integer = RectangleToClient(New Rectangle(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y, 0, 0)).Top - oControlOffsetPoint.Y

        If e.Button = Windows.Forms.MouseButtons.Left Then
            sender.Location = New Point(sX, sY)
            sender.Left = sX
            sender.Top = sY
        End If
    End Sub

    Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MessageBox.Show("Set Table")
    End Sub

End Class

0
Photo of Frogleg
NA 7.9k 33k 14y
Can you upload your project