My Profile
Gifts
Active Members
TodayLast 7 Days
more...
|
Save the desktop application settings for subsequents sessions
Posted Date: 27 Jul 2008 Resource Type: Articles Category: General
|
Posted By: Bejaoui Bechir Member Level: Silver Rating: Points: 20
|
Introduction:
The application settings are simply the several properties’ settings recording to the application controls, in other words, if we assume for e.g. that there is a text box in the form and the user make change its back color property from white to red, we can say that the user sets the text box back color property to red, and this is one of the application settings. You tell me, Ok understood, but where is the problem here? Before responding to the question, what if the given user close the application and reopen it again? what happens? the given text box back color turn back to it's first property, I mean white back ground color.In order to resolve this problem I invite you to follow those steps.
Walkthrough:
1. Create a new windows application project 2. As a form called Form1 is now in the design editor, add two text boxes to this form and name them respectively FirstTextBox and SecondTextBox 3. Add two labels to the form and set their text properties, respectively, First text box and second text box then put each label above it corresponding text box 4. Add a new class to the project and name it "cApplicationSettings" Remarque: The targeted properties these will be used for the current demonstration are ForeColor, BackColor and Font corresponding to the first text box only, the purpose here is to demonstrate the difference between the first text box and the second one. 5. include System.Configuration and System.Drawing in the class code editor 6. Copy and paste this below code:
class cApplicationSettings : ApplicationSettingsBase { [UserScopedSetting] public Color TextBox1FontColor { get { return (Color)this["TextBox1FontColor"]; } set { this["TextBox1FontColor"] = value; } } [UserScopedSetting] public Color TextBox1BackColor { get { return (Color)this["TextBox1BackColor"]; } set { this["TextBox1BackColor"] = value; } } [UserScopedSetting] public Font TextBox1Font { get { return (Font)this["TextBox1Font"]; } set { this["TextBox1Font"] = value; } } }
7. Open the Form1 code editor and add those lines of code:
/* Define a new cApplicationSettings object */ cApplicationSettings mySettings; // Implement the Form1_Load handler private void Form1_Load(object sender, EventArgs e){ // Create a new instance of mySettings object mySettings = new cApplicationSettings(); // Add a new font Binding corresponding to the first text box Binding FontTextBox = new Binding("Font", mySettings, "TextBox1FontColor", true, DataSourceUpdateMode.OnPropertyChanged); // Add it to the FirstTextBox DataBindings FirstTextBox.DataBindings.Add(FontTextBox); // Add a new font color Binding corresponding to the first text box Binding FontColorTextBox = new Binding("ForeColor", mySettings, "TextBox1FontColor", true, DataSourceUpdateMode.OnPropertyChanged); // Add it to the FirstTextBox DataBindings FirstTextBox.DataBindings.Add(FontColorTextBox); // Add a new back color Binding corresponding to the first text box Binding BackColorTextBox = new Binding("BackColor", mySettings, "TextBox1BackColor", true, DataSourceUpdateMode.OnPropertyChanged); // Add it to the FirstTextBox DataBindings FirstTextBox.DataBindings.Add(BackColorTextBox); // Create a new event handler for this.FormClosing event this.FormClosing+=new FormClosingEventHandler(Form1_FormClosing); }
8. implement the Form1_FormClosing(object sender, ..Eventargs e){} stab using this code:
mySettings.Save();
Now, In order to allow the user change the given properties corresponding to the both text boxes proceed as follow:
1. In toolbox, select Context Menu strip, drag and drop it on the Form1 2. Add Font, Font color and Back color to the context menu items 3. Clone the Context menu by coping and pasting it, contextMenuStrip2 is created 4. Set the ContextMenuStrip properties according to the both text boxes respectively ContextMenuStrip1 for FirstTextbox and ContextMenuStrip2 for the SecondTextbox 5.Implement the both context menu strip elements with the following codes:
private void fontToolStripMenuItem_Click(object sender, EventArgs e) { FontDialog oFontDialog = new FontDialog(); oFontDialog.ShowDialog(); FirstTextBox.Font = oFontDialog.Font; } private void fontColorToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog oColorDialog = new ColorDialog(); oColorDialog.ShowDialog(); FirstTextBox.ForeColor = oColorDialog.Color; } private void backColorToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog oColorDialog = new ColorDialog(); oColorDialog.ShowDialog(); FirstTextBox.BackColor = oColorDialog.Color; } private void toolStripMenuItem1_Click(object sender, EventArgs e) { FontDialog oFontDialog = new FontDialog(); oFontDialog.ShowDialog(); SecondTextBox.Font = oFontDialog.Font; } private void toolStripMenuItem2_Click(object sender, EventArgs e) { ColorDialog oColorDialog = new ColorDialog(); oColorDialog.ShowDialog(); SecondTextBox.ForeColor = oColorDialog.Color; } private void toolStripMenuItem3_Click(object sender, EventArgs e) { ColorDialog oColorDialog = new ColorDialog(); oColorDialog.ShowDialog(); SecondTextBox.BackColor = oColorDialog.Color; }
6. Now, run the application and write something in the both text boxes, the Form 1 7. Change the settings of both text boxes 8. Finally, close and reopen the application then observe.
Good dotneting!!!
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
|