Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI FRIENDS,

How can i print a form ona panel component?
thank you for all
Posted
Comments
OriginalGriff 9-May-11 15:43pm    
No, it still makes no sense.
Would you please use the "Improve Question" widget, and try to explain in more detail what you are trying to do: at the moment it looks like you are trying to put a form inside a panel...
Kim Togo 9-May-11 15:46pm    
You want to print on a printer => paper ?
guendouz bachir 9-May-11 17:10pm    
yes, I want put a form inside a panel...
charles henington 9-May-11 17:54pm    
is the form the current form or a differnt form? will the panel be include in the panel if is the current form?
guendouz bachir 9-May-11 18:22pm    
no, a different form
example: we like to put form2 inside panel and this panel is in form1

No, you cannot.

[EDIT]

The short answer would be "this is not recommended". Please see solution by Dave, but it needs care.

For more information, please see discussion (and my answer) on the past question:
Is it possible to run a window form application from another running window form application[^]

—SA
 
Share this answer
 
v3
Comments
Anthony Mushrow 9-May-11 22:57pm    
Well actually, you could use the Win32 SetParent function to make the panel the parent of the other form. But err, not recommended.
Sergey Alexandrovich Kryukov 9-May-11 23:13pm    
I know, I know. I took it into account and after some thinking decided to say "you cannot". Technically speaking, you're right, but effect of it... It can create a great mess... I used to try many tricks on low-level API...
--SA
Dave Kreskowiak 9-May-11 23:30pm    
Actually, not impossible, and quite easy. You don't even need Win32 functions to do it!
Sergey Alexandrovich Kryukov 10-May-11 0:33am    
I see. Does it really work? A form should have WS_POPUP style...
--SA
Dave Kreskowiak 10-May-11 7:10am    
Yes, it works. I've seen it done way too many times.
It's pretty easy to do. The Form class derives from Control, so you just need to add the form instance to the Controls collection of the Panel you want. Oh, but only after you set the Forms TopLevel property to False. Just remember that your still using a Form. You still have to Show the form to get it to show up!
MyForm mf = new MyForm();
mf.TopLevel = false;
Panel1.Controls.Add(mf);
mf.Show();
 
Share this answer
 
Comments
guendouz bachir 10-May-11 10:28am    
thank you Mr Dave Krakowiak ,,, but what i do when i want just to put component of form2 inside fomr1 ?
Dave Kreskowiak 10-May-11 10:52am    
The short answer is you can't. It's either the entire form or you have to create new instances of the controls you want to add to the Panel.

The long answer is it's possible, but extremely bad practice, to move controls from one form to another. You do NOT want to do this unless you have a very good grasp on what is going on, how control events are wired up, and what the pitfalls are. There are no advantages doing this and frankly, it makes your code more confusing to support and harder to debug if you run into problems.
Sergey Alexandrovich Kryukov 10-May-11 13:39pm    
And this is my turn to say "it's pretty easy to do". When you execute myControl.Parent = anotherForm, the controls simply jumps from an old parent to a new one. Of course, the advantage is at least questionable but I don't think it's more then the advantage of sticking a form in a panel.
--SA
Dave Kreskowiak 10-May-11 14:22pm    
Yeah, but whether the controls work as intended or should have some other meaning if left up to how the event handlers are wired.

As for the advantage, you have to keep in mind what happens when/if one of the forms is destroyed, but not the other. You also have a condition possible where one form is created (the panel host) and the other (supplying the controls) doesn't exist. You're two forms are forever bound together and not seperately reusable.
Sergey Alexandrovich Kryukov 10-May-11 21:40pm    
If does not matter. When a control is jumped, it is not destroyed; GS won't let it go, as the control is referenced in new place. Do this trick, remove the donor form and you will see that is works.
--SA

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