Click here to Skip to main content
14,172,361 members
Click here to Skip to main content
Add your own
alternative version

Stats

29.9K views
1.7K downloads
32 bookmarked
Posted 29 Jan 2015
Licenced CPOL

Dynamic Project Creation C#

, 8 Feb 2015
Rate this:
Please Sign up or sign in to vote.
Dynamic Project Creation using Winform C#

Introduction

In my previous article (Windows Form Design at Run Time)

I have explained about how to design form at runtime. I have extended the program to next advance version with more functionality.

Limitation in my previous article:

In my previous article it has limitation like user needs to design and add code for events like binding grid and 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 2 part 1st is Form View part where user can view all there dynamically created forms by selecting from the tree view.
  2. 2nd Form Design part is to Add New/Edit/Save and Open Form dynamically.
  3. User no needs 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 have 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 detail in Form Design part.

2) In Tree view I will list all the Form Name. Whenever users create new Form and save. The saved filename will be listed in Treeview .When user click on Treeview Node related form will be loaded right side.

3) Here for example we can see that, now user have clicked on “Itemsearch” from Treeview and the related form has been loaded

2nd Form Design part:

When user click on “New Form Design” Button the form design will be open. You can refer 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 and etc at runtime to design the form.

2) User can design there 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 and etc.

4) This is the important part for design 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.

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

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.

“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 option to save the worked Form to be viewed from the main screen. You can refer 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.

Open Existing Form: In the Toolbar user has option to open an existing Form for modification. You can refer 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.

Using the code

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

Creating Tables with Sample data insert:

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

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: 

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.

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.

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

  1. Initial release  - 2015-01-15.

License

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

Share

About the Author

syed shanu
Team Leader
India India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog

My Interview on Microsoft TechNet Wiki Ninja Link

You may also be interested in...

Comments and Discussions

 
QuestionNice Pin
mak199029-Jan-15 12:29
membermak199029-Jan-15 12:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190524.3 | Last Updated 8 Feb 2015
Article Copyright 2015 by syed shanu
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid