-
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:
Private Sub CmdRemovePictureBox_Click()
Me.Controls.Remove "Picture1"
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:wave:
-
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
-
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.
-
Re: Removing a control that was not added with the Controlers.add command
How about just setting the visible property to false?
-
Re: Removing a control that was not added with the Controlers.add command
-
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.
-
Re: Removing a control that was not added with the Controlers.add command
Try this:
VB Code:
' Put this at the top of your form.
Private Const WM_CLOSE As Long = &H10
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
And then to call:
VB Code:
SendMessage Command1.hwnd, WM_CLOSE, 0, 0
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
-
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?
-
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 ... :confused: :confused: :confused:
Can't you use control array of pictureboxes so they all share the same name but each has distinct index?
-
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
-
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:
Dim pct As Picturebox
Set pct = Me.Controls.Add("VB.Picturebox, "pctTemp")
'somewhere in your code:
Me.Controls.Remove "pctTemp"
-
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
-
Re: Removing a control that was not added with the Controlers.add command
What are talking about - create it during Form_Load event.
-
Re: Removing a control that was not added with the Controlers.add command
I tried that and it still gave me the same error.
-
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
-
Re: Removing a control that was not added with the Controlers.add command
ThunderPictureBoxDC if it helps :)
-
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 ...
-
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:
' These are the declerations. Put at the top of your form.
Private Const WM_CLOSE As Long = &H10
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
And to call:
VB Code:
SendMessage FindWindowEx(Me.hwnd, &0, "ThunderPictureBoxDC", vbNullString), WM_CLOSE, 0, 0
Me.Refresh
Cheers,
RyanJ
-
1 Attachment(s)
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.
-
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
-
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
-
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:
Dim pic As PictureBox
If shpCrop.Visible = False Then Exit Sub
Set pic = Controls.Add("VB.PictureBox", "pic")
With pic
.Width = intWidth
.Height = intHeight
.Left = Picture1.Width
.AutoRedraw = True
.Visible = True
End With
pic.PaintPicture Picture1.Picture, 0, 0, , , intLeft, intTop
Clipboard.Clear
Clipboard.SetData pic.Image
shpCrop.Visible = False
Set pic = Nothing
-
Re: Removing a control that was not added with the Controlers.add command
Quote:
Set pic = Controls.Add("VB.PictureBox", "pic")
if you just remove this line
change this to
pic.Picture = LoadPicture() 'Clear the picturebox
pete
-
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:
' In form Declarations
Private mobjPicture2 As PictureBox
' Handle picturebox events
Private Sub mobjPicture2_Click()
' etc.
End Sub
' Dynamically create the picturebox
Set mobjPicture2 = Controls.Add("VB.PictureBox", "Picture2")
' Destroy the picturebox
Controls.Remove "Picture2"
Set mobjPicture2 = Nothing
HTH
-
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.
-
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.
-
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 :thumb: Just wanted to show how it could be done if you really wanted to.
-
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
-
Re: Removing a control that was not added with the Controlers.add command
VB Code:
Set Pic2.Picture = pic.Picture
;)
-
1 Attachment(s)
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!
-
Re: Removing a control that was not added with the Controlers.add command
Quote:
Originally Posted by penagate
VB Code:
Set Pic2.Picture = pic.Picture
;)
Hello penagate,
Thank you for the code. But I had already tried that. :(
Stilekid07
-
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.
-
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:
Private Sub Command1_Click()
Dim pic As PictureBox
Set Pic2.Picture = pic.Picture
End Sub
And now I get a error "Object variable or With block variable not set"
-
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:
Private mobjPicture As PictureBox
Private Sub Form_Load()
Set mobjPicture = Controls.Add("VB.PictureBox", "Picture")
mobjPicture.Picture = LoadPicture([i]path[/i])
End Sub
Private Sub Command1_Click()
Set Pic2.Picture = mobjPicture.Picture
End Sub
Private Sub Form_QueryUnload()
Controls.Remove "Picture"
Set mobjPicture = Nothing
End Sub
-
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:
Private Sub Command1_Click()
Set Pic2.Picture = mobjPicture.Picture
End Sub
-
Re: Removing a control that was not added with the Controlers.add command
:confused:
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:
Private Sub Form_Load()
Controls.Add "VB.PictureBox", "Picture1"
Me!Picture1.Picture = LoadPicture(path)
Set mobjPicture = Me!Picture1
End Sub
-
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!!!! :thumb:
Stilekid007:wave:
-
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 :D
Glad it works :)