1

here is my code

Public Sub Personalize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each fi As FileInfo In New DirectoryInfo(Application.StartupPath + "\web").GetFiles
        Dim pictooltip As New ToolTip
        Dim pbx As New Button
        AddHandler pbx.Click, AddressOf pbx_click
        pbx.Width = 150
        pbx.Height = 150
        pbx.BackgroundImage = Image.FromFile(fi.FullName)
        wallpapers.Controls.Add(pbx)
        pbx.Cursor = Cursors.Hand
        pictooltip.SetToolTip(pbx, fi.Name)
        pbx.BackgroundImageLayout = ImageLayout.Stretch

    Next
End Sub

Private Sub pbx_click()
    main.BackgroundImage = Image.FromFile(fi.FullName)
End Sub

i can't figure how to use "fi" in pbx_click()

any hint for that ??

4 Answers 4

2

Just put the FullName() into the Tag() property of the Button and pull it back out when it gets clicked:

Dim pbx As New Button
pbx.Tag = fi.FullName

Pulling it back out:

Private Sub pbx_click()
    main.BackgroundImage = Image.FromFile(DirectCast(sender, control).Tag.ToString())
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

Try this,

Public Class Form1

Dim fi as FileInfo

Public Sub Personalize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each Me.fi In New DirectoryInfo(Application.StartupPath + "\web").GetFiles
        Dim pictooltip As New ToolTip
        Dim pbx As New Button
        AddHandler pbx.Click, AddressOf pbx_click
        pbx.Width = 150
        pbx.Height = 150
        pbx.BackgroundImage = Image.FromFile(fi.FullName)
        wallpapers.Controls.Add(pbx)
        pbx.Cursor = Cursors.Hand
        pictooltip.SetToolTip(pbx, fi.Name)
        pbx.BackgroundImageLayout = ImageLayout.Stretch

    Next

End Sub

Private Sub pbx_click()
    main.BackgroundImage = Image.FromFile(fi.FullName)
End Sub

End Class

1 Comment

Not working :( (Object reference not set to an instance of an object.)
0

Your Fi is declared locally in the first subroutine (or sub).

Declare Fi outside of the Personalize_Load sub, then pass data to it.

 Dim fi as String = ""

Public Sub Personalize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each fii As FileInfo In New DirectoryInfo(Application.StartupPath + "\web").GetFiles
    Dim pictooltip As New ToolTip
    Dim pbx As New Button
    AddHandler pbx.Click, AddressOf pbx_click
    pbx.Width = 150
    pbx.Height = 150
    pbx.BackgroundImage = Image.FromFile(fii.FullName)

    fi = fii.FullName

    wallpapers.Controls.Add(pbx)
    pbx.Cursor = Cursors.Hand
    pictooltip.SetToolTip(pbx, fi.Name)
    pbx.BackgroundImageLayout = ImageLayout.Stretch

    Next
 End Sub

Private Sub pbx_click()
    main.BackgroundImage = Image.FromFile(fi)
End Sub

You can then use pbx_click() by simply calling it on any sub, or trying it to a handler.

Comments

0

Change your event to this:

Private Sub pbx_click(sender As Object, e As System.EventArgs)
    main.BackgroundImage = Image.FromFile(DirectCast(sender, Button).BackgroundImage)
End Sub

Or, you can use the Tag property and store some serialized data. See here: Add parameter to Button click event

2 Comments

Type 'RoutedEventArgs' is not defined :(
@Ahmad Visioń - Just make it System.EventArgs. Wasn't sure if you were using WPF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.