XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

AprMay 2008Jun
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Archives

Topics

Ramblings

VB.NET

Very early on in my .NET learning curve I came up against the old "How do I access data and controls on the startup form?" problem.   With a little help from a knowledgeable friend  to help me solve the puzzle, I thought I may as well put my solution in an article for devCity.  Which I did - http://www.devcity.net/Articles/117/1/multipleforms4.aspx here.

  I know that it's been helpful to many .NET Newbies - the feedback comments tell me so.  As time has gone on, though, I'm less sure that the way described there is necessarily the best one.  But it works and people seem to be able to use it to resolve the problem.  Should I change it, I wondered.

  As it turns out, having dawdled over the decision for so long , events are pretty close to overtaking me with  Visual Basic 2005 being just around the corner.   It seems that the problem of the inaccessible startup form has been such a thorn in the side of Classic VB Upgraders that the 2005 development team have reverted to the old ways of providing a default instance of the startup form which can be referred to by name.

  So, I thought I'd just leave the original article alone and content myself with offering some alternative approaches in this blog.  

  The first alternative, probably a better way,  would simply be to run the application from a Sub Main in a Module.  

   Create a project which has two forms - leave them as the default names of Form1 and Form2.   Add a Module to the project.

  Put this code in the Module:

Module Module1
    Friend F1 As Form1

    Sub Main()
        F1 = New Form1
        Application.Run(F1)
    End Sub
End Module

 Then right click on the Project name and select Properties.  Use the drop down list and select Sub Main as the StartUp object.

  Save the project and run it.   Sub Main will be accessed first and it will "open  Form1" in the way you will have become familiar with in  Classic VB (that is, it will create and show an instance of a Form1 object).

   Close the project down and we'll test that the Form1 instance can be accessed from another form.   Add this code to the Load event of Form1:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim F2 As New Form2
        F2.Show()
    End Sub

  Finally, add this code to the load event of Form2.

 Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        F1.Text = "Changed by code in F2 !"
    End Sub

All that this does is :
1.   creates and shows an instance of Form2 as soon as Form1 is loaded
and
2.   makes a superficial change to the Caption of the Form1 instance when Form2 has been created and loaded. 

  Save and run the project now and the two forms will be visible.   The Caption text on Form1 will have been changed by code located inside Form2.   This simply provides confirmation that Form1 can indeed now be accessed from other forms.

  -------------------

  There are other ways of doing this, which I'll  blog up later.

posted on Saturday, August 06, 2005 12:41 PM