Answered by:
Is this possible? (Properties.Settings.Default)
-
I want to make a test on the Properties.Settings.Default in the middle of an array...
List<Button> buttons = new List<Button>();
for (int j = 0; j < hour; j++)
{
Button btn = new Button();
btn.Name = GenerateButtonName(day[i],j);
name = btn.Name.Substring(3);
if (Properties.Settings.Default.SettingsKey == name)
{
btn.BackColor = System.Drawing.Color.Green;
}
else
{
btn.BackColor = System.Drawing.Color.LightGray; ;
}
The buttons name will be like btnMon12 and im appending it to Mon12 (which is the name of a setting in the app.Config)
I want to test to make sure its the correct entry and if its true or false
Question
Answers
-
Hi,
you are checking the SettingsKey property. This property is there for supporting multiple AppSettings (one for each instance of the application).
If you like to check, if there is a specific entry inside the Properties collection, you can simple iterate trough the Properties-property with foreach:
bool found=false; foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) { if(currentProperty.Name == name) { found = true; } }
Best Regards,
Ilija Injac- Edited by Ilija Injac Thursday, November 06, 2008 5:17 AM typos
- Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, November 13, 2008 1:29 AM
-
Hello,
Please try not to run the application in Visual Studio Debugger. And please double click the application and press “Ctrl + F5” in Visual Studio IDE to run the application directly, then run again please, the settings value is actually changed. If we run the application via Visual Studio Debugger, the project default information and settings will be changed back to original default values. So it looks like you could only save the values during run time. Following are the testing codes:
-------------------------------------------------------------------
public Form1(){
InitializeComponent();
MessageBox.Show(Properties.Settings.Default.Setting1);
Properties.Settings.Default.Setting1 = "New Value";
Properties.Settings.Default.Save();
}
-------------------------------------------------------------------
Thanks,
Best Regards,
Lingzhi
Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6- Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, November 13, 2008 1:23 AM
All replies
-
Hi,
you are checking the SettingsKey property. This property is there for supporting multiple AppSettings (one for each instance of the application).
If you like to check, if there is a specific entry inside the Properties collection, you can simple iterate trough the Properties-property with foreach:
bool found=false; foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) { if(currentProperty.Name == name) { found = true; } }
Best Regards,
Ilija Injac- Edited by Ilija Injac Thursday, November 06, 2008 5:17 AM typos
- Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, November 13, 2008 1:29 AM
-
Edit: I fixed it just missed the quotes around the "True"
- Edited by project247 Thursday, November 06, 2008 6:50 AM
-
Now i got it working with this code:
foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties)
{
if (currentProperty.Name == name)
{
if (currentProperty.DefaultValue.Equals("True"))
{
btn.BackColor = System.Drawing.Color.Green;
}
else
{
btn.BackColor = System.Drawing.Color.LightGray;
}
}
}
It wont save, i have Properties.Settings.Default.Save(); on a button click
I modified my code for testing..private void btn_Click(object sender, EventArgs e) { string senderName; senderName = ((System.Windows.Forms.Button)sender).Name.Substring(3); if (((System.Windows.Forms.Button)sender).BackColor == System.Drawing.Color.Green) { ((System.Windows.Forms.Button)sender).BackColor = System.Drawing.Color.LightGray; foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) { if (currentProperty.Name == senderName) { currentProperty.DefaultValue = "False"; label1.Text = currentProperty.DefaultValue.ToString(); Properties.Settings.Default.Save(); } } } else { label8.Text = ((System.Windows.Forms.Button)sender).BackColor.ToString(); ((System.Windows.Forms.Button)sender).BackColor = System.Drawing.Color.Green; foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) { if (currentProperty.Name == senderName) { currentProperty.DefaultValue = "True"; label1.Text = currentProperty.DefaultValue.ToString(); Properties.Settings.Default.Save(); } } } }
The lable changes from true to false on click (which should mean the setting has changed, but if i close and open the form, the settings are lost- Edited by project247 Thursday, November 06, 2008 7:51 AM
-
-
-
ok it seems that this bit is wrong
foreach (SettingsProperty currentProperty in Properties.Settings.Default.Properties) { if (currentProperty.Name == senderName)
i can add pretty much anything to that and it wont error even though its a bool. Is there some other way to do this- Edited by project247 Friday, November 07, 2008 2:01 AM
-
I found that even though the settings look like this
Name | Type | Scope | Value
mon0 bool user True
this statement returns false
public Form1()
{
InitializeComponent();
CreateButton();
label1.Text = Properties.Settings.Default.Mon0.ToString();
}
however if i add "Properties.Settings.Default.Mon0 = true;" so it looks like this
public Form1()
{
InitializeComponent();
CreateButton();
Properties.Settings.Default.Mon0 = true;
label1.Text = Properties.Settings.Default.Mon0.ToString();
}
it will return true and the settings will still remain as
Name | Type | Scope | Value
mon0 bool user True
Whats going on? I tried to publish it and it still doesn't save the changes and draw the boxes properly
- Edited by project247 Friday, November 07, 2008 2:47 AM
-
Hello,
Please try not to run the application in Visual Studio Debugger. And please double click the application and press “Ctrl + F5” in Visual Studio IDE to run the application directly, then run again please, the settings value is actually changed. If we run the application via Visual Studio Debugger, the project default information and settings will be changed back to original default values. So it looks like you could only save the values during run time. Following are the testing codes:
-------------------------------------------------------------------
public Form1(){
InitializeComponent();
MessageBox.Show(Properties.Settings.Default.Setting1);
Properties.Settings.Default.Setting1 = "New Value";
Properties.Settings.Default.Save();
}
-------------------------------------------------------------------
Thanks,
Best Regards,
Lingzhi
Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6- Marked as answer by Michael Sun [MSFT]Microsoft employee, Moderator Thursday, November 13, 2008 1:23 AM