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,
Step 2insert into [PROG_COLOR] ([colorMainval],[colorPanalval])values('#c0c0ff','#8080ff')Step 3Start VS.Net and create a new Desktop project with the name Dynamic Theme.Step 4Now we must add two panels and two button controls to the form as shown in the figure.Step 5Now we must add another form with the name "frmChangeColor". Step 6Now we must place six buttons on the form and two labels as shown in the picture.Step 7Add the new class with the name Main. In my project.Step 8Now 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
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 9After adding the following code you will see the following error; to remove these errors you must insert using System.Data.SqlClient.Step 10Now 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 11Now 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 12Now write the following code for the btnsave click event. It will update the selected color in the database using an update SQL statement.Step 13Now write the following code in the btndef click event. This updates the default color.Step 14Now write the following code in the btnSys click event. This updates the System color in the database.Step 15Now write the following code in the btnExit click event. Step 16Now write the following code in form1 for the btnchange click event. This creates the object of form frmChangeColor and then shows the form.Step 17Now write the following code in form1 on btnExit click event.Step 18It'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.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: