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

In this article you will learn how to Change Static themes to Dynamic themes using C Sharp with SQL Server.

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

Step 1

CREATE TABLE [dbo].[PROG_COLOR](

      [colorMainval] [varchar](15) NULL,

      [colorPanalval] [varchar](15) NULL,

) ON [PRIMARY


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.

Image1.jpg

Step 4

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

Image2.jpg

Image3.jpg
Step 5

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

Image4.jpg

Step 6

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

Image5.jpg

Step 7

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

Image6.jpg

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.

Image7.jpg

Image8.jpg


Step 10

Now we must double-click on frmChangeColor.

Image9.jpg

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.

Image10.jpg


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.

Image11.jpg


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

Image12.jpg

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.

Image13.jpg


Step 13

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

Image14.jpg

Step 14

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

Image15.jpg


Step 15

Now write the following code in the btnExit click event.

Image16.jpg

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.

Image17.jpg

Image17.5.jpg

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.

Image18.jpg

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

Image19.jpg

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

Image20.jpg

Image21.jpg

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

Image22.jpg

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