8 Replies - 1433 Views - Last Post: 13 July 2009 - 03:44 AM Rate Topic: -----

#1 s2009   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-June 09

Retaining the changes made to the backcolor property

Post icon  Posted 11 July 2009 - 11:52 PM

Hi all,

I am developing a windows application.

I have 3 forms:

I want to change the backcolor of all the 3 forms to the color selected by the user.

I have used the following code I am able to change the backcolor but When I exit the application and restart it I am not able to get the color that user has set. I am getting the default colour only.

Is it possible to retain the colour selected by the user and use it as backcolor when the user restarts the application.

CODE

In Form1

ColorDialog c1 = new ColorDialog();
		public static System.Drawing.Color bkc;
		private void button1_Click(object sender, EventArgs e)
		{

			DialogResult res = c1.ShowDialog();
			if (res == DialogResult.OK)
			{
				bkc = c1.Color;
				this.BackColor = bkc;

			   
				MessageBox.Show(Convert.ToString(bkc));
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			Form2 obj1 = new Form2();
			obj1.BackColor = bkc;
			obj1.Show();
		}




In Form 2
CODE
private void button2_Click(object sender, EventArgs e)
		{
			Form3 obj1 = new Form3();
			obj1.Show();
		}

		private void Form2_Load(object sender, EventArgs e)
		{
			this.BackColor = Form1.bkc;
					
		}




In Form3
CODE

private void button2_Click(object sender, EventArgs e)
		{
			Form1 obj1 = new Form1();
			obj1.Show();
		}

		private void Form3_Load(object sender, EventArgs e)
		{
			//Form1 obj2 = new Form1();
			this.BackColor = Form1.bkc;
		}




In the color dialog box I am selecting a color and pressing Ok button the color is also changed but when I restart the application I dont get the colour which I set using the Color Dialog.I want to retain this setting so that the user can get the desired color without resetting it each time the application is executed.

The above code does not generate any error.

can anybody help me out in performing this task?

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Retaining the changes made to the backcolor property

#2 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Retaining the changes made to the backcolor property

Posted 12 July 2009 - 02:10 AM

Variables don't retain their values when the application is closed. Your computer automatically frees up all the memory used by the closed application. So you have to save the value somewhere, few possibilities:
- Save the color info to a file or database
- Make a new class called Settings and add the backcolor variable there and serialize the class when your program is closed (or user presses save button) and deserialize it at startup.
Was This Post Helpful? 0
  • +
  • -

#3 s2009   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-June 09

Re: Retaining the changes made to the backcolor property

Posted 13 July 2009 - 12:00 AM

View Postjanne_panne, on 12 Jul, 2009 - 01:10 AM, said:

Variables don't retain their values when the application is closed. Your computer automatically frees up all the memory used by the closed application. So you have to save the value somewhere, few possibilities:
- Save the color info to a file or database
- Make a new class called Settings and add the backcolor variable there and serialize the class when your program is closed (or user presses save button) and deserialize it at startup.



Thanks for your reply.

I am trying to store the color in the database and retrieve it on form load event.

I have used the following code:

	   public ColorDialog c1 = new ColorDialog();
		public static System.Drawing.Color bkc;
		SqlConnection objSqlconnection = new SqlConnection();

		public string dbUpdate;
		public string connectionStringsetting = "Data Source = .;Initial Catalog = Sample; Integrated Security = SSPI";
		private void button1_Click(object sender, EventArgs e)
		{
			
			DialogResult res = c1.ShowDialog();
			if (res == DialogResult.OK)
			{
				bkc = c1.Color;
				this.BackColor = bkc;
				dbUpdate = bkc.
				
				objSqlconnection.ConnectionString = connectionStringsetting;
				objSqlconnection.Open();
				SqlCommand objSqlCommand = new SqlCommand("update Table_3 set ColourChoice = @Colour", objSqlconnection);
				SqlParameter objSqlParameter;
				objSqlParameter = objSqlCommand.Parameters.Add("Colour", SqlDbType.NVarChar);
				objSqlParameter.Value = dbUpdate;
				objSqlCommand.ExecuteNonQuery();
				objSqlconnection.Close();
			}

				
				MessageBox.Show(Convert.ToString(bkc));
				   }

 private void Form1_Load(object sender, EventArgs e)
		{
			objSqlconnection.ConnectionString = connectionStringsetting;
			objSqlconnection.Open();

			SqlCommand objsqlcommand = new SqlCommand("select ColourChoice from Table_3 where Id = 1", objSqlconnection);
			dbUpdate = objsqlcommand.ExecuteScalar().ToString();
		   
			this.BackColor = Color.FromName(dbUpdate);
			objSqlconnection.Close();
		}



Now When I select the color and see the table in the database it is stored it as :
color[Fuscia]

When I exit the application and re-run it I am not getting the color in the database rather it is showing the default grey color.

What should I do to retain this color and use it as the backcolor when the user starts the application?

Please help me!

Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Retaining the changes made to the backcolor property

Posted 13 July 2009 - 01:41 AM

Looks like there is something missing from this section of your code you pasted:

				bkc = c1.Color;
				this.BackColor = bkc;
				dbUpdate = bkc.
				



Probably ToString(), anyways:

Your program is probably failing because of the ToString() you are using when you convert color to string for database. You should use either ToKnownColor().ToString() or ToArgb().ToString(). I recommend ToArgb because not all colors have a known name.

I made a program of my own (coped most of your code) which changes the backcolor and saves it to a database and reads the database at the startup and it works.

Here is the code which should work:

	public partial class Form1 : Form {

		public ColorDialog c1 = new ColorDialog();
		public static System.Drawing.Color bkc;
		private SqlConnection objSqlconnection = new SqlConnection();

		public string connectionStringsetting =
			@"connectionstring here";


		public Form1() {
			InitializeComponent();
			objSqlconnection.ConnectionString = connectionStringsetting;
		}


		private void button1_Click(object sender, EventArgs e) {

			DialogResult res = c1.ShowDialog();
			if (res == DialogResult.OK) {
				bkc = c1.Color;
				this.BackColor = bkc;
							 
				if (objSqlconnection.State == ConnectionState.Closed)
					objSqlconnection.Open();
				SqlCommand objSqlCommand = new SqlCommand(
					"update Settings set BackColor = '" + bkc.ToArgb().ToString() + "'", 
					objSqlconnection);
				objSqlCommand.ExecuteNonQuery();
				objSqlconnection.Close();
			}
			MessageBox.Show(Convert.ToString(bkc.ToArgb().ToString()));
		}

		private void Form1_Load(object sender, EventArgs e) {
			objSqlconnection.Open();

			SqlCommand objsqlcommand = new SqlCommand("select BackColor from Settings", objSqlconnection);
			SqlDataReader vReader = objsqlcommand.ExecuteReader();

			string colorString = Color.Gray.ToKnownColor().ToString();
			
			if (vReader.HasRows) {
				vReader.Read();
				colorString = vReader[0].ToString();
			}
			vReader.Close();

			this.BackColor = Color.FromArgb(int.Parse(colorString));
			objSqlconnection.Close();

		}
	}


Was This Post Helpful? 0
  • +
  • -

#5 s2009   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-June 09

Re: Retaining the changes made to the backcolor property

Posted 13 July 2009 - 02:36 AM

View Postjanne_panne, on 13 Jul, 2009 - 12:41 AM, said:

Looks like there is something missing from this section of your code you pasted:

				bkc = c1.Color;
				this.BackColor = bkc;
				dbUpdate = bkc.
				



Probably ToString(), anyways:

Your program is probably failing because of the ToString() you are using when you convert color to string for database. You should use either ToKnownColor().ToString() or ToArgb().ToString(). I recommend ToArgb because not all colors have a known name.

I made a program of my own (coped most of your code) which changes the backcolor and saves it to a database and reads the database at the startup and it works.

Here is the code which should work:

	public partial class Form1 : Form {

		public ColorDialog c1 = new ColorDialog();
		public static System.Drawing.Color bkc;
		private SqlConnection objSqlconnection = new SqlConnection();

		public string connectionStringsetting =
			@"connectionstring here";


		public Form1() {
			InitializeComponent();
			objSqlconnection.ConnectionString = connectionStringsetting;
		}


		private void button1_Click(object sender, EventArgs e) {

			DialogResult res = c1.ShowDialog();
			if (res == DialogResult.OK) {
				bkc = c1.Color;
				this.BackColor = bkc;
							 
				if (objSqlconnection.State == ConnectionState.Closed)
					objSqlconnection.Open();
				SqlCommand objSqlCommand = new SqlCommand(
					"update Settings set BackColor = '" + bkc.ToArgb().ToString() + "'", 
					objSqlconnection);
				objSqlCommand.ExecuteNonQuery();
				objSqlconnection.Close();
			}
			MessageBox.Show(Convert.ToString(bkc.ToArgb().ToString()));
		}

		private void Form1_Load(object sender, EventArgs e) {
			objSqlconnection.Open();

			SqlCommand objsqlcommand = new SqlCommand("select BackColor from Settings", objSqlconnection);
			SqlDataReader vReader = objsqlcommand.ExecuteReader();

			string colorString = Color.Gray.ToKnownColor().ToString();
			
			if (vReader.HasRows) {
				vReader.Read();
				colorString = vReader[0].ToString();
			}
			vReader.Close();

			this.BackColor = Color.FromArgb(int.Parse(colorString));
			objSqlconnection.Close();

		}
	}



Thanks for your prompt reply.

The code given by you is generating the following error:

FormatException Was Unhandled
Input String was not in a correct format.

The error is pointed out towards this line of code:

this.BackColor = Color.FromArgb(int.Parse(colorString));

I am not able to identify the error can anybody help me out in knowing what exactly the error is?

How do I overcome it?

Please help me out!!
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retaining the changes made to the backcolor property

Posted 13 July 2009 - 02:55 AM

Take a look at this. It outlines using settings like you would in VB.NET, only in C#. Hope this helps :)

(Sorry it's 3am and my typing may be off a bit lol) :crazy:
Was This Post Helpful? 0
  • +
  • -

#7 s2009   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-June 09

Re: Retaining the changes made to the backcolor property

Posted 13 July 2009 - 03:08 AM

View PostPsychoCoder, on 13 Jul, 2009 - 01:55 AM, said:

Take a look at this. It outlines using settings like you would in VB.NET, only in C#. Hope this helps :)

(Sorry it's 3am and my typing may be off a bit lol) :crazy:


Thanks for your reply!

I had a look through the settings file but I think doing it in the way of storing it in the database would be more efficient.


So I am trying to do it by storing it in Database.

Can you help me out in storing and retrieving the color settings from the database.
Was This Post Helpful? 0
  • +
  • -

#8 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Retaining the changes made to the backcolor property

Posted 13 July 2009 - 03:38 AM

View Posts2009, on 13 Jul, 2009 - 01:36 AM, said:

The code given by you is generating the following error:

FormatException Was Unhandled
Input String was not in a correct format.

The error is pointed out towards this line of code:

this.BackColor = Color.FromArgb(int.Parse(colorString));


Add MessageBox.Show(colorString); before the line which gives the error to see what comes out from the database (or debug the application). If it's not integer which comes from the database, you should check the section of your code where you insert/update database.

Post your code here if you aren't sure how to correct it and also let us know what is the value colorString has.
Was This Post Helpful? 0
  • +
  • -

#9 s2009   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-June 09

Re: Retaining the changes made to the backcolor property

Posted 13 July 2009 - 03:44 AM

View Postjanne_panne, on 13 Jul, 2009 - 02:38 AM, said:

View Posts2009, on 13 Jul, 2009 - 01:36 AM, said:

The code given by you is generating the following error:

FormatException Was Unhandled
Input String was not in a correct format.

The error is pointed out towards this line of code:

this.BackColor = Color.FromArgb(int.Parse(colorString));


Add MessageBox.Show(colorString); before the line which gives the error to see what comes out from the database (or debug the application). If it's not integer which comes from the database, you should check the section of your code where you insert/update database.

Post your code here if you aren't sure how to correct it and also let us know what is the value colorString has.


Thanks for your reply

The messagebox shows the number ff808040.

Once I click on the OK button of the I get the error: Input string was not in a correct format.

What should I do now?

This post has been edited by s2009: 13 July 2009 - 03:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1