Jump to content
  • Advertisement
Sign in to follow this  
bigjoe11a

Application Settings in C Sharp

This topic is 3887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have set up a form that has 2 options in it. 1) mtpServer 2) SmtpPort How can I read them in and change them and then save them back to the Application Settings file. PLEASE make it as simple as possible so that I can under stand.

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure how much simpler it can get than that page.

I'm guessing your smtpServer and smtpPort variables are being obtained from TextBox components? You can get the value of them by using the Text or Value property, depending on what type of component they are.

After you get the values, simply set the settings values and save them using the Settings class.

Properties.Settings.Default.smtpServer = TextBox1.Value
Properties.Settings.Default.smtpPort = TextBox2.Value
Properties.Settings.Default.Save();

Obviously you change the variable names to match your own.

Share this post


Link to post
Share on other sites
Quote:
Original post by Justin Rebuilt
I'm not sure how much simpler it can get than that page.

I'm guessing your smtpServer and smtpPort variables are being obtained from TextBox components? You can get the value of them by using the Text or Value property, depending on what type of component they are.

After you get the values, simply set the settings values and save them using the Settings class.

Properties.Settings.Default.smtpServer = TextBox1.Value
Properties.Settings.Default.smtpPort = TextBox2.Value
Properties.Settings.Default.Save();

Obviously you change the variable names to match your own.


Well This is what I have. and well that part I do under stand.
Heres my settings. This in what I have for the save.

Properties.Settings.Settings.SmtpServer = TextBox1.Value1
Properties.Settings.Settings.SmtpPort = TextBox2.Value2
Properties.Settings.Settings.Save();


What I'm trying to point out is that how do I load the current setting in to be displayed on the screen or form. I have form3 that displays or should display the current setting and then the user has an option of changing one or the other or even both.

I Have TextBox1.Text = SmtpServer; // For the SmtpServer Address
and I have TextBox2.Text = SmtpPort; // For the Port number
I just want to display the current setting.
And it tells me that they are not definded.

Then I can use

if (TextBox1.Text <> "")
{
Properties.Settings.Settings.SmtpServer = TextBox1.Value1
}
This will tell if the Value1 is blank or not and if the field is not bank then it will update it.
and then I can save it.









Share this post


Link to post
Share on other sites
The compiler is telling you what's wrong. You haven't defined SmtpServer and SmtpPort. If you're trying to retrieve them from settings:

Example:
int SmtpPort = Properties.Settings.Default.SmtpPort;

Share this post


Link to post
Share on other sites
Quote:
Original post by Justin Rebuilt
The compiler is telling you what's wrong. You haven't defined SmtpServer and SmtpPort. If you're trying to retrieve them from settings:

Example:
int SmtpPort = Properties.Settings.Default.SmtpPort;


Thank You I was missing the Default part. One more thing

On this line

Properties.Settings.Default.SmtpPort = Value2.Text;

Witch is an INT for the port being used. I get this error. and Yes I know what this error is.

Error 1 Cannot implicitly convert type 'string' to 'int'

How Can I process it as a string.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheUnbeliever
Use something like

if (!int.TryParse(Value2.Text, out Properties.Settings.Default.SmtpPort))
{
// Conversion failed - do something sensible like warn the user.
}


Thank You now I have a new error
Error 1 A property or indexer may not be passed as an out or ref

So Heres my code. All of it.

public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
// Load Current setting for Smtp Settings
String SmtpServer = Properties.Settings.Default.SmtpServer;
int SmtpPort = Properties.Settings.Default.SmtpPort;

label2.Text = SmtpServer;
//label3.Text = SmtpPort; // I need to display the INT in the label3

}

private void button2_Click(object sender, EventArgs e)
{
// User Aborts Save
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
// User Updates and saves New Setting;;

if (Value1.Text != "")
{

Properties.Settings.Default.SmtpServer = Value1.Text;
}
if (Value2.Text != "")
{
if (!int.TryParse(Value2.Text, out Properties.Settings.Default.SmtpPort))
{
// Conversion failed - do something sensible like warn the user.
// I put a Message Box here
}

}

// Save The enties if any
Properties.Settings.Default.Save();
this.Close();

}
}

Share this post


Link to post
Share on other sites
Sorry, that's my fault for not checking. Properties.Settings.Default.SmtpPort is a property (a single (or pair of) function(s) that act(s) like a field (a member variable), and you can't pass that in as a out parameter because it's not really a field so there's nothing 'real' to pass as a reference.

int smtpPort = Properties.Settings.Default.SmtpPort;
if (!int.TryParse(Value2.Text, out smtpPort))
{
// Conversion failed - do something sensible like warn the user.
}
Properties.Settings.Default.SmtpPort = smtpPort;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!