Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Dynamic Project Creation C#

4.74/5 (16 votes)
8 Feb 2015CPOL5 min read 36.3K   2K  
Dynamic Project Creation using Winform C#

Image 1

Introduction

In my previous article, Windows Form Design at Run Time, I explained how to design form at runtime. I have extended the program to the next advanced version with more functionality.

Limitation in My Previous Article

In my previous article, it had a limitation like user needs to design and add code for events like binding grid, etc., user can load only one form.

New Features in Dynamic Project Creatation

This has all my previous article features but with more different functionality like:

  1. New program has two parts, 1st is Form View part where user can view all their dynamically created forms by selecting from the treeview.
  2. 2nd Form Design part is to Add New/Edit/Save and Open Form dynamically.
  3. User does not need to write any code.
  4. Easy form design.
  5. User can enter SQL query to bind the result to selected grid.
  6. User can add parameter and select dynamic textbox control for parameter, which needs to be used in SQL Where condition.
  7. Save and Open Form.
  8. User can add/edit SQL Query and parameter for selected form.

1st Form View Part

In the below image, I pointed each part by Numbers.

  1. When user clicks on “New Form Design” Button, Form will be open where users can Add New, Edit, Open and Save Forms. We can see more details in Form Design part.
  2. In Treeview, I will list all the Form Names. Whenever users create new Form and save, the saved filename will be listed in Treeview. When user clicks on Treeview Node, the related form will be loaded right side.
  3. Here, for example, we can see that, now user has clicked on “Itemsearch” from Treeview and the related form has been loaded:

Image 2

2nd Form Design Part

When user clicks on “New Form Design” Button, the form design will be open. You can refer to the below image for how the Design form looks. This form plays the main role for creating dynamic form at runtime. Here, users can Add New, Edit, Open and Save Forms. In the below image, I have pointed each part by Numbers.

  1. In the Left side, we have the Tool Menu where user can add Textbox, Label, Button, DataGridView, etc. at runtime to design the form.
  2. User can design their form here. User can drag and drop controls, resize controls.
  3. Using the property grid, users can change the property of each selected controls BackColor, FontColor, Text, etc.

    Image 3

  4. This is the important part for designing the form.

Here, user can enter the SQL query which needs to be bind to grid.

User can add the where condition fieldname at “Parameter Condition Name”.

User can add controls like textbox at “TextBox Name” which needs to be checked in where Condition of SQL Query.

User can add the Control to Bind like DataGridView name which will be used to bind the SQL result.

If there is no where condition for SQL query, for example, if we need to display all the records without condition, then you can leave empty for “Parameter Condition Name” and “TextBox Name”. You can see the above form which has no parameter and needs to bind all the data to the grid.

Let’s see another example for adding the parameter and TextBox name for search and bind the result to the grid.

Image 4

Here, you can see in this form I have Item Name text field and a Button for search.

For example, I want search like the below query:

SQL
Select Item_Code, Item_Name, Price, tax1, Discount, _
Description from ItemMasters where Item_Name like + @ItemName +’%’

For the above query, user can enter the query like below:

SQL
"Select Item_Code,Item_Name,Price,tax1,Discount,Description from ItemMasters"

For the Where Condition Field and parameter, user can enter the “Parameter Condition Name” with “Item_Name” and for parameter input, user can select the Textbox from the form. Whenever users click on the textbox, the related TextBox control name will be displayed at the Textbox Name field.

Save the Form

In the Toolbar, user has the option to save the worked Form to be viewed from the main screen. You can refer to the below image. When users click on Save Toolbar button, list of Main Menu names will be displayed, here all the previous saved File Name (I will be using the file name as Menu name to display in the main screen Treeview). If users want to save the Form as Submenu to an existing menu, then user can select the Main Menu name and enter the new Form name to save. If user wants the Form should be as a Main Menu, then he can select the “Select” from Combobox and enter the New Form Name. Once the Form is saved, the Menu will be added with new Form Name on main screen.

