CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2007
    Posts
    12

    [RESOLVED] Save data/Text so I can use it later.

    I think this is something simple but I can’t think of it right now.

    I run the following code on a button click,

    Label25.Text = My.Computer.Clock.Localtime

    I would like to save the text [LocalTime] and repopulate the Label25.text on form load.
    Where or how do I save that text to a location for reuse later on?


    Thanks,
    Robert J

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Save data/Text so I can use it later.

    For small bits of info, I'd recommend saving the info in the Registry
    Will that work for you Robert ¿

  3. #3
    Join Date
    Apr 2007
    Posts
    81

    Re: Save data/Text so I can use it later.

    Robert J,

    Another option:

    http://www.devx.com/vb/Article/34742

    Kerry Moorman

  4. #4
    Join Date
    Feb 2007
    Posts
    12

    Re: Save data/Text so I can use it later.

    I have looked through both links and maybe I am missing something here.

    Say I click the button on 6/10/2007 09:45:03 AM, this will show up in label25 but when I close the form and reopen it, I would like to have that still displayed until I press the button again instead of just the Label25 that is default in the text properties.

    Thanks and sorry I didn’t give an example when I first posted.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Save data/Text so I can use it later.

    Robert, wehn you close this form, do you navigate to another form, or does the closing of this form represent the closing of the Application ¿

  6. #6
    Join Date
    Feb 2007
    Posts
    12

    Re: Save data/Text so I can use it later.

    Closing the form shuts down the application.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Save data/Text so I can use it later.

    I thought so

    OK, here's a small example of how to save the value of your Label25 into the registry, and how to display that value again.
    Code:
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim strDate As String 'Variable to store value read from Registry
    
            'Determine whether or not the date Key actually exists
            If Not (Application.UserAppDataRegistry.GetValue("Date") Is Nothing) Then
                'If the date Key exists
                strDate = Application.UserAppDataRegistry.GetValue("Date").ToString() 'Store value read in strDate
                Label25.Text = strDate 'Assign the value to the Form
           Else
                Label25.Text = ""
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Label25.Text = System.DateTime.Now.ToString()
        End Sub
    
        Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            Application.UserAppDataRegistry.SetValue("Date", Label25.Text) 'Store Value in HKEY_CURRENT_USER\Software\ProgramName
        End Sub
    Let me explain what is going on here..
    In form Load, I tested to see whether or not the particular key exists in the registry ( the registry, as you probably know, is a "settings database", where all program settings are stored - for example, recently opened documents in MS Word).
    If the key exists, I did another test to see what that particular key contains, and then load that onto my Label.

    In the closing event of the form, I saved the date to the registry ( In that key ).
    In the button 1 click event, I just enabled the program to show a new date & time.

    The first time this app is run, you will see nothing on Label25. When you have clicked the button, it will show the date & time, when you close the form, that date & time gets stored. run the program again, and you will see the date & time you clicked earlier

    Does that better explain everything ¿

  8. #8
    Join Date
    Feb 2007
    Posts
    12

    Re: Save data/Text so I can use it later.

    HanneSThEGreaT,

    Works like a charm.
    I understand better but I couldn't do it from scratch right now if I had to if you know what I mean.

    Also, how or where do I mark this thread as resolved?

    Thanks Again,
    Robert J

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Save data/Text so I can use it later.

    You're welcome Robert, glad I could help

    To mark a thread Resolved, all yo uneed to do is to click Thread Tools just above your first post, then select Mark Thread Resolved

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