2

I'm an MS Access programmer moving over to C#. In MsAccess I have a routine where I open create a new form in design view and then the routine populates it with controls based upon the fields present in a SqlServer table. Not only does it create the controls, it creates the default event handlers that I use. it's a great time saver.

So now i'm trying to do this in C#. I've got a routine which creates controls dynamically like this:

MyTextBox=new System.Windows.Forms.TextBox();
this.Controls.Add(MyTextBox);

My issue is that the controls aren't permanent. When I go back to the form design, none of the dynamically created controls are there. Is there a way to do this?

I think my last resort would be to make my routine open the .CS sourcecode file and write lines of C# code in there. But if there's an easier way to do it I'd like to know please.

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • No, not easily anyway. Without implementing the design time serialization system and writing to your *.designer.cs files, anything you change runtime won't be reflected design time. The simplest solution is to save the control information in an XML or other file and load it the next time the application runs, restoring controls. – Ron Beyer Jul 01 '15 at 15:19
  • With the time you spend writing that routine to build the form, you could just build the form by hand. Build your app, dont write code to build your app. its a time waster. see here: http://www.hanselman.com/blog/YakShavingDefinedIllGetThatDoneAsSoonAsIShaveThisYak.aspx – Glenn Ferrie Jul 01 '15 at 15:19
  • I have dozens of forms to create, some of which have up to 40 controls. Each control has a minimum of 5 event handlers. My MsAccess routine sets up the form in a second, then all I have to do is drag the controls to the correct place. Yak shaving doesn't come into it. – Ian Pendlebury Jul 01 '15 at 15:27
  • You are probably using the wrong project template then, it seems what you need is to use LightSwitch, not a forms application. See https://msdn.microsoft.com/en-us/library/ff851953.aspx – Ron Beyer Jul 01 '15 at 15:34
  • Is this routine used at run-time or design-time? In WinForms, the form definition, such as it is, is persisted in the form of code, so that is what you need to generate. I would agree with @GlennFerrie, though, that you probably can find a better way to do this than maintain code that generates code. – cdkMoose Jul 01 '15 at 15:56
  • Ten years ago I met a company making a big CRM system. All their Forms were created from parameters read in from a databese. All on the fly and highly flexible. It worked like a charm Today machines are so much faster, keeping thing dynamic is really not a problem. - Of course you also __could__ write CS files..[create DLLs](http://stackoverflow.com/questions/604501/generating-dll-assembly-dynamically-at-run-time) from them and [load them at runtime](http://stackoverflow.com/questions/1087794/c-sharp-load-a-dll-file-and-access-methods-from-class-within). - Somewhat involved.. – TaW Jul 01 '15 at 16:20
  • 1
    I've got started with this now. I wrote a routine which copies each line from the .Designer.cs file into records in a table. Then it writes them back into a temporary file inserting new lines where appropriate, in order to define my new fields. Yes it's not simple, but it's teaching me the C# language as I go along. So i'm enjoying doing it. – Ian Pendlebury Jul 01 '15 at 20:29

2 Answers2

0

So basically, you want to point your "Wizard" at a "data source" and have it produce a "standard" form. Which you then modify.

It's not a repetitive or runtime thing, it's a "run once".

You need a templating engine, such as T4 or CodeSmith.

And from there we wander into opinion territory, so I'll shut up.

Roger Willcocks
  • 1,649
  • 13
  • 27
-1

Subclass the Form class and in its constructor create and add your controls.

then you can select it as a template when you are add new form to your project.

your template form may be :

public partial class TempleteForm : Form
{
    public TextBox MyTextBox;
    public TempleteForm()
    {
        MyTextBox = new System.Windows.Forms.TextBox();
        this.Controls.Add(MyTextBox);
    }
}

enter image description here

enter image description here

Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47