dcsimg
Results 1 to 7 of 7
  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    68

    Resolved [RESOLVED] Loop buttons to add an image

    I have 20 buttons with a stop sign as the image. I am cleaning up the code in my program and want to reduce lines Below is how I assign an image to each button.

    Code >>>
    Button01.Image = My.Resources.Stopsign
    Button02.Image = My.Resources.Stopsign
    Button03.Image = My.Resources.Stopsign
    ...
    Button20.Image = My.Resources.Stopsign
    End Code <<<

    I got the brilliant idea of using a for...next loop to add images to the buttons:


    Code>>>

    dim Sign_Img(20)

    For x = 1 To 20
    Sign_Img(x) = "Button" & x.ToString().PadLeft(2, "0")

    Dim newSignstring As String = String.Concat(Sign_Img(x), ".Image = My.Resources.Stopsign")

    newsignstring
    Next x
    End Code <<<


    I am getting the "expression is not a method" error. Is it possible to do what I want to do? I think I could do this if I dynamically added buttons, but I am too far in the program to dynamically add the buttons.
    Thanks
    Carroll

  2. #2
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    440

    Re: Loop buttons to add an image

    Few ways to do it, here's one:
    Code:
        Private Sub ControlsExample_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each ctrl As Control In YourContainer.Controls
                If TypeOf ctrl Is Button Then
                    DirectCast(ctrl, Button).Image = My.Resources.NOIMG
                End If
            Next
        End Sub

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,936

    Re: Loop buttons to add an image

    When you post code please put it inside code tags so it is displayed in a more readable way - either using the Code/VBCode buttons in the post editor screen (or at the top of the Quick Reply box), or by putting them in manually, like this: [code] code here [/code]


    As for your question, it wont work the way you are trying... because what you have created is just a String (which is data), not code.

    There are several ways you could do it, probably the easiest of which would be to create an array of buttons (either just before the loop, or at class level so it can be re-used elsewhere), eg:
    Code:
    Dim theButtons as Button() = {Button01, Button02, Button03, ... , Button20}
    
        For Each buttonToChange in theButtons
            buttonToChange.Image = My.Resources.Stopsign
        Next

  4. #4
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    440

    Re: Loop buttons to add an image

    Having another look, it looks like he also wants to create the button dynamically.
    In that case I would recomend a FlowLayOutPanel
    Code:
        Private Sub ControlsExample_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i As Integer = 0 To 20
                FlowLayoutPanel1.Controls.Add(New Button With {
                                              .Image = My.Resources.NOIMG})
            Next
        End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    68

    Re: Loop buttons to add an image

    SI_The_Geek - Thanks I think that is the solution I am looking for. I'll try it and see .Sorry about the code.

    KPMC - Sorry, I already have the button layout created, so I don't want to dynamically create the buttons in this application. In future code I will, but I am too far into this project 8-)

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,499

    Re: Loop buttons to add an image

    Are you planning on modifying the image in the buttons?
    Right now you're creating 20 copies of the image so are using 20 times as much memory as needed.

    If I'm not going to modify the images, then I would just load it once from the resource and then set the buttons to reference that image.
    Code:
       Dim Buttons as Button() = {Button01, Button02, Button03, ... , Button20}
       Dim StopSignImage As Image = My.Resources.Stopsign
    
        For Each buttonToChange in theButtons
            buttonToChange.Image = StopSignImage
        Next
    If the button images were going to be changed in the future to some image from a small subset of images, I would load the subset of images into a form scope level variables (or array) once at startup, and reference those images when needed. The images would only be loaded once, and the picturebox would reference them when you assign to the image property, so not take any additional memory for the image they contain.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2016
    Posts
    68

    Re: Loop buttons to add an image

    Thanks to everyone who responded. I went with S_The_Geeks suggestion. It is working well for me. Thanks

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width