How to create a button dinamically in VB

How to create a button by code ? take a look here, this is very simple...


create a form , add a button then double click on it :
or you just call it from main form routine; (as you wish...)
...were call the sub CreateButton()
(add a image to resource if you want to have button with image _
but be careful whit image size is important)


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CreateButton()
    End Sub


then create a sub routine:

 Sub CreateButton()
        ' Create a Button object

        ' Set Button properties ,location and size
        dynamicButton.Location = New Point(10, 100)
        dynamicButton.Height = 30
        dynamicButton.Width = 100

        ' Set Button Type
        dynamicButton.FlatStyle = FlatStyle.Flat
        dynamicButton.FlatAppearance.BorderColor = Color.Lime
        dynamicButton.FlatAppearance.BorderSize = 0

        ' Set background and foreground
        dynamicButton.ForeColor = Color.DodgerBlue
        dynamicButton.BackColor = Color.Transparent

      
         ' Set name and Text which is displayed for user
        dynamicButton.Text = "Button"
        dynamicButton.Name = "newBtn1"
        dynamicButton.Font = New Font("Calibri", 10)



        ' Add a imagine
        ' this method is whithout text :
        'dynamicButton.BackgroundImageLayout = ImageLayout.Zoom
        'dynamicButton.BackgroundImage = My.Resources.ok_checked

        ' THE NEXT METHOD is whit text but....
        ' here is very important a size of image, resize you image_
        '_before add it whit a imagine editor ,the size sould be a same whit a button size_
        '_therefore will fit in.
        dynamicButton.ImageAlign = ContentAlignment.MiddleLeft
        dynamicButton.Image = My.Resources..ok_checked


        ' Add control command ;
        ' this is very important  ,without this command you cannot access the button
        AddHandler dynamicButton.Click, AddressOf DynamicButton_Click

        ' Add more control command like mouse enter and mouse leave
        AddHandler dynamicButton.MouseEnter, AddressOf DynamicButton_MouseEnter
        AddHandler dynamicButton.MouseLeave, AddressOf DynamicButton_MouseLeave

        ' Add Button to the Form. Placement of the Button
        ' will be based on the Location and Size of button
        Controls.Add(dynamicButton)

    End Sub


'then create a functional control:

    Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MessageBox.Show("Button is clicked.")
    End Sub

' then the mouse control:


    Private Sub DynamicButton_MouseEnter(sender As Object, e As EventArgs)
     
        Cursor = Cursors.Hand
        dynamicButton.FlatAppearance.BorderSize = 1
        dynamicButton.FlatAppearance.BorderColor = Color.DodgerBlue
       'dynamicButton.ForeColor = Color.DodgerBlue

    End Sub


    Private Sub DynamicButton_MouseLeave(sender As Object, e As EventArgs)
    
        Cursor = Cursors.Default
        dynamicButton.FlatAppearance.BorderSize = 0
        'dynamicButton.ForeColor = Color.Transparent

    End Sub

And this is it...good coding...

Comments

Popular posts from this blog

How to create a rectangle

Move bordless Form