Save back color - c#

How can I change and save the back color in a C# Windows Application so that when I
close the application and run the program again the new color will be the
back color default?

You can get that going with very little effort. Select the form in the designer, in the Properties window open the ApplicationSettings node. Select (PropertyBinding) and click the button. Select BackColor in the popup dialog. Click the dropdown arrow and click New. Set the name to, say, "FormBackColor".
The only other thing you need is an option to let the user pick another color. Very easy to do with the ColorDialog class:
private void OptionChangeColor_Click(object sender, EventArgs e) {
using (var dlg = new ColorDialog()) {
if (dlg.ShowDialog() == DialogResult.OK) {
this.BackColor = Properties.Settings.Default.FormBackColor = dlg.Color;
Properties.Settings.Default.Save();
}
}
}

You'll need to save the new color in some file that you load on startup and apply as the background color.
Or use a user setting like this.

You could do something simple like File.WriteAllText("bg.txt", this.BackColor.ToString()); and when the app loads do this.BackColor = Color.FromName(File.ReadAllText("bg.txt"));
Of course storing this color in Isolated Storage or in the registry might be better. But you get the idea...

Some time ago there was thread about best practices to do that here on stackoverflow. Please take look: Best practice to save application settings in a Windows Forms Application

Related

Closing a form that was opened by ShowDialog()

I have a winform application where I've written my own little color picker that will only display system colours. It's basically a TableLayoutPanel with a bunch of smaller panels added to it, to which I just set the background color.
Pretty simple:
Now I'm opening this form for with:
using (frmColourWindow colourPicker = new frmColourWindow (Cursor.Position.X, Cursor.Position.Y, findingPriority))
{
colourPicker.ShowDialog();
if (!colourPicker.SelectedColor.IsEmpty)
{
pnlColor.BackColor = colourPicker._SelectedColor;
}
}
and closing it with by setting the DialogResult when the user has clicked on one of the color panels.
This all works pretty good, the only thing I can not manage to get right is by closing the form when it loses focus (E.g. when the user clicks somewhere else or starts typing). I've had a look at the Deactivate, LostFocus, and Leave events. Just can't seem to get those events to fire when I want them to. Maybe I'm missing something obvious?
As I mentioned in the comments, when using the ShowDialog() you can only use the Dialog you have opened and thus it never looses focus, so event like Deactivate, LostFocus and Leave won't work.
You need to use the Show() command to use those event to close the opened Form.
As to addressing the issue you pointed out in the comments about assigning the color to the object. you can do the following:
Declare a public Property
Color SelectedColor {get; private set; }
In your color picker and change your using statement to this:
var colourPicker = new frmColourWindow (Cursor.Position.X, Cursor.Position.Y, findingPriority);
colourPicker.Closed += (o, args) => { pnlColor.BackColor = colourPicker.SelectedColor };
colourPicker.Show();
This is of course just one of many possible solutions for that.
You can achieve this by displaying the form with the Show() method and then using the Form.Deactivate event.

How to add pop-box alike? c# vs2013

I would like to improve my application's interface. Is it possible to add something like this(refer to image below)?:
When you click the name or move the cursor to the name, a pop up box will appear with user's main info. If possible, can you teach me the appropriate tools to use. I don't mind if it is basic tools, so i can make a little similar to the image.
BTW, i'm using visual studio 2013.
Here is an (absolutely minimal) example of a pop-up Panel that takes care of the showing and hiding:
Panel popPanel = new Panel();
private void linkLabel1_MouseEnter(object sender, EventArgs e)
{
popPanel.Parent = linkLabel1.Parent;
popPanel.Location = new Point(linkLabel1.Left - 20, linkLabel1.Top + 10);
//popPanel.displayData(someDataClassFromSender);
popPanel.Show();
popPanel.MouseLeave += (ss, ee) => { popPanel.Hide(); };
}
For the layout and display of the info on the Panel (or some other control) you should create a class PopUpPanel and give it a method to load the data it shall display..
If it is a web application go check out http://bootsnipp.com/snippets/featured/fancy-navbar-login-sign-in-form
Just change the event from click to hover.
And get youserlf a copy of bootstrap http://getbootstrap.com/

Making it so I can close a pic once opened

