Ahmad Visioń
Ahmad Visioń

Reputation: 57

vb.net passing variables between controls

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 ??

Upvotes: 1

Views: 720

Answers (4)

mattmc3
mattmc3

Reputation: 18335

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

Upvotes: 0

Malcolm Salvador
Malcolm Salvador

Reputation: 1566

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.

Upvotes: 0

Idle_Mind
Idle_Mind

Reputation: 39132

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

Upvotes: 2

Codemunkeee
Codemunkeee

Reputation: 1613

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

Upvotes: 0

Related Questions