Introduction
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 Dynamic name Theme.
Step 4
As shown in the figure, we must add two panels and two button controls to the form.
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 will create 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 setting. In my case, I am using Conn for the connection setting, but you can change that to your own.
For Example
- SQL Server instance name. (.\\CRMIS)
- SQL Server user name and password. (user id sa password .###ReconcilE123)
- 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; insert using System to remove these errors.Data.SqlClient.
Step 10
Now we must double-click on frmChangeColor.
Now it's time to write the code behind the form, as shown in the picture.
In the first step, we create the connection from the SQL Server using the Main class we defined in Step 9 and define the cmd1 object 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, allowing the user to 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 updated 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 the btnExit click event.
Step 18
It's time to write your final code to change your application's color; in our case, this will change the form 1 background and panels colors.
As in the following, we must add one more public static function in the Main class to fetch data from the database.
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 form 1 to see the results.
Conclusion
This article taught us how to Change Static themes to Dynamic themes using C Sharp with SQL Server.