Thread: dynamic form

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    12

    dynamic form

    Hi
    Is it possible to have some dynamic form in C# ?

    In php and other , we can have it easily....but the concern here is of c#

    The problem is, I want the program to have textboxes/checkboxes for all the attributes that a database is going to have.

    Say, for eg: if a database has columns like id,name,age,
    then i would like to dynamically generate a form which contains textbox for each of these attributes....is it possible ? if so, how do i do it ?


    well it has to be dynamic as database structure might get changed later on and the program needs to be flexible.

    any ideas ???

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I think it is only possible in scripting languages, but I'm not sure.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Sure, just create a form with a flowlayout panel. Then create the textboxes (depending on how the database table looks like) and add them to the panel. They will be arranged automatically.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    the thing is, the checkbox and other controls need to be generated on the fly and dynamic..


    Quote Originally Posted by Magos View Post
    Sure, just create a form with a flowlayout panel. Then create the textboxes (depending on how the database table looks like) and add them to the panel. They will be arranged automatically.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure it can be done, but it's not entirely trivial - you'd have to build your own data structures (just like it's heck of a lot easier to create a menu by adding it in a resource editor than building your own menus in runtime).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by sangfroid View Post
    the thing is, the checkbox and other controls need to be generated on the fly and dynamic..
    Well that's what I meant. Assuming a flowlayoutpanel already exists, it might be something like this:

    Code:
    		private void CreateStuffDynamically()
    		{
    			FlowLayoutPanel1.Controls.Add(CreateCheckBoxControl("Are you male?"));
    			FlowLayoutPanel1.Controls.Add(CreateTextBoxControl("First name:"));
    			FlowLayoutPanel1.Controls.Add(CreateTextBoxControl("Last name:"));
    		}
    
    		//A simple control, just a checkbox
    		private System.Windows.Forms.Control CreateCheckBoxControl(string Text)
    		{
    			System.Windows.Forms.CheckBox CheckBox = new System.Windows.Forms.CheckBox();
    			CheckBox.Text = Text;
    
    			return CheckBox;
    		}
    
    		//A more advanced control, a combination of a label and a textbox
    		//(combining stuff like this is easier if you design a UserControl in the editor)
    		private System.Windows.Forms.Control CreateTextBoxControl(string Text)
    		{
    			System.Windows.Forms.Panel Panel = new System.Windows.Forms.Panel();
    			Panel.AutoSize = true;
    
    			System.Windows.Forms.Label Label = new System.Windows.Forms.Label();
    			Label.Text = Text;
    			Panel.Controls.Add(Label);
    
    			System.Windows.Forms.TextBox TextBox = new System.Windows.Forms.TextBox();
    			TextBox.Left = Label.Right;
    			TextBox.Width = 100;
    			Panel.Controls.Add(TextBox);
    
    			return Panel;
    		}
    There are 2 functions for creating the controls. CreateCheckBoxControl simply creates a check box, as simple as that. CreateTextBoxControl creates a combination of a label and a textbox showing that you're not limited to simple controls. You don't have to create all this manually, in Visual Studio you can create a UserControl control to get a reusable group of controls.

    CreateStuffDynamically uses the above 2 functions to create 3 controls and add them to the flowlayout panel. In your case you do not hardcode it like this, you base it off whatever you have in your database.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling datas from another form?
    By Aga^^ in forum C# Programming
    Replies: 2
    Last Post: 02-06-2009, 02:17 AM
  2. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 09:27 AM
  3. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM
  4. Making an MFC form to suit
    By TJJ in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2004, 11:20 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM