CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    9

    Saving srting problem

    Hi all,

    I am using visual studio in form mode where i have a textbox, a button and a label. When the user insert a value in the textbox and the button is pressed, the value will be saved in integer X. when the form is closed the value in integer X is saved and it is re-loaded in integer X when the form is loaded again. The value is then displayed in the label to confirm that the value is being saved.

    For some reason the label is not changing (i.e. stays as Label1), which indicates that the integer isn't being saved or re-loaded.

    Can someone please guide me where is the mistake please??

    Below is the saving code:
    Code:
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                string get = X.ToString();  //convert integer "X" to string "get"
    
                Properties.Settings.Default.test = get;   
                Properties.Settings.Default.Save();             
            }
    Below is the re-loading code:
    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                string get;
    
                get = Properties.Settings.Default.test;
                label1.Text = get;
            }
    I also included ' using testing.Properties; ' at the beginning of the coding.

    Attached is a settings screenshot.

    Any help would be appreciated.
    Thanks in advance.
    Attached Images Attached Images  
    Last edited by Dritech; January 14th, 2013 at 06:11 PM.

  2. #2
    Join Date
    Nov 2011
    Posts
    36

    Re: Saving srting problem

    Are you forgetting to set value X when they click the button? Something like:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                Int32.TryParse(txtBox.Text, out X);
            }
    What you have should work.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Saving srting problem

    If you're going to use TryParse, you should always check the result:L

    Code:
    if( !Int32.TryParse(txtBox.Text, out X) )
        MessageBox.Show("Hey!  That's not valid!");  //Some error message
    Otherwise just use

    Code:
    X = Int32.Parse(txtBox.Text);
    Which will throw an exception if you can't parse the string. Obviously, in code dealing with user-input the TryParse-and-check method is better than the exception handling method.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured