7 Replies - 8100 Views - Last Post: 07 January 2011 - 03:15 PM Rate Topic: -----

#1 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

generate c# code when dynamically adding controls to winform

Posted 07 January 2011 - 06:20 AM

Hi, I don't like to post topics like this, but I have searched google / bing for ages and yet to see a solution to this problem (if it's even feasible).

I have this simple code for example:
private void AddControlToForm(Control ctrl)
{
    Form newForm = new Form();
    newForm.Show();
    this.Controls.Add(ctrl);
}


Is there a way that I can get hold of the source code for this new form (like the <form>.Designer.cs file that VS generates?

Or am I going to have to self generate this code as I need to be able to save this file like newForm.cs

Any ideas or pointers would be appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: generate c# code when dynamically adding controls to winform

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: generate c# code when dynamically adding controls to winform

Posted 07 January 2011 - 07:36 AM

View Postbatesy3k, on 07 January 2011 - 06:20 AM, said:

Hi, I don't like to post topics like this, but I have searched google / bing for ages and yet to see a solution to this problem (if it's even feasible).

I have this simple code for example:
private void AddControlToForm(Control ctrl)
{
    Form newForm = new Form();
    newForm.Show();
    this.Controls.Add(ctrl);
}


Is there a way that I can get hold of the source code for this new form (like the <form>.Designer.cs file that VS generates?

Or am I going to have to self generate this code as I need to be able to save this file like newForm.cs

Any ideas or pointers would be appreciated :)



Visual Studio automatically adds controls to the forms.

About your code, while are you creating a new form and adding a control to the current form?
Was This Post Helpful? 0
  • +
  • -

#3 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: generate c# code when dynamically adding controls to winform

Posted 07 January 2011 - 07:40 AM

Oops, yeah that should be newForm.Controls.Add()... good spot :D

but to answer my question... how would I go about getting the source of the newly generated form with controls? Or do I need to manually create the source to save it?

This post has been edited by batesy3k: 07 January 2011 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#4 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: generate c# code when dynamically adding controls to winform

Posted 07 January 2011 - 07:44 AM

View Postbatesy3k, on 07 January 2011 - 07:40 AM, said:

Oops, yeah that should be newForm.Controls.Add()... good spot :D

but to answer my question... how would I go about getting the source of the newly generated form with controls? Or do I need to manually create the source to save it?


If you right click the InitializeComponect method in the Form's constructor and go to 'Go to Definition', it will take you to the auto-generated Form code. I hope that is what you want.
Was This Post Helpful? 0
  • +
  • -

#5 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: generate c# code when dynamically adding controls to winform

Posted 07 January 2011 - 07:50 AM

Sorry should have explained it slightly better:

The concept behind this is to create a form like designer for users of the system to create their own "views" using a selection of controls.

Thus the new form will be dynamically generated at runtime from a barebones form...
This is why I would like to know if there is a way of doign this, or will I have to write my own code saver to save the newly generated view (form) from the user to a .cs file?

Regards:)
Was This Post Helpful? 0
  • +
  • -

#6 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: generate c# code when dynamically adding controls to winform

Posted 07 January 2011 - 08:08 AM

View Postbatesy3k, on 07 January 2011 - 07:50 AM, said:

Sorry should have explained it slightly better:

The concept behind this is to create a form like designer for users of the system to create their own "views" using a selection of controls.

Thus the new form will be dynamically generated at runtime from a barebones form...
This is why I would like to know if there is a way of doign this, or will I have to write my own code saver to save the newly generated view (form) from the user to a .cs file?

Regards:)


Ah, well this is where it gets complicated. You can either make a designer (or find one) that is exactly like Visual Studios.

Or, you can simplify it. Add some buttons to your current form. Each button will corespond to a control. For example, pressing the Button button will add a button to your form. Then, you can press Test so how that designed form.

It is only as complicated as you make it.

For the form, make it a global variable so that you can always access it.
Form testForm = new Form();
private void AddControlToForm(Control ctrl)
{
        testForm.Controls.Add(ctrl);
}
public void ButtonBtn_Clicked(object sender, EventArgs e)
{
      AddControlToForm(new Button()); //Adds a button to that form.
}



I hope this gets you started.
Was This Post Helpful? 1
  • +
  • -

#7 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: generate c# code when dynamically adding controls to winform

Posted 07 January 2011 - 08:21 AM

Yeah I understand that part, I just need to know how to save it so all is not lost when they close the form down...

Tricky also on how to wire up these buttons for example... so if they click on a newly generated button, then it will perform an action...
Guess i'll just have to recreate what VS does when it creates a form with the code it produces... would that work?

Quote

Then, you can press Test so how that designed form.

Don't quite understand this... :)

This post has been edited by batesy3k: 07 January 2011 - 08:25 AM

Was This Post Helpful? 0
  • +
  • -

#8 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: generate c# code when dynamically adding controls to winform

Posted 07 January 2011 - 03:15 PM

View Postbatesy3k, on 07 January 2011 - 08:21 AM, said:

Yeah I understand that part, I just need to know how to save it so all is not lost when they close the form down...

Tricky also on how to wire up these buttons for example... so if they click on a newly generated button, then it will perform an action...
Guess i'll just have to recreate what VS does when it creates a form with the code it produces... would that work?

Quote

Then, you can press Test so how that designed form.

Don't quite understand this... :)



Take a look at this: http://www.developer...orms-designers/
That will get you started with having a designer interface like Visual Studio.

The next step is to serialize the Form to a file so you can open that Form up again. This is where I left off on a project from the past because this is a extremely hard part.

My guess is you can loop through the controls and their controls (and so on) and add each control to a xml file that you can reload at a later time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1