Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How to implement own design environment instead of InitializeComponent() which is the default?
Posted
Updated 24-Mar-11 10:30am
v4
Comments
Sandeep Mewara 24-Mar-11 9:14am    
Can you elaborate what are you trying to do?

Pay attention: InitializeComponent is not a member of System.Windows.Forms.Form. This is just the artifact of available Designer. You can do whatever your want instead. Basically, you need to develop your own variant of some function which is called from within the form's constructor, pretty much the way Wayne advised.

Now, I don't see any reason for doing that. WinForms is evil. WPF is much better. Using WinForms with available designer is good enough to make heavy investment in yet another designer. However… The only goal I see is to provide Form designer for Mono.

(One note personally for you, Mehdi. Please, no offense: You're absolutely not ready to get to any work of such level. I'm well familiar with tens of your Questions and the level of understanding the Answer. Please, listen to a good friendly advice: finally, take a hard work and learn some programming first, stop asking your Questions at CodeProject wich do not result in any work. Do something real. Honestly…)

—SA
 
Share this answer
 
Comments
Henry Minute 24-Mar-11 15:23pm    
+5 for the personal note.

The rest was pretty good too. :)
Sergey Alexandrovich Kryukov 24-Mar-11 15:45pm    
Thank you, Henry.
--SA
Espen Harlinn 25-Mar-11 4:36am    
Good advice - 5ed!
Sergey Alexandrovich Kryukov 25-Mar-11 12:19pm    
Thank you, Espen.
--SA
You are probably looking for something like this:
Have a Great DesignTime Experience with a Powerful DesignSurface (Extended) Class[^]

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Mar-11 0:02am    
Very interesting reference, my 5.
--SA
Espen Harlinn 25-Mar-11 4:36am    
Thank you, SAKryukov!
Instead of adding a form to your solution, add a class and then let it inherit from Form. then in the constructor you can create your own controls as you see fit like this:
C#
class MyForm:Form
    {
        public MyForm()
        {
            Initialise();
        }
        private void Initialise()
        {
            Button myButton = new Button();
            myButton.Text = "Press Me";
            myButton.Click += new EventHandler(myButton_Click);
        }
        void myButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World");
        }
    }


Hope this helps
 
Share this answer
 
Comments
[no name] 24-Mar-11 11:49am    
in designtime not show messagebox ,why do not worked?
Sergey Alexandrovich Kryukov 24-Mar-11 15:19pm    
Agree. My 5. I added some in my Answer...
--SA
InitializeComponent is a call to the designer generated code for whatever environment you're working in (WPF, Winforms)

If you want design type support for your application, you're going to have to call InitializeComponent. This is what sets up whatever controls you've put on your form, places them in the specified locations etc.

If you want to do absolutely everything by hand, the approach from Wayne will work.
 
Share this answer
 
Comments
Wayne Gaylard 24-Mar-11 9:27am    
It is definitely best to stick with VS generated partial classes.

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