Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
I can store a color in custom color for particular instance.
As soon as the color dialog box closes and if it was opened again the selected color gets deleted.

How to get rid of this
Posted

By using the custom color dialog as a static property (in the main from for example) that is created only at its first invocation, then reused.
public Form MainForm
{
protected static ColorDialog _colorDialog = null;


protected void UsageOfColorSelection(...)
{

if(_colorDialog == null)
{
_colorDialog = new ColorDialog();
}

 if (_colorDialog.ShowDialog() == DialogResult.OK)
        textBox1.ForeColor =  colorDialog.Color;
//...


}
}
 
Share this answer
 
v2
Comments
KUMAR619 13-Mar-14 8:38am    
Can you explain with small code.
Raul Iloc 13-Mar-14 9:39am    
see my last update in 1st solution.
BillWoodruff 13-Mar-14 12:11pm    
This code will not make CustomColors persist when the Application is closed. And, there are other ways to make the CustomColors persist while the Application is running (already demonstrated to the OP on another question). There is absolutely no need for static code here, and it is a mistake to use it.
Raul Iloc 14-Mar-14 3:57am    
I provide a solution related to the question, and it was not related with the application closing. So there is no mistake in my solution!

If you want to provide help give a complete or better solution to the current question!
BillWoodruff 14-Mar-14 4:48am    
You can make defined CustomColors in the ColorDialog persist during any one Application session by following the simple instructions here, which I referenced in my solution below:

http://www.codeproject.com/Answers/743301/How-to-select-a-existing-color-in-color-dialog-b

No code is required ! See Solution #2 below.

Evidently you do not understand that when you put a ColorDialog Component on a Form using drag-drop from the ToolBox, that code is written into the Designer.cs file that creates an instance of the Component when the Application is launched. That instance persists during an Application session ... as long as you don't dispose of it.

Creating a static variable to hold the instance of the ColorDialog is absolutely unnecessary, and in no way assists the OP with their question.

You have a choice to make: be childishly angry and down-vote, or be mature enough to examine your knowledge and assumptions and learn. I voted your post #3 which is a neutral vote that will not affect your reputation score: why ? Because I felt your intent was to be helpful, even if the technical content of your post was completely off-target.
Actually, it is incorrect to say that Custom Colors defined in the way I showed you in this answer: [^] ... do not persist. They do persist, as long as your Application is running.

They are, of course, cleared when you quit your Application.

So, the question of how to save them, so they'll be available the next time you run your Application, is the same question you have to answer any time you want to save any Application Setting.

The CustomColors are available as a Property of the ColorDialog object: [^]. It's an array of 16 integers where each integer represents a Color. The default for each "slot" ... undefined ... is #16777215, which is White.

There are a variety of ways to approach saving Application settings, and restoring them. These include Application Settings, Serialization, and other methods.

I suggest you start here, and study Application.Settings: [^]. Once you get started, and know how to create an Application Setting, set it, and restore it, ask specific questions here.

Please remember that the goal, for many of us answering questions, here, is to help people become better programmers, to teach, to enable.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900