Results 1 to 7 of 7

Thread: [RESOLVED] Run function from another form using Button click

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2020
    Posts
    20

    Resolved [RESOLVED] Run function from another form using Button click

    I have a series of buttons that I would like to be able to click on that would run a function on a separate form and be able to pass a variable to that function. For example:

    When any of the following buttons are clicked:

    [button 01] [button 02] [button 03] [button 04]

    They pass their corresponding number on to a function on another form to be inserted into the code (XX is the passed variable.

    PictureBoxMain.BackgroundImage = MyResources.Image_XX

    How would I go about doing this?

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,012

    Re: Run function from another form using Button click

    You make a Public Sub (it doesn't sound like you need a Function) in the Second Form's class, and you call it.
    i.e. a call like this in Button1's handler

    SecondForm.ExampleSub(1)
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2020
    Posts
    20

    Re: Run function from another form using Button click

    Thank you. Yes, sub would work fine.

    However, how do I get that number into the control declaration? I have multiple images (Image1, Image2, Image3, etc.). I can't seem to figure out how to append that number to it...

    Private Sub ExampleSub(ByVal ChosenImage As Integer)
    PictureBoxMain.BackgroundImage = MyResources.ImageX
    End Sub

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,441

    Re: Run function from another form using Button click

    Rather than passing an Integer you can pass an Image (or another data type).

    That would allow you to pass the resource from the caller. For example, if Button1 is supposed to show MyResources.Image1, you could have code like this in the Button1 click event:
    Code:
    ExampleSub(MyResources.Image1)
    ...and ExampleSub would be like this:
    Code:
    Private Sub ExampleSub(ByVal ChosenImage As Image)
    PictureBoxMain.BackgroundImage = ChosenImage
    End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2020
    Posts
    20

    Re: Run function from another form using Button click

    Unfortunately this wont work as I am using passed number to do a few other things.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,441

    Re: Run function from another form using Button click

    Well there are a variety of ways you could deal with it, such as:

    • pass two parameters to the sub, the Integer and the Image
    • use a Select Case (or If's) to set the image inside the routine
    • use an array to store the images, and use the Integer value as an index to the array


    The Select Case could be like this:
    Code:
    Private Sub ExampleSub(ByVal ChosenImage As Integer)
      Select Case ChosenImage
      Case 1
        PictureBoxMain.BackgroundImage = MyResources.Image1
      Case 2
        PictureBoxMain.BackgroundImage = MyResources.Image2
    ...

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,018

    Re: Run function from another form using Button click

    You can also get a resource by name using the ResourceManager property. This is done off the top of my head so I can't guarantee that it's 100% correct but you can do something like this:
    vb.net Code:
    1. PictureBoxMain.BackgroundImage = DirectCast(My.Resources.ResourceManager.GetObject($"Image_{imageNumber:00}"), Image)
    By the way, should you really besetting the BackgroundImage property of a PictureBox? Almost certainly not. Any time I've seen someone do it it was wrong and indicative of the fact that they hadn't done their research properly. The whole point of a PictureBox is its Image property so, unless you're using it for GDI+, you're doing it wrong if you're not using the Image property. BackgroundImage is a property that all controls have and, as the name suggests, can be used to display an image in the background of the primary content. Given that the primary content for a PictureBox is an image, displaying an image in the background almost never makes sense.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width