none
Using Global Variables to Populate Text Boxes

    Question

  • I am working on a small program that has four forms at the moment with the likelihood that several more will be added. I have 6 global variables declared in a module. Most of the global variables are assigned values from text boxes on the first form. The other three forms also have some duplicate text boxes that use these variables. I do not have any problem using the variables on the other forms. What I would like to know is how can I have the text boxes on the other forms auto populate with the global values when I open each of the other forms. I used a "formactivated" event to trigger the assignment of the variables to the text boxes and that works but would that be the best way or the correct way to do this of is there another better way.
    Friday, March 17, 2017 4:37 AM

Answers

  • What I would like to know is how can I have the text boxes on the other forms auto populate with the global values when I open each of the other forms.

    Form.Activated would be a suitable choice for global variables.  If they were global constants then Form.Shown might be more suitable.   But note that the activated event can fire many times in a form's lifetime - if they are text boxes and not labels then presumably the user can override these values, and in that case if you use Form.Activated you would need to ensure that you were not deleting something the user had entered.

    An alternative that decouples these forms from the module is to pass the values to the form using a custom constructor. That means they will only be populated once on instantiating the form.

    • Marked as answer by J_Reid Saturday, March 18, 2017 11:59 PM
    Friday, March 17, 2017 5:28 AM
  • As an alternative, consider using My.Settings to store the values and then you can easily bind each textbox's text property to the settings value.  This will keep all textboxes in sync with the value with no extra code.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 17, 2017 3:16 PM
    Moderator

All replies

  • What I would like to know is how can I have the text boxes on the other forms auto populate with the global values when I open each of the other forms.

    Form.Activated would be a suitable choice for global variables.  If they were global constants then Form.Shown might be more suitable.   But note that the activated event can fire many times in a form's lifetime - if they are text boxes and not labels then presumably the user can override these values, and in that case if you use Form.Activated you would need to ensure that you were not deleting something the user had entered.

    An alternative that decouples these forms from the module is to pass the values to the form using a custom constructor. That means they will only be populated once on instantiating the form.

    • Marked as answer by J_Reid Saturday, March 18, 2017 11:59 PM
    Friday, March 17, 2017 5:28 AM
  • Thank you for your comment. You made me realize I had made a mistake. I am getting the values for 2 of the variables from each of the three forms. So I should be using only two text boxes and four labels on each form. it's late and I have been working on this too long.  After looking at this some more I believe that the activated event is probably correct for my situation. As I switch back and forth between the forms the values for the variables may change over time so this way they will get updated on the other forms as I switch back to any other form. Thanks again for your help.

    JR

    Friday, March 17, 2017 8:57 AM
  • As an alternative, consider using My.Settings to store the values and then you can easily bind each textbox's text property to the settings value.  This will keep all textboxes in sync with the value with no extra code.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 17, 2017 3:16 PM
    Moderator
  • I like your suggestion. I did not think of doing that but I will absolutely use this going forward. For this particular project someone else has already issued an edict as to how they want this done. It would be pure speculation on my part as to the why.

    I actually intended for this post to be a question rather that a discussion but I must have accidentally issued it as a discussion because I don't have the option to mark either response as an answer. I don't know how to remedy this. Both responses are very good answers and I would mark both of them as an answer if I could.


    JR

    Saturday, March 18, 2017 11:03 PM
  • I like your suggestion. I did not think of doing that but I will absolutely use this going forward. For this particular project someone else has already issued an edict as to how they want this done. It would be pure speculation on my part as to the why.

    I actually intended for this post to be a question rather that a discussion but I must have accidentally issued it as a discussion because I don't have the option to mark either response as an answer. I don't know how to remedy this. Both responses are very good answers and I would mark both of them as an answer if I could.


    JR

    If both answered are suitable to you then mark them both as answered.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 18, 2017 11:16 PM
    Moderator