How to make a button addition system with unique events for Launch?

Asked

Viewed 42 times

1

I would like to know how to make a system in which automatically adds in a Flowlayoutpanel, for each file of a folder, a button with a specific event that, when clicking, starts the application. The code I know how to do is something like this:

Try
        For Each file As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
            Dim mbtn As New MaterialSkin.Controls.MaterialFlatButton
            mbtn.Icon = (Icon.ExtractAssociatedIcon(file)).ToBitmap
            mbtn.Text = My.Computer.FileSystem.GetName(file)
            mbtn.Tag = file
            flp.Controls.Add(mbtn)
        Next
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

With this, I’ve been able to do everything but open the link system. And I’d also like to know how to hide the extension. I await answers :)

  • Unanswered..... ;-;

  • ;-;-;-;-;-;-;-; WHY THAT

  • 1

    If this "Materialflatbutton" control has events, add one with the AddHandler: AddHandler mbtn.Click, AddressOf mbtn_Click and then declare its function: Private Sub mbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs). In case you don’t bring the extension use: mbtn.Text = Path.GetFileNameWithoutExtension(fileName).

  • 1

    @Smael, your comment is actually the answer to the question of the post, I think you should publish it as an answer...

1 answer

2

You will need to create an event at your control.
Use the Addhandler, see:

AddHandler mbtn.Click, AddressOf mbtn_Click

Then state your function mbtn_Click:

Private Sub mbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
   ' ...seu código
End Sub

Its function will be executed whenever the click event is triggered at its controls. If you want something specific for a certain control, you will have to do a treatment using the parameter sender.

Private Sub mbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Select CType(sender, MaterialFlatButton).Name
    Case "MaterialFlatButton1_Nome"
        ' seu código
    Case "MaterialFlatButton2_Nome"
        ' seu código
    End Select
End Sub

In case you don’t bring the extension use:

mbtn.Text = Path.GetFileNameWithoutExtension(file)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.