Reputation: 4611
I have a problem in showing the colordialog when the form is closed.Can we save the custom color selection in the colordialog in VB.NET?
Upvotes: 2
Views: 2348
Reputation: 408
Since this question is tagged VB.NET 2010 I'll provide a compatible VB.NET answer.
If the user adds custom colors while working with a ColorDialog
, you can access these colors using the CustomColors
property. It returns their colors as an Integer()
.
My.Settings
The most convenient place to store these custom colors might be in My.Settings
, it gives you an easy place to store settings on a per user basis, if that's what you are looking for.
If you were to attempt to add an Integer()
typed setting using the GUI, you'll find that it won't work, Visual Studio doesn't support that.
Luckily, you can still make this work by manually editing the Settings.settings file.
(Thanks to Jen-Ari for this related useful answer.)
String
typed setting named CustomColors
, we'll change the type later.The contents of the file will look like this:
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="My" GeneratedClassName="MySettings" UseMySettingsClassName="true">
<Profiles />
<Settings>
<Setting Name="CustomColors" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
Change Type="System.String"
to Type="System.Int32[]"
, so you have this:
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="My" GeneratedClassName="MySettings" UseMySettingsClassName="true">
<Profiles />
<Settings>
<Setting Name="CustomColors" Type="System.Int32[]" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
Here's some sample code showing how you could use this technique:
Public Class Form1
Private Sub btnChooseColor_Click(sender As Object, e As EventArgs) Handles btnChooseColor.Click
'I'm assuming that dlgColorDialog has been placed using the Forms designer.
dlgColorDialog.ShowDialog()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Load the custom colors from My.Settings into the dialog when the form loads.
dlgColorDialog.CustomColors = My.Settings.CustomColors
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
'Save the custom colors when the form closes.
My.Settings.CustomColors = dlgColorDialog.CustomColors
My.Settings.Save()
End Sub
End Class
Upvotes: 2
Reputation: 6698
You can get and set the custom colors with the CustomColors
property. This is an array of int
, where the color format is 00BBGGRR
. B
is blue, G
is green and R
is red. You can convert a .Net Color to this format:
Color myColor = Color.Green;
int ColorAsBGR = (((myColor.B << 16) | (myColor.G << 8)) | myColor.R);
dlgColor.CustomColors = new int[] { ColorAsBGR };
or without using .Net colors:
// Get the colors
int[] customColors = dlgColor.CustomColors;
// Set the custom colors
dlgColor.CustomColors = customColors;
You would have to store and retrieve each custom color in an array of int and set the CustomColors property with it.
Upvotes: 4