Saving Controls to File - Help!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Saving Controls to File - Help!

  1. #1
    Join Date
    May 2013
    Posts
    2

    Saving Controls to File - Help!

    In VB 2005, I have a form with a panel control. On right-click of the panel, a context menu displays different types of controls the user can add (at runtime) to the form by clicking the menu items. At runtime, the user adds a variable number of different controls to the form by performing this task mutliple times.

    My goal is to have the form remember all of the controls upon form.closing and reload them on form.load, but I'm not sure how to go about translating between control objects and text. Ideally, I'd like to iterate through the panel's controls, and write all properties of each control to a file; but I have no idea how to translate a control object into text (capturing -all- of its properties), or vice versa (creating a control from a comprehensive property list in text/list/etc).

    I'd also like to be able to store all of the parent form's controls and their properties, and the form's global variables. Depending on how this can be achieved, I might need to use multiple files for persisting the data (maybe even one per control), but I'm stuck at the translation part.

    It's not as easy as grabbing a textbox's .text property, because I also need its .backcolor and everything else. Additionally, some controls have properties that others don't have; so if I wrote a method to iterate through a passed control's .backcolor, .borderstyle, ... , .tag, .text, ... then I'd miss the unique ones (like listbox/combobox .items, checkbox .checked, etc). Is there a way to simply iterate through all properties of a given control and reference the name and value of each?

    Help!

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,961

    Re: Saving Controls to File - Help!

    One way is to use XML like the new versions of Windows 8 programs
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: Saving Controls to File - Help!

    I haven't tried it but my first thought would be to use a for each loop to loop through the controls properties possibly nested within another for each loop that would loop through all the controls in the panel
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    May 2013
    Posts
    2

    Re: Saving Controls to File - Help!

    @dglienna: could you be more specific? By what process can I use XML to accomplish this task?

    @DataMiser: That was my first thought as well; however, my core issue is that I know of no way to do so. If I understand correctly, you mean:

    Code:
    For Each c As Control in Panel1.Controls
        For Each p as [PROP] in c.[PROPERTIES]
        ...
    ...
    If so, I don't know of anything that actually goes where I placed [PROP] and [PROPERTIES] that will actually allow one to iterate through all properties of a variable Control object, or perhaps what other syntax I need to use. I hope someone does, because I'm sure it's simple and possible.

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,961

    Re: Saving Controls to File - Help!

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: Saving Controls to File - Help!

    If you are putting the controls on the form, through the menu, then you can keep track of what is on the form and what is not on the form. Keep a list of all of the available objects with a flag of whether or not you put it on the form.

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

    Re: Saving Controls to File - Help!

    For saving the controls an XML file could work. You'd have to save each property setting alongwith each control, so this could become a bit time consuming though.

    I'd recommend implementing the ISerializable interface which will be able to save / retrieve objects ie. your pyhiscal control as is, without any string manipulation afterwards.

    Also, Saving this type of things I wouldn't recommend only saving it on Form_Close, I'd have a button saving it prior to existing / in order to exit
    Last edited by HanneSThEGreaT; May 23rd, 2013 at 08:41 AM. Reason: added link

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center