Click here to Skip to main content
15,911,528 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,
I am working on a windows project right now on my own.I had designed and coded almost of the program.And an Idea just strike my head. But I cant figure any possible ways to do it. Please help me in this matter.

First Project:
I have a form with button controls. And if I press each button another forms will appear as
" *.ShowDialog() ". I just done it in old fashion way.

New Idea :

I just made my parent form a little bit bigger. And I placed a panel there.

Now I need the controls, events and whatever in " *.ShowDialog() " to appear in my panel when I press that button.

That is: I just need to transfer whole form into a panel dynamically and execute the functions and events .

Waiting for your kindness.

Irshad
Posted
Comments
CHill60 30-Apr-13 9:54am    
Can I just check I've got this right ... the dialog forms are already in your project, so instead of creating panels at design time and putting the controls onto them, you want to transfer controls from the existing form that would have appeared when you push a particular button, onto a panel on your "main" form instead ...
Member 10012743 30-Apr-13 9:56am    
yes.
and it works as just as the old form itself.
CHill60 30-Apr-13 10:06am    
Hm ... see OriginalGriff's solution ... it is possible by using the this.Controls.Add construct but you could get into an awful mess if there is any code behind on those dialog forms. Much better to do this at design time rather than dynamically - I agree with OriginalGriff
Sergey Alexandrovich Kryukov 30-Apr-13 10:11am    
First of all, why? Why?! I generally see no reason to answer such questions if the idea is not properly motivated.
—SA
Member 10012743 30-Apr-13 10:29am    
see Mr. Sergey,
I just need a help in a matter. I ask for your favor.
I am not a professional, so please help.

How could I add an event handler dynamically?

1 solution

I wouldn't do it that way.
Instead, I would create the collection of controls as a UserControl, and include one of those in the form, or in the panel instead. It's easier to do, and an lot more manageable.
 
Share this answer
 
Comments
Member 10012743 30-Apr-13 11:39am    
Thanks OriginalGriff and CHill60.

So I will have to create a dynamic panel with the controls right ?
So how could I add the events (when pressing a button) and invoke them dynamically?
OriginalGriff 30-Apr-13 11:49am    
No, make a UserControl - it's similar to a panel, but it's designed to be a stand alone control. It can have properties and events of it's own, and the the outside world knows nothing about what it actually contains.
So you decide what events the outside world needs to know about("data entered" or whatever - I don't know your application) and it signals them for the containing control or form to handle.

Easy to make: Right click your project, and select "Add...User control". Resize it, drop your buttons, and stuff on and hook up the code copied from your existing form. Compile, and it appears in the tool box so you can drag'n'drop it onto your forms, or you can create it dynamically in exactly the same way you do a Button or TextBox, but you get all the controls it contains, and the functionality you gave them.
Member 10012743 30-Apr-13 16:16pm    
thanks
let me check it out.
OriginalGriff 1-May-13 3:35am    
You're welcome!

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