Results 1 to 38 of 38

Thread: Removing a control that was not added with the Controlers.add command {RESOLVED}

  1. #1

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Resolved Removing a control that was not added with the Controlers.add command {RESOLVED}

    Hello,

    I have a picture box that I need to be deleted from the program when the user clicks on a command button.

    I tried using

    VB Code:
    1. Private Sub CmdRemovePictureBox_Click()
    2. Me.Controls.Remove "Picture1"
    3. End Sub

    But I got the error "Controls.Remove can only remove controls added with controls.add."

    Is there a way to remove a picture box with a different command?

    Thank you very much!
    Stilekid007
    Last edited by stilekid007; Jun 10th, 2005 at 10:27 AM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Removing a control that was not added with the Controlers.add command

    That is correct. But what is it that you need to do - delete current picture from picturebox? If yes then simply set it to nothing:

    Set Picture1.Picture = Nothing

  3. #3

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Thank you for your speedy response. I need to remove the whole picture box so that it does not exist if that is possible.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Removing a control that was not added with the Controlers.add command

    How about just setting the visible property to false?


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing a control that was not added with the Controlers.add command

    Can you just HIDE it?

  6. #6

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    I would but I have other controls that will need to use the same name.

  7. #7
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Removing a control that was not added with the Controlers.add command

    Try this:

    VB Code:
    1. ' Put this at the top of your form.
    2. Private Const WM_CLOSE As Long = &H10
    3. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
    4.      ByVal hwnd As Long, _
    5.      ByVal wMsg As Long, _
    6.      ByVal wParam As Long, _
    7.      ByRef lParam As Any) As Long

    And then to call:

    VB Code:
    1. SendMessage Command1.hwnd, WM_CLOSE, 0, 0
    2. Me.Refresh

    Where Command1 is the name of the control

    Edit: that will not work because the Picturebox does not have a .hwnd s it is not a window its self...

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Removing a control that was not added with the Controlers.add command

    Well then how about adding it with the controls collection so then it can be removed?


    Has someone helped you? Then you can Rate their helpful post.

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Removing a control that was not added with the Controlers.add command

    Quote Originally Posted by stilekid007
    I would but I have other controls that will need to use the same name.
    What in world whould this mean ? I'm totally confused ...
    Can't you use control array of pictureboxes so they all share the same name but each has distinct index?

  10. #10

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    lol sorry - I will explain a little.

    I have a form with a picture box. This picture box has a shape control. When I click Crop - it allows me to drag the shape around the picture and then click copy and paste and it copies it to the clipboard and then makes a new picturebox named picture2 and then pastes it into picture2.

    Now I have a bunch of code for picture2 and in order to have that code picture2 has to exist other wise you get the varible not defined error.

    So I created the picture2 but I need to delete it in order to create it again when I crop it again.

    Sciguyryan,
    Thank you for the code. It seems to work (The picture box leaves the form) BUT when I try to create a new Picture box it says "There is already a control with the name "Picture1"
    So I am not sure if it worked or not.

    Stilekid007

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Removing a control that was not added with the Controlers.add command

    In thata case as manavo suggested create tempporary picturebox "on the fly" so you can remove it later:
    VB Code:
    1. Dim pct As Picturebox
    2.  
    3.     Set pct = Me.Controls.Add("VB.Picturebox, "pctTemp")
    4.  
    5. 'somewhere in your code:
    6.  
    7.     Me.Controls.Remove "pctTemp"

  12. #12

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Yes, I could do that but the picture box has to be created before I start the program because I would get the "Compile Error.. Variable Not defined

  13. #13
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Removing a control that was not added with the Controlers.add command

    What are talking about - create it during Form_Load event.

  14. #14

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    I tried that and it still gave me the same error.

  15. #15
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Removing a control that was not added with the Controlers.add command

    Does anyone know the PictureBox class?

    Maybe using that you could use the FIndWindow API call and then use that to send the close command too, if the Picturebox has one that is...

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  16. #16
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Removing a control that was not added with the Controlers.add command

    ThunderPictureBoxDC if it helps


    Has someone helped you? Then you can Rate their helpful post.

  17. #17
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Removing a control that was not added with the Controlers.add command

    Quote Originally Posted by stilekid007
    I tried that and it still gave me the same error.
    What did you try and what gives you an error? Show us your code ...

  18. #18
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Removing a control that was not added with the Controlers.add command

    Quote Originally Posted by manavo11
    ThunderPictureBoxDC if it helps

    Thanks, in that case try this:

    VB Code:
    1. ' These are the declerations. Put at the top of your form.
    2. Private Const WM_CLOSE As Long = &H10
    3. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
    4.      ByVal hwnd As Long, _
    5.      ByVal wMsg As Long, _
    6.      ByVal wParam As Long, _
    7.      ByRef lParam As Any) As Long
    8. Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
    9.     ByVal hWnd1 As Long, _
    10.     ByVal hWnd2 As Long, _
    11.     ByVal lpsz1 As String, _
    12.     ByVal lpsz2 As String) As Long

    And to call:

    VB Code:
    1. SendMessage FindWindowEx(Me.hwnd, &0, "ThunderPictureBoxDC", vbNullString), WM_CLOSE, 0, 0
    2. Me.Refresh

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  19. #19

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Hmm nothing is working. I have attached the program so that you all can have a look at it.
    Attached Files Attached Files

  20. #20
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Removing a control that was not added with the Controlers.add command

    Quote Originally Posted by stilekid007
    Hmm nothing is working. I have attached the program so that you all can have a look at it.
    The code I posted does work but it deleted the wrong one.
    If anyone knows a way to change which one it would delete first then a suggestion to that would be welcome

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  21. #21
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Removing a control that was not added with the Controlers.add command

    i have just looked at your code, but i don not understand why you need to remove the control and then add it again, why not just clear the picture and reuse it?

    pete

  22. #22

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Well I was looking at it and I was thinking. All I need to do is get this code below and instead of having it create the picture box "pic", Make it use the picture box that is already on the form (picturebox pic).

    So all I need to do is get this code and make it use the picture box instead of create it.

    I don't know how to get that to work though.
    VB Code:
    1. Dim pic As PictureBox
    2.    
    3.     If shpCrop.Visible = False Then Exit Sub
    4.    
    5.     Set pic = Controls.Add("VB.PictureBox", "pic")
    6.     With pic
    7.         .Width = intWidth
    8.         .Height = intHeight
    9.         .Left = Picture1.Width
    10.         .AutoRedraw = True
    11.         .Visible = True
    12.     End With
    13.    
    14.     pic.PaintPicture Picture1.Picture, 0, 0, , , intLeft, intTop
    15.    
    16.     Clipboard.Clear
    17.     Clipboard.SetData pic.Image
    18.    
    19.     shpCrop.Visible = False
    20.     Set pic = Nothing

  23. #23
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Removing a control that was not added with the Controlers.add command

    Set pic = Controls.Add("VB.PictureBox", "pic")
    if you just remove this line
    Set pic = Nothing
    change this to
    pic.Picture = LoadPicture() 'Clear the picturebox

    pete

  24. #24
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing a control that was not added with the Controlers.add command

    I'm not really understanding, but heres what I think you want (all code):

    VB Code:
    1. ' In form Declarations
    2. Private mobjPicture2 As PictureBox
    3.  
    4. ' Handle picturebox events
    5. Private Sub mobjPicture2_Click()
    6.     ' etc.
    7. End Sub
    8.  
    9. ' Dynamically create the picturebox
    10. Set mobjPicture2 = Controls.Add("VB.PictureBox", "Picture2")
    11.  
    12. ' Destroy the picturebox
    13. Controls.Remove "Picture2"
    14. Set mobjPicture2 = Nothing

    HTH

  25. #25
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing a control that was not added with the Controlers.add command

    Ryan the Picturebox has a .hWnd property too, so theres no need to use FindWindowEx

    But sending a WM_CLOSE message will only cause the window to close, not destroy the object itself. To do that you would need something like WM_DESTROY, but
    a) It probably wouldnt work
    b) If it did, it would muck up VB's Controls collection anyway, and you would probably get funny things happending.

  26. #26
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Removing a control that was not added with the Controlers.add command

    I don't see any reason to remove the picturebox, and create a new one.
    Just change the properties of the existing picturebox.
    Frans

  27. #27
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing a control that was not added with the Controlers.add command

    Quote Originally Posted by Frans C
    I don't see any reason to remove the picturebox, and create a new one.
    Just change the properties of the existing picturebox.
    Yep thats a much better solution Just wanted to show how it could be done if you really wanted to.

  28. #28

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Hello there everyone! Thank you for all your replys!

    Ok can someone tell me why this doesn't work with the program I attached here?

    All I want to do make pic.Picture to be sent to Pic2.Picture. BUT just run the program and see what happens.

    Code:
    Pic2.Picture = pic.Picture
    Thanks!
    Stilekid007

  29. #29
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing a control that was not added with the Controlers.add command

    VB Code:
    1. Set Pic2.Picture = pic.Picture

  30. #30

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    OOPS I forgot to attach the program. UH! I must be sleeping!
    Attached Files Attached Files

  31. #31

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Quote Originally Posted by penagate
    VB Code:
    1. Set Pic2.Picture = pic.Picture

    Hello penagate,

    Thank you for the code. But I had already tried that.

    Stilekid07

  32. #32
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing a control that was not added with the Controlers.add command

    It should work, I've done it many times before. What happens instead?

    I can't check cos my VB is stuffed.

  33. #33

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Well it works with a regular picture box but since pic was created via Controls.add it gives me an error Variable Not Defined... SO I put this code in.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim pic As PictureBox
    3.  
    4. Set Pic2.Picture = pic.Picture
    5.  
    6. End Sub

    And now I get a error "Object variable or With block variable not set"

  34. #34
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing a control that was not added with the Controlers.add command

    Thats cos you havent loaded the object yet.

    Did you try it my way?

    VB Code:
    1. Private mobjPicture As PictureBox
    2.  
    3. Private Sub Form_Load()
    4.     Set mobjPicture = Controls.Add("VB.PictureBox", "Picture")
    5.     mobjPicture.Picture = LoadPicture([i]path[/i])
    6. End Sub
    7.  
    8. Private Sub Command1_Click()
    9.     Set Pic2.Picture = mobjPicture.Picture
    10. End Sub
    11.  
    12. Private Sub Form_QueryUnload()
    13.     Controls.Remove "Picture"
    14.     Set mobjPicture = Nothing
    15. End Sub

  35. #35

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Yes I am using the code mentioned above but I still get the "Object variable or With block variable not set" with this peice of code.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Set Pic2.Picture = mobjPicture.Picture
    3. End Sub

  36. #36
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing a control that was not added with the Controlers.add command



    You're using that exact code? Are you sure you've loaded both mobjPicture, and the picture itself using LoadPicture?

    Also, I just realised, it probably isnt a good idea to call the picturebox itself "Picture"...

    If that still doesnt work then you could try this
    VB Code:
    1. Private Sub Form_Load()
    2.     Controls.Add "VB.PictureBox", "Picture1"
    3.     Me!Picture1.Picture = LoadPicture(path)
    4.     Set mobjPicture = Me!Picture1
    5. End Sub

  37. #37

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Removing a control that was not added with the Controlers.add command

    Ok, nameing it picture1 helped and it all works!

    Thank you penagate!!!!

    Stilekid007

  38. #38
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Removing a control that was not added with the Controlers.add command {RESOLVED}

    Sweet. I only realised afterwards that I had named it the same as the .Picture object. Doing that tends to get VB confused

    Glad it works

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