Use a string to represent a control name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghunadhs
    New Member
    • May 2007
    • 52

    Use a string to represent a control name

    Hi All,
    Here my doubt is regarding to a picture box in VB 6.0

    assume that i have few picture boxes in my appliaction named Picture1,pictur e2,picture3..et c.

    And in my application, i have to load the pictures dynamically. i can load like...
    Picture1.pictur e=Load picture("path of a .bmp)
    Picture2.pictur e=Load picture("path of a .bmp)
    Picture3.pictur e=Load picture("path of a .bmp)

    But i am not interested to load like this. i want to write a method like this..

    private sub subLoadPictures (byval pictrueName as string, byval PicturePath as string)
    pictureName.pic ture=load picture(picture path)
    end sub

    I tried this one, but could not get working properly.. could u please say how to use a string that represents the name of a picture.

    Thanks:
    regards:
    raghunadhs.
  • Robbie
    New Member
    • Mar 2007
    • 180

    #2
    Originally posted by raghunadhs
    Hi All,
    Here my doubt is regarding to a picture box in VB 6.0

    assume that i have few picture boxes in my appliaction named Picture1,pictur e2,picture3..et c.

    And in my application, i have to load the pictures dynamically. i can load like...
    Picture1.pictur e=Load picture("path of a .bmp)
    Picture2.pictur e=Load picture("path of a .bmp)
    Picture3.pictur e=Load picture("path of a .bmp)

    But i am not interested to load like this. i want to write a method like this..

    private sub subLoadPictures (byval pictrueName as string, byval PicturePath as string)
    pictureName.pic ture=load picture(picture path)
    end sub

    I tried this one, but could not get working properly.. could u please say how to use a string that represents the name of a picture.

    Thanks:
    regards:
    raghunadhs.
    If you're using PictureBox controls, that's the type you should use for your sub.
    Using your example:

    private sub subLoadPictures (PictureObj as PictureBox, byval PicturePath as string)
    PictureObj.pict ure=load picture(picture path)
    end sub

    Notice also that I got rid of the ByVal. You probably shouldn't use that when referring to an actual object, rather than just a property of it. ;)
    (I THINK that is true, at least)

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Thanks again, Robbie.

      raghunadhs, you might be interested to know that you can refer to a control using a string as you suggested, by making use of the Controls collection. (Though normally you shouldn't need to). Assuming this code resides within a form (not a code module), you could have said:
      [CODE=vb]Private Sub subLoadPicture( ByVal PictureName As String, ByVal PicturePath As String)
      Me.Controls(Pic tureName).Pictu re = LoadPicture(Pic turePath)
      end sub[/CODE]
      A couple of things to consider...
      • Your terms are a bit confusing. PictureName is the name of a PictureBox control, not the actual "picture". I think you need to differentiate between the picture and the control that displays it.
      • It's probably better to just pass the control, as Robbie showed.
      • If you haven't already, I highly recommend that you learn about control arrays. They are very useful for this kind of thing.

      Comment

      • raghunadhs
        New Member
        • May 2007
        • 52

        #4
        Thanks again Killer, and thanks Robbie...
        it is working. Killer... I am using the control arrays. But there are so many type of control arrays like picture1(0),pic ture2(0).. like that. I think you are familiar with my previous thread (drag and drop objects, in that example I am adding some more picture boxes).
        For doing some experiments with that stuff I needed this type of common method which can represent a particular pictureBox, by taking picturebox name, and path as parameters.

        Anyhow both of your examples have helped me a lot.

        Thanks:
        regards:
        raghunahds

        Originally posted by Killer42
        Thanks again, Robbie.

        raghunadhs, you might be interested to know that you can refer to a control using a string as you suggested, by making use of the Controls collection. (Though normally you shouldn't need to). Assuming this code resides within a form (not a code module), you could have said:
        [CODE=vb]Private Sub subLoadPicture( ByVal PictureName As String, ByVal[/CODE]
        Last edited by Killer42; Aug 20 '07, 09:47 AM. Reason: Just tidied up a little.

        Comment

        Working...