Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to save Users and the Application settings.

I also read this well documented article, but still no luck:

I created the Settings named as Language and Country. Both are Strings.



As a next step, I try to Load the Settings at Runtime. Then here, I have a problem. When I add the using statement "using Options.Properties;" to my code then not use in it. Not highlighted, but anywhere in the code seen the settings if try to type something with.

C#
Properties.Settings.Default.language = textBox1.Text;
Properties.Settings.Default.language = comboBox1.SelectedItem;


But I have an error with TextBox and ComboBox as: An object reference is required for the non-static field, method, or property.

What have I missed and how can it be fixed?

What I have tried:

Here some screenshots:

Settings are working

Settings are not working
Posted
Updated 6-Nov-23 6:51am
v3
Comments
[no name] 5-Nov-23 12:14pm    
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/using-application-settings-and-user-settings?view=netframeworkdesktop-4.8
ernteSKY 5-Nov-23 12:26pm    
I also seen this too... But in a simple Form App the Visual Studio asking for using statement if type Settings or hover with mouse the code of Properties.Settings.Default... Then replace with Settings.Default... and start using that using statement where storing the data. What about is it an DLL?! I think is same but I cant set to using my using using statement.
[no name] 5-Nov-23 12:50pm    
"If you use Visual Studio, you can define settings within the Windows Forms Designer using the (ApplicationSettings) property in the Properties window."

https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/application-settings-overview?view=netframeworkdesktop-4.8

"If you want more detailed control over your settings, you can define your own custom applications settings wrapper class. This is accomplished by deriving a class from ApplicationSettingsBase, adding a property that corresponds to each setting, and applying special attributes to these properties. For details about creating wrapper classes, see Application Settings Architecture."

 
Share this answer
 
Comments
Maciej Los 5-Nov-23 13:21pm    
5ed!
Richard MacCutchan 6-Nov-23 3:44am    
Thanks. But I do wonder why so many developers cannot find their way to the documentation.
Maciej Los 6-Nov-23 10:25am    
The question is not "why they cannot find their way" by "why they don't try to find it".
Richard MacCutchan 6-Nov-23 10:54am    
You are correct.
ernteSKY 6-Nov-23 11:08am    
This does not cover the whole reality! I made that example solution and it doesn't work! Can't you ask for help or what's wrong with you?!
Your error message is:
Quote:
An object reference is required for the non-static field, method, or property


It means that you're trying to access an object in non-static method. For example, if your method is:
C#
private void ReadData(..){...}

change it to:
C#
private static void ReadData(..){...}


More at: Compiler Error CS0120 - C# | Microsoft Learn[^]
 
Share this answer
 
Comments
ernteSKY 6-Nov-23 12:53pm    
Yeah I already tried... I made some screenshots! You can find under "What I have tried" sections...
Maciej Los 6-Nov-23 13:11pm    
It seems you're trying to set the properties to object whose instance does not exist (Form_Settings).
ernteSKY 6-Nov-23 13:16pm    
Is there but not seen and don't know why... Try to delete the whole class project and try again? The namespace is all the same... I already created before this Form_Settings in a different project but inside in the same solution. After I deleted... That can be made some error?
Maciej Los 6-Nov-23 13:30pm    
Sorry, i don't understand you...
What i'm trying to tell you, is:
Form_Settings fs = new Form_Settings();
fs.ComboBox_Language.SelectedItem = Properties....

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