Note: When user enters the existing form name to save, then I will overwrite the existing form with new saved version. I will be saving the forms as XML files.

Image 5

Open Existing Form

In the Toolbar, user has the option to open an existing Form for modification. You can refer to the below image. When users click on Open Toolbar button list of menu names (which is our Form Name) will be displayed, user can select their Form Name from the list which needs to be open for modification.

Image 6

Using the Code

The article is an extension of my previous article (Windows Form Design at Run Time). Mostly, I have reused the same code part with few more modifications.

Creating Tables with Sample Data Insert

First, we can create the below tables and insert few records to execute my existing forms.

SQL
CREATE TABLE [dbo].[ItemMasters](
	[Item_Code] [varchar](20) NOT NULL,
	[Item_Name] [varchar](100) NOT NULL,
	[Price]  Int NOT NULL,
	[TAX1]  Int NOT NULL,
	[Discount]  Int NOT NULL,
	[Description] [varchar](200) NOT NULL,
	[IN_DATE] [datetime] NOT NULL,
	[IN_USR_ID] [varchar](20) NOT NULL,
	[UP_DATE] [datetime] NOT NULL,
	[UP_USR_ID] [varchar](20) NOT NULL,
 CONSTRAINT [PK_ItemMasters] PRIMARY KEY CLUSTERED 
(
	[Item_Code] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, _
 IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Insert Item Master

SQL
INSERT INTO [ItemMasters]   ([Item_Code],[Item_Name],[Price],_
            [TAX1],[Discount],[Description],[IN_DATE]
           ,[IN_USR_ID],[UP_DATE],[UP_USR_ID])
     VALUES
           ('Item001','Coke',55,1,0,'Coke which need to be cold',GETDATE(),'SHANU'
           ,GETDATE(),'SHANU')
           
INSERT INTO [ItemMasters]   ([Item_Code],[Item_Name],[Price],[TAX1],_
            [Discount],[Description],[IN_DATE]
           ,[IN_USR_ID],[UP_DATE],[UP_USR_ID])
     VALUES
           ('Item002','Coffee',40,0,2,_
            'Coffe Might be Hot or Cold user choice',GETDATE(),'SHANU'
           ,GETDATE(),'SHANU')
           
INSERT INTO [ItemMasters]   ([Item_Code],[Item_Name],[Price],[TAX1],_
            [Discount],[Description],[IN_DATE]
           ,[IN_USR_ID],[UP_DATE],[UP_USR_ID])
     VALUES
           ('Item003','Chiken Burger',125,2,5,'Spicy',GETDATE(),'SHANU'
           ,GETDATE(),'SHANU')
           
INSERT INTO [ItemMasters]   ([Item_Code],[Item_Name],[Price],[TAX1],_
            [Discount],[Description],[IN_DATE]
           ,[IN_USR_ID],[UP_DATE],[UP_USR_ID])
     VALUES
           ('Item004','Potato Fry',15,0,0,'No Comments',GETDATE(),'SHANU'
           ,GETDATE(),'SHANU')

DBconnection String

In my project bin folder, you can find the “DBConnection.txt” text file. Replace the connection string to your local database connection.

Image 7

In my program every time during the database connection, I will check for the “DBConnection.txt”, if the text file is not available in bin folder, I will create a next text file with sample connection string.

C#
private String ReadConnectionString()
        {
            string path = Application.StartupPath + @"\DBConnection.txt";
            String connectionString = "";
            if (!File.Exists(path))
            {
                using (StreamWriter tw = File.CreateText(path))
                {
                    tw.WriteLine("Data Source=YourDBServerName;
                    Initial Catalog=YOURDBNAME;User id = DBUID;password=DBPWD");
                    tw.Close();
                }
            }
            else
            {
                TextReader tr = new StreamReader(path);
                connectionString = tr.ReadLine();
                tr.Close();
            }
            return connectionString;
        }

History

  • 15th January, 2015: Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)