Results 1 to 6 of 6

Thread: [2.0] Form.ActiveForm

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2.0] Form.ActiveForm

    Hey all,

    I have a class which contains a private method. The private method must have access to the main form. For example, I could do this:

    Code:
    frmSnake.ActiveForm.PropertyName
    But ActiveForm in my opinion is very bad programming to use in this situation. Sometimes the code fails at runtime and it just isn't a reliable thing to do in my opinion.

    So I don't want to use frmSnake.ActiveForm and the method is private so I cannot really pass any Form variables directly to it..

    Any ideas on this one? Here's some pics to help you envision my problem:


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Form.ActiveForm

    Firstly, you wouldn't use the ActiveForm property unless you specifically wanted a reference to the active form, whatever form that might be. If you specifically want a reference to your startup form then that's just not appropriate. Using the ActiveForm property is good programming if you want a reference to the active form, other wise it's not.

    Secondly, the ActiveForm property is static, so you call it on the Form class, not on an instance of the class.

    If you can't directly pass a reference to the main form to the places it's needed then you will need to use a global variable. You could use a static class, which is the C# equivalent of a VB module, or you could use a Singleton class. Either way you'd be setting and getting the main form reference via a static property so you'd have access to it from anywhere.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Form.ActiveForm

    The whole idea sounds a bit messy. I only need to be able to add controls to the forum. There must be a better solution than creating a static class or singleton JUST for calling the Controls property of the main form.

    Hopefully a better solution?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Form.ActiveForm

    I fail to see what's bad about the solution provided such that a better one need be sought. The "proper" way to get access to your main form elsewhere is to pass a reference to it where it's needed. THAT is the better solution. If you can't or won't do that then the alternative is as I described. VB has the My.Application.ApplicationContext.MainForm property; C# does not. Unless you have some magic that I don't know about then you will use the solution provided.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Form.ActiveForm

    Alright, thanks John

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Form.ActiveForm

    Quote Originally Posted by jmcilhinney
    Unless you have some magic that I don't know about then you will use the solution provided.
    You will use that solution because that's the only solution available, not just because I said so.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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