简体   繁体   中英

Need help retrieving application settings and changing the value

the application settings

Hello, I am having issue retrieving the value of a certain application setting then making it increment (++)

    public int orderIDnumber ;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        orderIDnumber = Properties.Settings.Default.OrderID;                            //Read the last order number from user settings
        orderIDnumber ++;                                                                 //Increase the order number by one ready to take an order
        ordernumLBL.Text = orderIDnumber.ToString();                                      //Display the order number in the label on screen after converting it to string
    }

  private void Newordernum()
    {
        orderIDnumber++;                                                                         //Increase order number by 1 (++ means increase by 1)
        ordernumLBL.Text = orderIDnumber.ToString();


    }

private void neworderBTN_Click(object sender, EventArgs e)
    {
        Saveorder();
        Clearlists();
        Clearboxes();
        Newordernum();



    }

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)          
    {                                                                             

        Properties.Settings.Default.OrderID = orderIDnumber;                    
        Properties.Settings.Default.Save();                                        
    }

Once I click a button I want it to update the label text but nothing is happening, seem the value is staying at 0 all the time.

You are reading the value of Properties.Settings.Default.OrderID into the field orderIDnumber . After that you are incrementing the field, leaving the original setting value unchanged.

You have to write the new number back into the setting eventually. Finally you need to call the save method to persist the new value:

Properties.Settings.Default.OrderID = orderIDnumber;
Properties.Settings.Default.Save();

https://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx

Saving User Settings at Run Time

Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method. These settings are saved in the User.config file.

To Write and Persist User Settings at Run Time Access the user setting and assign it a new value, as shown in the following example:

Properties.Settings.Default.myColor = Color.AliceBlue;

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:

Properties.Settings.Default.Save();

Is the property OrderID of type string?

In which case you might have to explicitly typecast it to int before you can increment it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM