473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How can I save the last settings used by my application?

Tyler Wiebe
I'm wonder how to save the last used settings that my application used. For example:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     public partial class Form1 : Form
  3.     {
  4.         public static int alphaValue;
  5.         public static int redValue;
  6.         public static int greenValue;
  7.         public static int blueValue;
  8.  
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.             pictureBox1.BackColor = Color.FromArgb(alphaValue, redValue, greenValue, blueValue);
  13.             trackBar1.Maximum = 255;
  14.             trackBar1.Value = alphaValue;
  15.             trackBar2.Maximum = 255;
  16.             trackBar2.Value = redValue;
  17.             trackBar3.Maximum = 255;
  18.             trackBar3.Value = greenValue;
  19.             trackBar4.Maximum = 255;
  20.             trackBar4.Value = blueValue;
  21.         }
  22.  
  23.         private void trackBar1_Scroll(object sender, EventArgs e)
  24.         {
  25.             alphaValue = trackBar1.Value;
  26.             pictureBox1.BackColor = Color.FromArgb(alphaValue, redValue, greenValue, blueValue);
  27.         }
  28.  
  29.         private void trackBar2_Scroll(object sender, EventArgs e)
  30.         {
  31.             redValue = trackBar2.Value;
  32.             pictureBox1.BackColor = Color.FromArgb(alphaValue, redValue, greenValue, blueValue);
  33.         }
  34.  
  35.         private void trackBar3_Scroll(object sender, EventArgs e)
  36.         {
  37.             greenValue = trackBar3.Value;
  38.             pictureBox1.BackColor = Color.FromArgb(alphaValue, redValue, greenValue, blueValue);
  39.         }
  40.  
  41.         private void trackBar4_Scroll(object sender, EventArgs e)
  42.         {
  43.             blueValue = trackBar4.Value;
  44.             pictureBox1.BackColor = Color.FromArgb(alphaValue, redValue, greenValue, blueValue);
  45.         }
  46.     }
  47.  
  48.  
I'd like to know how to save the alphaValue, redValue, greenValue, and blueValue, so that one the next load, they would be set to the last used values.
Mar 9 '11 #1
6 2923
GaryTexmo
1,501 Expert 1GB
The easiest way is to use the C# settings stuff. Here's a link to the MSDN page: http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx

If you're feeling creative, you can write one yourself, it's actually fairly easy. In the past, I've done it with an XML file that looked something along the lines of:

Expand|Select|Wrap|Line Numbers
  1. <AppName_Settings>
  2.   <Item Name="Colour" Value="FFFFFFFF" />
  3.   <Item Name="LastFile" Value="blahblah.txt" />
  4.   ...
  5. </AppName_Settings>
You can just read this file into whatever structure you like and look up the settings by name.

It's pretty much what the C# one does anyway so if you're in a hurry you might want to just use that. The only reason I recommend doing it yourself the first time is to gain an understanding of how it works, and to advance your programming knowledge :)

Good luck!
Mar 9 '11 #2
Thank you for your reply, I actually feel rather stupid not seeing that, but it's not like I knew it was there, and what it did. I haven't taken a class on C#, I've just been teaching myself, with the help of some beginner classes of C++, HTML/JavaScript.
Mar 10 '11 #3
GaryTexmo
1,501 Expert 1GB
We all start somewhere :)

I wouldn't worry about it, MSDN is great but sometimes if you're not sure what you're looking for, you can get bogged down. I've been doing .NET development for a good 4-5 years now and I'm still finding out about slick little things in the library. I'd imagine that's not going to change either ;)
Mar 10 '11 #4
I'm currently having problems with this. It doesn't seem to save the settings file.

I've already done a few practice runs with this, and they work, but they were small tests. The one I'm doing now is a bit more complex. It's pretty much saving the last Argb colors that were used that were set by 3 track bars, the alpha always being 255.

I had it working with only saving the red when I started to make sure it was working, but I did some more, and added the other colors, and now it doesn't seem to same the numbers.

Do you have any ideas?
Mar 10 '11 #5
GaryTexmo
1,501 Expert 1GB
Can you post some sample code? I've never actually used the built in settings one, I typically opt to do it myself. I made a class to take care of it a while back and I generally just use that so I'm not well versed in this one.

That said, there's a few thing we can look at. Have you figured out where it saves the settings file? I feel like I heard it was in the user directory somewhere, but I could be wrong. Anyway, it should be a normal XML file so you should be able to open it up and find out what it's writing.

Is it still saving the red, or is it not saving anything at all now? When this happens to be I try to go back and try the simple case again. Also check where you're reading/writing to the settings and make sure (by stepping through the code) that what you're expecting is going in and coming out.
Mar 10 '11 #6
Thank you for your help, but as it turns out, I just solved it. It was my fault, turns out it was the execution order of the code, my track bars were setting the value of the settings before my track bars value (trackBar1.Value) got set to the settings value, which, being track bars, are always set to zero at the beginning, which in turn, set the settings to zero, so it was like they never got saved.

Thanks again for your help, it was much appreciated.
Mar 10 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Sam Carleton | last post by:
It is my understanding that in .Net the registry is not the prefered place to save application settings. What has Microsoft put in place to replace it? I thought it was the .config file, but I...
10
by: Dica | last post by:
i've got an app that needs to connect to sql server and login with a useName/password. currently, my app saves this info in an XML file in plain text (i.e. no encryption). everytime the app is...
3
by: Damian | last post by:
Hi everyone I'm having an issue with one of our applications. I'm getting the following error when attempting to log in to the site: Server Error in 'xxxxxxxxxxxxxxxx' Application....
8
by: Eric bouxirot | last post by:
hi there, i never used XML beforre, i prefere INI files, but i want do same things with XML file format.. before i can save/read some element of INI files without the need to parse or read...
3
by: DaBrain | last post by:
I have a program that uses settings. When I save my settings they are saved: Properties.Settings.Default.Save(); when I restart the application, my settings are present, the changes are in...
4
by: Andrus | last post by:
I have WinForms MDI application. MDI child forms are created by menustrip click enent handler AppDesktop.FormMgr.Show(new ChildForm1("param1", "param2") ); Every MDI child form "childform1"...
17
by: Lazareth | last post by:
Hi I have a program that sends an email to selected recipients. I have two forms. One that creates and send the email and another that is used to select recipients and other information. ...
3
Atran
by: Atran | last post by:
I want to save my settings after I close my form. example: when I write in a textbox a word and I close the form and open it again see the word I was wrote it. example2: when my form runnig, I set...
4
by: tony curtis | last post by:
I have a list of order numbers and want to increase the last number used by 1 and store the latest number in a table. Autonumber does not work for me as all the data is in a table already. New...
1
by: Tony | last post by:
Hello! I use VS2005 and within folder properties there is a file called Settings.setting. I can remove this file and rebuild the application without any kind of error or problems. So my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.