Click here to Skip to main content
15,920,053 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
(1)
I am working on windows form application.

I want to create another picturebox when click in a picturebox.
Here is my code
C#
            PictureBox p = (PictureBox)sender; // Picturebox in which user clicks
            PictureBox pb = new PictureBox();

// Set the properties of new picturebox as similar to that of clicked one
            pb.Image = p.Image;
            pb.Location = new Point(100,100);
            pb.Size = p.Size;

But it doesn't show the new picturebox on the specified location.

(2)
Also, Is there any way to create a semi transparent picturebox ?

(3) How to make a picturebox transparent over next picturebox ? Setting the picturebox's backcolor to Transparent didn't work ?
Posted

Well, and how your UI is supposed to "know" that this picture box even exists? :-)
To show a control, also set its parent. Say, you have some Control parent; which can be any container control: Panel, TabPage, Form. Then
C#
p.Parent = parent; // adds this control to parent
// same as:
parent.Control.Add(p);


—SA
 
Share this answer
 
v2
Comments
BibhutiAlmighty 9-Jan-15 13:13pm    
What is "parent" ?
Sergey Alexandrovich Kryukov 9-Jan-15 13:29pm    
Please read again. Do pay at least some attention when you read the answer.
Wait a minute, let me highlight the identifier in the text...
In case you don't understand the concept of parent-child relationship, you should better stop doing what you are doing and learn it.
—SA
BillWoodruff 9-Jan-15 16:48pm    
fyi: newcomers to .NET Win Forms often have trouble distinguishing that the case of setting the Parent of a Control has the same result as using 'Add on the ControlCollection of a Container Control's 'Controls Property. This is compounded, sometimes, by the fact that some Win Form Controls which are not really meant to by used as Container Controls allow adding Controls to their exposed 'Controls Collection.
Sergey Alexandrovich Kryukov 9-Jan-15 17:15pm    
Understand, that's why I always emphasize this not so self-apparent fact.
—SA
[no name] 9-Jan-15 18:01pm    
Sorry to ask this here. I do not know .NET very well. But I know Borland VCL (very well). In bcb there is an owner and a parent (for WinControl). Owner is responsible for dealloc, parent is the WinControl where "this" needs to be displayed. Is there something comparable in .NET? I ask this because .NET manages the memory so on a first glance owner is not needed....
Thanks, Bruno
1) Add the new picturebox to the control list[^] of the form (or of the original pictures's parent)
2-3) Translucency is not simple, but can be done: http://bobpowell.net/transcontrols.aspx[^]
 
Share this answer
 
C#
PictureBox p = (PictureBox)sender; // Picturebox in which user clicks
PictureBox pb = new PictureBox();

// Set the properties of new picturebox as similar to that of clicked one
pb.Image = p.Image;

// [1]
p.Parent.Controls.Add(pb);

pb.Location = new Point(100,100);
pb.Size = p.Size;
[1] Assuming you want to the new PictureBox whose Properties you copy from the source PictureBox to appear at the location 100,100 in the co-ordinate space in which the source PictureBox is located: you need to add the new PictureBox to the Control Collection of the Parent Control in which the source PictureBox exists. After you do that, you can say that both the source and new PictureBoxes have the same Parent.

Setting the Parent Property of a Control to a Container Control has the same effect as using the 'Add method of a Container Control's 'Controls Property (which is a ControlCollection object).

If you are creating multiple PictureBoxes at run-time, I'd suggest making a UserControl that you could use as a "base:" that way, all the visual-appearance properties would be automatically identical every time you created a new one. Or, you could make a Custom Control that inherited from PictureBox.

If you use such a "factory method," and keep track of the last Control created, then, if you wish, you can automatically position the next one created relative to where the previously created one is.

Transparency in Windows Forms is a tricky business, and it is inherently limited; if you really want to work with transparency, I suggest WPF.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900