Results 1 to 8 of 8

Thread: Access form controls from a general module in .net

  1. #1
    Ergo60
    Guest

    Access form controls from a general module in .net

    In VB6 I could use the following code in a general module to change the text in a textbox on a form

    frmMain.tbText.Text = "Hello World"

    but this does not work in .NET. I have changed the Modifiers property to Public for each control I want access to, but they still do not show up in the intellisense listing.

    Any help?

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    you need to add a parameter to your routine of the Form class.

    ie

    mysub(ByVal myform As Form)

    then when you call it from code in you form, just pass Me

    mysub(Me)

    this is the OOP way of doing things.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Ergo60
    Guest
    OK

    I did try this and it seems to work, but is it correct practice?

    dim TfrmMain as frmMain

    set controls using TfrmMain reference

    TfrmMain = nothing

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yes it is. VB .NET is a true OOP language now, and OOP handles objects very differently as you dont have these persistant pointers acrosss process'. If that makes any sense.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Ergo60
    Guest
    So can I assume that when I execute the following code that it is just creating a new reference to the existing form? I would have thought that it was creating a new copy, and therefore wasting resources.

    What is it doing? Creating a new reference, or creating a new copy?

  6. #6
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    default ByRef

    in vb6 everthing was a copy of a copy of a copy because it was passed byval by default .net has integrated with the rest of the programming world and now passes a refreance so be careful what you do it isn't a copy unless you

    VB Code:
    1. Public Sub mSub(ByVal getCopyByVal as Object)

    so Cander is making a copy and your code is ByRef i think

    Cander what do you think = is = after all
    Last edited by Magiaus; Apr 12th, 2002 at 06:51 PM.
    Magiaus

    If I helped give me some points.

  7. #7
    Ergo60
    Guest
    I actually found out that the statement

    Dim TfrmMain as frmMain

    did not work. While it did create a variable of the frmMain type, it was still nothing, so it did not raise any errors, but it also did not do anything.

    I then changed it to Dim TfrmMain as New frmMain, and while this worked, it caused an endless loop of form creations, since I have initialization code in the area where the for is created.

    I did however find some code that microsoft uses when it converts an older VB6 project to .Net. This code creates a new property called defInstance that essentially does the same thing, but also defines a new boolean member variable that you can check to see if you are creating the initial form or just wanting a copy.

    While this is all well and good, it does seem to create a new instance of it, so when I am setting the values I am setting the ones of the copy, and then when I dispose of it the origional ones are still the old values.

    I will pst code in a few so that you can see what I am doing. I also do a similar thing with charting objects, but that actually works. I will post that code as well.

    Thanks

  8. #8
    Ergo60
    Guest
    OK here goes the code part, sorry if this gets to long:

    Here is the Microsoft code:
    Code:
    #Region "Upgrade Support "
      Private Shared m_vb6FormDefInstance As frmMain
      Private Shared m_InitializingDefInstance As Boolean
    
      Public Shared Property DefInstance() As frmMain
        Get
          If m_vb6FormDefInstance Is Nothing OrElse    
             m_vb6FormDefInstance.IsDisposed Then
             m_InitializingDefInstance = True
             m_vb6FormDefInstance = New frmMain()
             m_InitializingDefInstance = False
          End If
          DefInstance = m_vb6FormDefInstance
        End Get
        Set(ByVal Value As frmMain)
          m_vb6FormDefInstance = Value
        End Set
      End Property
    #End Region
    You can then use the m_InitializingDefInstance variable in your for initialize code to bypass any routines that should not be run on the new instance.


    In the code that I have in a General module I use it like this.

    frmMain.DefInstance.tbMinScale1.Text = GetSetting("ProdTest", "Settings", "ScaleMin1", "0")
    frmMain.DefInstance.tbMaxScale1.Text = GetSetting("ProdTest", "Settings", "ScaleMax1", "5")

    I have verified that the return value from GetSetting is correct, but it is not showing up on the form.

    I also have a generic status label on the main form that I use all over my code to inform the user what is going on. In VB6 it was easy to do, but .Net makes it a little harder. There has to be a way of making the form referance a public variable, right?

    Without passing it around all the time, there are many places where I need access outside of the main form code, so I would not have the reference to pass.

    thanks

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