I have C# code linked to an xaml file that builds a GUI. In the GUI, when I click on an option, the photo appears, as I want. However, I can't figure out how to make it so the user has the option to close the photo. Here's my C# code:
public void help_click(object sender, RoutedEventArgs e)
{
Image img;
img = new Image();
Uri diagram = new Uri(#"pack://application:,,,/PTDGUI;component/Content/Icons/controlmap.png", UriKind.RelativeOrAbsolute);
img.Source = new BitmapImage(diagram);
canvasSpace.Children.Add(img);
}
Image is not based on Control. You may require to customize for this behavior by either writing custom control or trapping any other control events to close this image.
This discsussion(stackoverflow) explains both the options I've mentioned, should help in understand and implementing what you rquired.
you can use the same help button for both, initially button will display "Help", When user clicks help, load the image and change the button text to "Close" and toggle it back to "Help" when users clicks again.
if(button.Text ="Help")
{
---load image
button.Text = "Close";
}
else
{
--Clear image
button.Text ="Help"
}
you may also use bool flag instead of checking text.

Need to use a color dialog result in a separate Windows Form

Please go easy on me I am very new to coding and stuck on something that I'm sure is very simple.
Currently I have an Options form in which the user can select from the standard colordialog.showdialog() and the result is displayed as a sample on the form as a label
If(backColorDialog.Showdialog() == DialogResult.OK);
backColorLabel.BackColor = backColorDialog.Color; // set to label to show option selected
I need to take that selected color and apply it to the background color of a "game board" in another Windows form. I have already added a reference from the "game board" form to the options form.
The "Game Board" is laid over a TableLayoutPanel so I need to be able to change the BackColor of the panel
TableLayoutPanel1.BackColor = (colordialog result from options form)
Like I said I am new to this and appreciate any help you can provide
Create a property on your configuration form:
Color SelectedColor { get; private set; }
And assign users choice to it:
if (backColorDialog.Showdialog() == DialogResult.OK)
{
backColorLabel.BackColor = backColorDialog.Color;
SelectedColor = backColorDialog.Color;
}
(note there was a mistake in your code - the ; after if line)
After this, in your main form you can read it from your configuration form:
TableLayoutPanel1.BackColor = optionsForm.SelectedColor;
As noted by justin.chmura in the comments below, you can also consider a different approach using events. Your options form would raise an event when user selects a different color, and main form's event handler would apply this color instantly on the game panel. The difference is that it would update the color immediately. When it's not needed the above solution with a property is enough.

Retaining the changes made to the backcolor property

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!
You will need to save the value somewhere such as the Application.exe.config:
// Open App.Config of executable
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("BackgroundColour",
bkc + " ");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
Here is a C# full code example: Using System.Configuration.ConfigurationManager Example
The suggestion of using the application configuration file is close, but there are two things wrong with it.
First, all users of the application share the same application configuration file. If you have multiple users (on a network, say, or different users on the same machine), storing a user's preference in the application configuration file will change that setting for all users. A second thing wrong with it is that under a default installation on Vista it won't work anyway: by default, Vista doesn't give the user write access to anything under the Program Files directory, so saving changes to the application configuration file will throw an exception.
The right answer is to use user settings. These get stored in the application's user settings file, which lives in a (deeply nested, and OS-version-dependent) subdirectory of the user's home directory. The ConfigurationManager loads these settings at runtime, and lets you update and save them in your code. There's an entire infrastructure built into Visual Studio to make this (relatively) easy, which is good, because doing it properly involves writing a spooky amount of code against the ConfigurationManager class. Here's how it works:
If you look under the Properties of your VS project, you'll see an item called Settings.settings. When you double-click on this, it will show you a grid that lets you add settings to your project. You give the setting name, choose its the data type and default value, and, crucially, the scope. The setting can be application scope, in which case its value will be common to all users of the application and be stored in the application configuration file. Or it can be user scope, in which case each user can have his own value for the setting, and the setting will live in the user settings file.
When you add a setting to this grid, VS generates code to make the setting available to your code. Basically, it creates a class that exposes these settings to your code as properties of a singleton object. (You can see this code if you want to get an idea of what this is saving you from having to do yourself; it gets stored in the 'Settings.Designer.cs' file created under 'Settings.settings' in the project view.) It also, conveniently, regenerates this class every time you change the information in the Settings grid. Once you create a setting in the settings grid, you can reference it in your code thusly:
ctl.BackColor = Properties.Settings.Default.BackColor;
User settings can be modified by your code:
Properties.Settings.Default.BackColor = newBackColor;
And you can save them to the user settings file like this:
Properties.Settings.Default.Save();
Having these settings being exposed as properties of a class is useful for a lot of reasons. One of the most important is that since they're properties (and not, say, dictionary entries accessed by a key, which is how most code that people write against the ConfigurationManager class works), there's compile-time checking of the names you're using in code. You're not ever going to get a NullReferenceException at runtime if you misspell the name of a setting; you'll get an error when you compile it instead.
There are a few subtleties to using user settings. One of the less obvious ones is: what happens when you produce a new release of the software? The user settings are stored in a directory that's keyed to the version number of the program; if you release a new version, the user settings file for it won't exist. How do you keep the user from losing all of his settings when he upgrades your program?
This is also built in to that Settings class; all you need to do is this:
if (Properties.Settings.Default.UpgradeSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeSettings = false;
}
This will copy the user's settings from the previous release into the settings file for the new release.
Why dont you create an event that all three forms listen to and get them to change the background colour when listening to the "change colour" event? And you could store the colour in a static variable so that when the form gets loaded, the background colour could be set to that stored in the variable.
In order for the screen to remember the colour settings, why not store the colour selected in a user preferences file? Try the "IsolatedStorage" functionality to save a preferences file.
You are doing it wrong way.
How will the application remember the user choice of backcolor?
The app runs in memory & shows chosen backcolor till its terminated.
Read on this & take it forward.
EDIT: Also, it is not right thing to use Form1.BackColor in Form2.
Open Form1, change backcolor, close Form1 & open Form2 to see what happens (you might see that Form1 opens again).

Categories