Changing Static Themes to Dynamic Themes Using C# With SQL Server

Changing Static themes to Dynamic themes using C# with SQL Server.

Step 1


      [colorMainval] [varchar](15) NULL,

      [colorPanalval] [varchar](15) NULL,


Step 2

insert into [PROG_COLOR] ([colorMainval],[colorPanalval])values('#c0c0ff','#8080ff')

Step 3

Start VS.Net and create a new Desktop project with the name Dynamic Theme.


Step 4

Now we must add two panels and two button controls to the form as shown in the figure.


Step 5

Now we must add another form with the name "frmChangeColor".


Step 6

Now we must place six buttons on the form and two labels as shown in the picture.


Step 7

Add the new class with the name Main. In my project.


Step 8

Now we are going to create the function GetDBConnection as a public function so it can be accessed anywhere in the project. But you must modify the data source setting according to your own setting. In my case I am using Conn for the connection setting but you can change that to your own.

For Example

  1. SQL Server instance name. (.\\CRMIS)
  2. SQL Server user name and password. (user id sa password .###ReconcilE123)
  3. Initial Catalog=PSH (database name)

Add the public static class in the Mian.cs

        public static SqlConnection GetDBConnection()


          SqlConnection conn = new SqlConnection(

         "Data Source=.\\CRMIS;Initial Catalog=PSH;User ID=sa;Password=###ReconcilE123");

         return conn;


Step 9

After adding the following code you will see the following error; to remove these errors you must insert using System.Data.SqlClient.



Step 10

Now we must double-click on frmChangeColor.


Now it's the time to write the code behind the form as shown in the picture.

In first step we create the connection from the SQL Server using the Main class that we defined in Step 9 and define the cmd1 object that we will use later.


Step 11

Now add the following code for the btnMain click event. That will define two variables of string type colorMain and colorPanal then it creates the object color from the class ColorDialog that C# built in the class. That object will open the color plate and then the user can select the main color. After selecting the color we must convert the string into type "x" and then save it in the colormain variable.

In the next line we must get the substring of the colormain and concatenate the "#" sign to it.

In the last line it will change the color of the button that is selected from the color plate.


Now we must write the same code for the btnpanal click event.


Step 12

Now write the following code for the btnsave click event. It will update the selected color in the database using an update SQL statement.


Step 13

Now write the following code in the btndef click event. This updates the default color.


Step 14

Now write the following code in the btnSys click event. This updates the System color in the database.


Step 15

Now write the following code in the btnExit click event.


Step 16

Now write the following code in form1 for the btnchange click event. This creates the object of form frmChangeColor and then shows the form.



Step 17

Now write the following code in form1 on btnExit click event.

Step 18

It's time to write your final code that will change the color of your application; in our case this will change the form 1 background and panels colors.

We must add one more public static function in Main class that will fetch data from the database as in the following.


Write the following code in the form1 MouseClick event. You can use the form load event as well.


After executing the program you must click on the form; it will show the following results.



On clicking the change button it will change the color of the form.


Know you must click the save button and click the exit button. Now you must click on form1 to see the results.

Up Next
    Ebook Download
    View all
    View all