Results 1 to 17 of 17

Thread: [RESOLVED] Why does this not work ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,460

    Resolved [RESOLVED] Why does this not work ?

    Hi,
    I have a PictureBox (Fred) sitting exactly on top of an identical size PictureBox (PictureBox1).
    I'm trying to find the name of the PictureBox beneth Fred.
    I only need Fred's image so I can capture that and dispose Fred.
    Code:
        Private Sub FredUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Fred.MouseUp
            Dim img As Image = Fred.Image
            Fred.Dispose()
            Me.Refresh()
            Dim ctrl As Control = Me.GetChildAtPoint(Cursor.Position)
        End Sub
    The cursor hasn't moved since MouseUP so I expect the cursor to be inside the lower PictureBox (PictureBox1)
    which would be visible were it not for Fred sitting on top of it. However...
    ...with a break point at 'End Sub' and running the code, ctrl.name is 'Fred' !
    How can this be ? As you can see, I've added 'Me.Refresh', just incase I needed it.

    Poppa
    Last edited by Poppa Mintin; May 25th, 2024 at 05:31 AM.
    Along with the sunshine there has to be a little rain sometime.

  2. #2

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,460

    Re: Why does this not work ?

    Hi again,

    On further investigation via the Watch Window, Fred still exists, I don't understand why.

    Pop
    Along with the sunshine there has to be a little rain sometime.

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,593

    Re: Why does this not work ?

    Disposing of a control doesn't remove or destroy the control, it certainly doesn't remove the variable.

    What are you expecting the code to do?

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

    Re: Why does this not work ?

    Of course the control still exists. Disposing an object doesn't make it cease to exist. It's about releasing resources. In the case of a control, that is most specifically a window handle. The .NET object still exists though.

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,243

    Re: Why does this not work ?

    You should explain exactly what the situation is and what your goal is here. There is almost certainly a "more correct" way of going about whatever you are trying to accomplish, that is significantly less convoluted.

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,543

    Re: Why does this not work ?

    Z-order of the controls?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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

    Re: Why does this not work ?

    Quote Originally Posted by PlausiblyDamp View Post
    Disposing of a control doesn't remove [...] the control
    Actually, disposing a control does remove it from its parent's Controls collection. Without a window handle, which is released as part of disposal, it cannot be displayed, so it cannot be part of that collection. I suspect that that all won't happen until the current event handler completes though. It looks like Poppa might think that the Refresh call would force that immediately but it isn't. I wonder whether a call to Application.DoEvents would do the trick. Of course, if you find that that method solves your problem, you're probably doing it wrong to begin with.

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,460

    Re: Why does this not work ?

    Ah... Thanks guys.
    Ok so that explains that.
    Is there a way to remove an object then ?

    Pop.
    Along with the sunshine there has to be a little rain sometime.

  9. #9
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,593

    Re: Why does this not work ?

    Quote Originally Posted by jmcilhinney View Post
    Actually, disposing a control does remove it from its parent's Controls collection. Without a window handle, which is released as part of disposal, it cannot be displayed, so it cannot be part of that collection. I suspect that that all won't happen until the current event handler completes though. It looks like Poppa might think that the Refresh call would force that immediately but it isn't. I wonder whether a call to Application.DoEvents would do the trick. Of course, if you find that that method solves your problem, you're probably doing it wrong to begin with.
    I never realised that! That will teach me to make assumptions!

    Even though it does remove the visible control due to the handle being released, it still however leaves the managed properties intact - checking the control's name would still display the name.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,222

    Re: Why does this not work ?

    Quote Originally Posted by Poppa Mintin View Post
    Ah... Thanks guys.
    Ok so that explains that.
    Is there a way to remove an object then ?

    Pop.
    Yes. You are essentially right about removing the object, as far as the UI is concerned, but not as far as your needs are concerned. I expect it's because you are still in the event handler. It WILL go away, just...not quite yet. You might try the DoEvents, as JMC suggested, you might try setting Fred = Nothing. That usually isn't of any value, but it might be in this case. I don't know the details of the order in which things go away. I'd be a bit surprised that Dispose really does anything useful (from your perspective), because it should just allow releasing stuff and setting the control as disposed. It would be quite inefficient if it was destroyed as soon as you call Dispose. Better would just to be to flag it and leave it until there is time to get rid of it. Setting the variable to Nothing doesn't get rid of it, either, but it clears the variable, and that may be enough for your purpose...depending on what your purpose ultimately is.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,460

    Re: Why does this not work ?

    Ok, Here's my problem.

    I'm trying to code a game of patience (solitaire) wherein I'd like to drag a card from one pile to another, the only Drag and Drop suggestions I can find are for a specific PictureBox, (or other control), to another specific control. I was trying to find a way of being able to 'Drag' a card, face-up, from one place to another, that's no problem if it's just from the same PictureBox to the same other PictureBox, but I want to be able to select to drag any of thirteen pictureboxes to any of thirteen other pictureboxes.

    The principal problem is that I need to be able to click a PictureBox (to turn the card face-up) as well as dragging it. Obvoiusly click includes MouseDown and MouseUp which confuses MouseMove, DragEnter, DragOver, MouseDown and MouseUp.

    I was trying to generate a PictureBox, set it's image to a relevant card face so that I had a specific PictureBox to drag, drag it to a relevant other PictureBox, save it's image, delete it and put it's image into the PictureBox that it was over. There's the reason for this thread.

    It would be nice to be able to just drag the image itself from one PictureBox to another, I can't find a way to do that, the nearest I've achieved (and I couldn't actually drag it) the image was about half the size of the PictureBox that it was in. (all my pictureboxes are set to StretchImage)

    Generating an Addhandler for each PictureBox is no problem but separating the 'Click' and the 'Drag' methods is bit of a headache. I have a trial app' in which I can move images from any PictureBox to any other PictureBox but it's not drag, it only uses click.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  12. #12
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,031

    Re: Why does this not work ?

    This link may be of interest:
    https://github.com/DualBrain/Solitaire
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,222

    Re: Why does this not work ?

    Don't use the click method. You want only MouseDown, DragEnter, DragDrop, and possibly GiveFeedback. None of the other methods.

    I would also suggest that you get beyond images. Create a small class that has members for the front image, the back image, and some indication of which one is shown. You might also want to include something for the drag image used in giving feedback such that the card is shown as being drawn. This would be essentially a custom mouse pointer icon, though you'd have to figure out where the target area is in this icon (probably the center, though one corner might also work).

    This Card Class can be placed in the .Tag property of the picturebox. On MouseDown, you'd have very little to do, but you might play around with changing which image is shown (should there be third image for "selected"). Otherwise, MouseDown will be essentially just this:

    pb.DoDragDrop(pb, DragDropEffects.Move)

    DragEnter doesn't have to do much. In fact, it might be the case that it can do nothing at all, but it has to be there.

    All the work will be on DragDrop. What is being dropped is the PB that was dragged, and that PB will have the Card object in the .Tag property, so you put that Card into the .Tag property of the destination, decide which image should be shown (set whatever class properties, if that's the way you go, which it seems like you should), then set the image according to the Card.

    There are a variety of ways that can work, but the basic outline looks like that. The whole idea of the Card object may be a bit overwrought, but I think it will end up being more convenient, as you can contain the concept of the front and back, as well as whether the card is selected, shown, hidden, and so on. Ultimately, the Card will make things easier.
    My usual boring signature: Nothing

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,544

    Re: Why does this not work ?

    Quote Originally Posted by Shaggy Hiker
    …You might also want to include something for the drag image used in giving feedback such that the card is shown as being drawn. This would be essentially a custom mouse pointer icon, though you'd have to figure out where the target area is in this icon (probably the center, though one corner might also work).
    You can create a custom DragImage which is the whole card image as it is, using the mouse location on that card at mousedown as the hotspot

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,222

    Re: Why does this not work ?

    Yeah, that would be good. Whenever I've used a drag image, there was always some obvious item to use that was not the item that was dragged, but in the case of a card, the card itself is the obvious drag image.
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,460

    Re: Why does this not work ?

    Hi y'all

    I persevered with my original idea, using a PictureBox to transport an image from one PictureBox to another.
    In a trial app' I put 12 pictureboxes on Form1, and added another to use as the transport, (I named it 'Taxi'), and five images in resources.

    To do the Drag and Drop I used MouseDown, MouseMove and MouseUp. I put the 'taxi', with the cardback showing, where I want to pick up a card, drag it to any space (PictureBox with no image, or even to one already having an image) and drop it in. As soon as it's dropped, I hide the 'Taxi' and shunt it off to 0,0 leaving the PictureBox beneath free to accept the image. I originally used the Click to turn the card face-up, but I realised that I could do that with the MouseDown instead.

    You can download my whole trial app. from here. You should remember that it's only a trial, so variable names aren't very helpful. (sorry)

    There's no instructions with it because it's only a trial. But just drag the cardback image to any indicated space and release with the cursor inside your selected space. A new card back will arrive in a vacant space.

    It's a shame that I didn't give this thread a more specific name, marking it as resolved doesn't help anyone !
    Still, my initial question was answered.

    Thanks guys.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,544

    Re: Why does this not work ?

    Quote Originally Posted by Poppa Mintin View Post
    …You can download my whole trial app. from here.
    That’s what the codebank is for.

    Quote Originally Posted by Poppa Mintin View Post
    …It's a shame that I didn't give this thread a more specific name
    Well, bear that in mind when you put it in the codebank

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