Results 1 to 6 of 6

Thread: Saving an edited Windows Form GUI

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    88

    Saving an edited Windows Form GUI

    Hi guys,

    How do I save a form which programmatically adds objects onto the GUI, when after I close I can get it to its latest form?

    So here's an example code:

    vb.net Code:
    1. Private Sub Form1_Click(sender As Object, e As MouseEventArgs) Handles MyBase.Click
    2.         Dim newTB As New TextBox
    3.         newTB.Name = "tbNew" & objectcounter
    4.         'Set location, size and so on if you like
    5.         Me.Controls.Add(newTB)
    6.  
    7.         Dim x = e.Location.X
    8.         Dim y = e.Location.Y
    9.  
    10.         newTB.Location = New Point(x, y)
    11.     End Sub

    So after adding a few textboxes on the form, and after exiting, I'd like to be able to save its latest form and continue from there.

    Is this possible?

    Thanks
    Vizier87

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,117

    Re: Saving an edited Windows Form GUI

    No it's not possible, from the point of view of the form class that is defined in your application. Whatever you create in the designer and the code window before you build is the class so that's what gets created when you instantiate that class. Anything else has to be done in code after that. Just like anything else, if you want to "save" actions that the user takes in your application then you need to save that to a file or a database or the like. If you want the user to be able to add TextBoxes to your form at run time then you might store the Bounds of each of those TextBoxes to a text file. At startup, you read that text file and create a TextBox for each value with the Bounds based on that value. The specifics are up to you but that's the principle that you need to implement.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    88

    Re: Saving an edited Windows Form GUI

    Quote Originally Posted by jmcilhinney View Post
    Just like anything else, if you want to "save" actions that the user takes in your application then you need to save that to a file or a database or the like. If you want the user to be able to add TextBoxes to your form at run time then you might store the Bounds of each of those TextBoxes to a text file. At startup, you read that text file and create a TextBox for each value with the Bounds based on that value.
    Sounds like quite a task.

    So if I understood you correctly, here's the principle:

    1. Save the "parameters" in a text file
    2. Run the form to read the "parameters" (let's say a textbox, length of the object, position, etc)
    3. Done.

    I've been googling around for something to achieve this but haven't found any (my search was "create objects from text file bounds vb.net").

    Any keywords or links that may be of help?

    I'd like to just be able to create the objects just by running from a saved text file with the "params" as you mentioned.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,117

    Re: Saving an edited Windows Form GUI

    Quote Originally Posted by Vizier87 View Post
    Sounds like quite a task.
    Not really.
    Quote Originally Posted by Vizier87 View Post
    So if I understood you correctly, here's the principle:

    1. Save the "parameters" in a text file
    2. Run the form to read the "parameters" (let's say a textbox, length of the object, position, etc)
    3. Done.
    Basically, yes.
    Quote Originally Posted by Vizier87 View Post
    I've been googling around for something to achieve this but haven't found any (my search was "create objects from text file bounds vb.net").

    Any keywords or links that may be of help?

    I'd like to just be able to create the objects just by running from a saved text file with the "params" as you mentioned.
    Stop expecting magic. Break the problem down into parts and tackle each part individually. Creating a control in code is the same regardless, so first learn how to do that. Once you can do that, you will know what parameters you'll need. You can then write a method that takes those values as arguments and then you can call that method with the appropriate arguments no matter where the values come from. Boom! that part is done.

    Now, completely separately, you can work out how to store the required values in a file, how read that data from a file and how to convert it to the required form, e.g. read text and convert it to numeric size and location values that you can then pass into the method you wrote earlier.

    This is how you need to approach EVERY programming problem: divide and conquer. Don't expect to find an "example" of the exact end-to-end process that you're trying to implement. Instead, break that process down into its constituent parts, learn what you can about each of those and try implementing each one separately. You can then ask a question about one specific part that you're having trouble with to solve that specific problem. Once you have solved all the subproblems, you simply combine the solutions into one and you've inherently solved the original problem. The is problem-solving 101 and is not specific to programming.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    88

    Re: Saving an edited Windows Form GUI

    Thanks. You got me thinking again and yeah I have to admit I was lazy to think of this really quite a simple thing actually.

    So I just loaded a saved text file and voila upon this stone I'll build my throne.

    vb.net Code:
    1. Dim fileReader As String
    2.         fileReader = My.Computer.FileSystem.ReadAllText("bla bla bla \Metadata.txt")
    3.  
    4.  
    5.         Dim object_type = fileReader.Split(",")(0)
    6.         Dim x = fileReader.Split(",")(1)
    7.         Dim y = fileReader.Split(",")(2)
    8.  
    9.         If object_type = "Textbox" Then
    10.             Dim newTB As New TextBox
    11.             newTB.Name = "tbNew" & objectcounter
    12.             'Set location, size and so on if you like
    13.             Me.Controls.Add(newTB)
    14.  
    15.             newTB.Location = New Point(x, y)
    16.         End If

    I guess this is why people have their own database file formats.. if I just make it into a text file it might be easily messed up by some rascal.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,117

    Re: Saving an edited Windows Form GUI

    Those wabbits can indeed be wascally.

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