How to reference an active form in design time

My question is:
I have to change status of controls, i.e text of a textbox, on the startup form of my application. I just don't know how to make a reference to the form to do that. As you
may know, the form is created automatically when application starts and creating a new instance of the form is obviously not the way.


Answer this question

How to reference an active form in design time

  • R Hearn

    going off of Jacob's code, you could instead declare the form as part of the module instead of inside Sub Main so that it would be global to your app and you could access Form1 that way, then set the Text Property
  • curiousEngine

    To avoid a module, you can use a static class as your entry point:

    Public Class AppEntryPoint
         Private Shared m_MainForm As MyForm

         <STAThread()> _
         Public Shared Sub Main()
              Application.Run(MainForm)
         End Sub

         Public Shared ReadOnly Property MainForm() As MyForm
              Get
                   If m_MainForm Is Nothing Then
                        m_MainForm = New MyForm()
                   End If
              End Get
         End Property
    End Class


    Set the startup object of your app to be this sub Main.  And, at any point throughout the execution of the app, you can acquire a reference to the startup form by accessing the shared property.

  • khaley

    I don't understand why you can't do it in the form code itself, but you could do the following:

    1-  Add a module to your project
    2-  In the module add code similar to the following:

    Public Sub Main()
         Dim f As New Form1()
         'Modify here...
         Application.Run(f)
    End Sub

    3-  Change the startup object of your project to be Sub Main.

    If this isn't what you are looking for, could you elaborate a bit

  • gharryh

    Thanks for your reply,but what really troubles me is that:suppose I have two forms in my application(SDI),form1 which is the startup form, and form2.  What I would like to do is to change form1's text property when form2 is closed. The code in form2 may look like the following:

    Sub Form2_Closing...

        dim myForm as form1=
        myForm.text=somethingelse
        ----
    End Sub

    What should I put in the place of " " to make a reference to form1

  • AmitG

    That did the trick. Thanks a lot.
  • Scottaroberts99

    Hello, I have basically the exact same question, but for C#. What I have is a form that pops up when the user wants to add/edit an issue.

    editIssue newWindow = new editIssue();

    newWindow.Show();

    This is how I open the new form. The problem I am having is that on the main form, I have a text area that displays the file that this form is accessing and editing. When the "editIssue" form closes, i would like to refresh the text box on the main form containing the text file so that it shows the modifications done in "editIssue." Could someone help me out with getting this problem fixed I am new to C# so I do not have a lot of knowledge on how to fix this. Thanks.


  • Manena

    Just to point out the obvious here, a Module is nothing more than a class in which all the members are shared/static. Using a module is essentially a shortcut for doing what Jacob suggests. There is no shame in it, except that it makes it a tiny bit more effort if you want to convert your code to C#. But it's a simple thing, just a class with all shared members.
  • JasonReis

    I'm glad that was asked and pointed out!  I never use Modules!  ;) 
  • codrakon

    This is absolutely one of the solutions. But how if I try to avoid using Module as it is not an standard OOP way. Do you know how this problem is solved in other OOP language like C#,C++ or Java
  • Tom_In_Dallas

    editIssue newWindow = new editIssue();

    newWindow.Showdialog();

    textarea.refresh;

    remco


  • How to reference an active form in design time