Jun 9th, 2005, 03:40 PM
#1
Thread Starter
Hyperactive Member
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
Last edited by stilekid007; Jun 10th, 2005 at 10:27 AM .
Jun 9th, 2005, 03:42 PM
#2
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
Jun 9th, 2005, 03:43 PM
#3
Thread Starter
Hyperactive Member
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.
Jun 9th, 2005, 03:44 PM
#4
Jun 9th, 2005, 03:45 PM
#5
Re: Removing a control that was not added with the Controlers.add command
Jun 9th, 2005, 03:46 PM
#6
Thread Starter
Hyperactive Member
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.
Jun 9th, 2005, 03:48 PM
#7
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
Jun 9th, 2005, 03:50 PM
#8
Jun 9th, 2005, 04:00 PM
#9
Re: Removing a control that was not added with the Controlers.add command
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?
Jun 9th, 2005, 04:06 PM
#10
Thread Starter
Hyperactive Member
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
Jun 9th, 2005, 04:20 PM
#11
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"
Jun 9th, 2005, 04:23 PM
#12
Thread Starter
Hyperactive Member
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
Jun 9th, 2005, 04:36 PM
#13
Re: Removing a control that was not added with the Controlers.add command
What are talking about - create it during Form_Load event.
Jun 9th, 2005, 04:42 PM
#14
Thread Starter
Hyperactive Member
Re: Removing a control that was not added with the Controlers.add command
I tried that and it still gave me the same error.
Jun 9th, 2005, 04:46 PM
#15
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
Jun 9th, 2005, 04:49 PM
#16
Jun 9th, 2005, 04:50 PM
#17
Re: Removing a control that was not added with the Controlers.add command
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 ...
Jun 9th, 2005, 04:56 PM
#18
Re: Removing a control that was not added with the Controlers.add command
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
Jun 9th, 2005, 05:20 PM
#19
Thread Starter
Hyperactive Member
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
Jun 9th, 2005, 05:26 PM
#20
Re: Removing a control that was not added with the Controlers.add command
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
Jun 9th, 2005, 09:02 PM
#21
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
Jun 9th, 2005, 11:23 PM
#22
Thread Starter
Hyperactive Member
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
Jun 9th, 2005, 11:44 PM
#23
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
change this to
pic.Picture = LoadPicture() 'Clear the picturebox
pete
Jun 10th, 2005, 06:31 AM
#24
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
Jun 10th, 2005, 06:32 AM
#25
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.
Jun 10th, 2005, 06:40 AM
#26
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.
Jun 10th, 2005, 06:58 AM
#27
Re: Removing a control that was not added with the Controlers.add command
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.
Jun 10th, 2005, 08:59 AM
#28
Thread Starter
Hyperactive Member
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
Jun 10th, 2005, 09:22 AM
#29
Re: Removing a control that was not added with the Controlers.add command
VB Code:
Set Pic2.Picture = pic.Picture
Jun 10th, 2005, 09:22 AM
#30
Thread Starter
Hyperactive Member
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
Jun 10th, 2005, 09:27 AM
#31
Thread Starter
Hyperactive Member
Re: Removing a control that was not added with the Controlers.add command
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
Jun 10th, 2005, 09:32 AM
#32
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.
Jun 10th, 2005, 09:42 AM
#33
Thread Starter
Hyperactive Member
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"
Jun 10th, 2005, 09:51 AM
#34
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
Jun 10th, 2005, 10:11 AM
#35
Thread Starter
Hyperactive Member
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
Jun 10th, 2005, 10:18 AM
#36
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:
Private Sub Form_Load()
Controls.Add "VB.PictureBox", "Picture1"
Me!Picture1.Picture = LoadPicture(path)
Set mobjPicture = Me!Picture1
End Sub
Jun 10th, 2005, 10:25 AM
#37
Thread Starter
Hyperactive Member
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
Jun 10th, 2005, 10:28 AM
#38
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
Forum Rules
Click Here to Expand Forum to Full Width