Results 1 to 6 of 6

Thread: [RESOLVED] Auto updating one Picture Box when another is manually changed

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Resolved [RESOLVED] Auto updating one Picture Box when another is manually changed

    Hi

    I have a small app written in VB that I need to adapt so that when a Picture Box is manually updated with a drag and drop, another one next to it has its image automatically updated.

    There are two columns with ten PB's in each column. The left column has boxes named left_01, etc., etc., and the right column has boxes named right_01, etc., etc. (names simplified for the purpose of this post).

    I've tried extracting the name of the box on the right which has the drag and drop event and then extracting the last two characters, e.g. "05", and then appending that to "left_" to make a complete string of "left_05", which then gets stored in a variable called Result (I have verified that the variable contains the expected contents by displaying it in the console).

    I then attempt to update the image in PB left_05 automatically with the line:

    Result.Image = My.Resources.test

    This produces an error of 'Image is not a member of 'String' when I check the line of code which is underlined in red, so I'm obviously going in the wrong direction. I've searched around but can't find any info on how to get this done.

    Thanks for any help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Auto updating one Picture Box when another is manually changed

    If Result is type String then it's a String, not a PictureBox. If I was to write your name on a piece of paper and put it in my pocket, does that mean that I have you in my pocket? Of course not. Your name is not you, just as the Name of a PictureBox is not the PictureBox. If you want to get a control by name then one way is to index the Controls collection of its parent, e.g.
    vb.net Code:
    1. Dim pb = DirectCast(Me.Controls(Result), PictureBox)
    'pb' now refers to the PictureBox with that name.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Auto updating one Picture Box when another is manually changed

    On a separate note, there are two things that make this code less than ideal:
    vb.net Code:
    1. somePictureBox.Image = My.Resources.test
    Firstly, it creates a new Image object so, if it's executed multiple times, you're creating multiple identical Image objects when you only need one. Secondly, if that PictureBox already contained an Image then you have now lost access to it without disposing it.

    You really only need to correct one of those, as you would not be disposing Images if you're reusing them. Let's assume that you are using a new Image each time, which may be the case when not using resources. In that case, you should do something like this:
    vb.net Code:
    1. Dim oldImage = somePictureBox.Image
    2.  
    3. oldImage.Dispose()
    4. somePictureBox.Image = My.Resources.test
    Any object that supports disposal should be disposed when you're done with it. In the case of an Image object, disposing it returns its image handle back to the OS. You should avoid keeping such resources longer than you need to.

    In this case though, I don't think that you need to dispose the image at that point because you should probably be reusing Images. When you use My.Resources, every time you access a property you are extracting the data and creating a new object, so that code will create a new Image object every time it executes. A more efficient approach is to access the property once, assign the result to a field and then reuse that field multiple times. That way, you use the same Image over and over. You can then dispose that Image object via that field when you know you don't need it any more.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Re: Auto updating one Picture Box when another is manually changed

    Quote Originally Posted by jmcilhinney View Post
    If Result is type String then it's a String, not a PictureBox. If I was to write your name on a piece of paper and put it in my pocket, does that mean that I have you in my pocket? Of course not. Your name is not you, just as the Name of a PictureBox is not the PictureBox. If you want to get a control by name then one way is to index the Controls collection of its parent, e.g.
    vb.net Code:
    1. Dim pb = DirectCast(Me.Controls(Result), PictureBox)
    'pb' now refers to the PictureBox with that name.
    Thanks very much for taking the time to reply and for the information. I particularly like your 'piece of paper' analogy which helped to clarify some confusion I was having about the mechanics of this issue. Also, thanks for the explanation and suggestion regarding disposing of objects, something I'll be paying more attention to.

    I've been trying this evening to get your method to work but was getting errors when I tested it. It seemed to be the Result part of ...Controls(Result)... that was giving me the trouble as it seemed to be needing to be part of a method. I tried quite a few different things to correct the issue and also tried to find out more online but in the end my inexperience kept me from correcting the issue.

    Eventually I stumbled upon another method that turned out to help me progress, although I don't know if it would be classed as an elegant solution.

    It involved me extracting the final two characters of the Picture Box that had been targeted for a drop. I did this because the Picture Box I needed to update as a result would always have the same last two characters as well, e.g. 03, 04, etc. (the range was from 01 to 11).

    I then created another string of the first five characters of the name of the Picture Box I needed to update. All the PB's in this column would start with "left_" followed by their corresponding number. I then added the two strings together and stored them in a variable.

    This variable was then passed into the following line:

    Code:
    CType(myTestPanel.Controls(Result), PictureBox).Image = My.Resources.blank
    This gave me the desired effect and caused the PB immediately to the left of the PB the user had interacted with to become my blank placeholder image. So, I'm pleased about that and it has allowed me to progress with the updates but, as I said, I'm not experienced enough to know if what I've done is considered as sacrilege as far as correct coding is concerned...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Auto updating one Picture Box when another is manually changed

    That's not another method. that is exactly what I told you to do:
    If you want to get a control by name then one way is to index the Controls collection of its parent, e.g.
    vb.net Code:
    1. Dim pb = DirectCast(Me.Controls(Result), PictureBox)
    The EXAMPLE I provided was for a control whose parent is the form. If your control's parent is a Panel then you need to use the Controls collection of that, which is exactly what your code does.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    3

    Re: Auto updating one Picture Box when another is manually changed

    Ok, so this is a classic example of only knowing part of the story, as in I haven't yet got the knowledge to know that the two situations were so close to each other in functionality and essentially only differing in specification of their respective parent.

    Thanks again for the help, much appreciated.

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