Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

Referencing Forms

I'm working on an application that starts with a sub main in a main module which sets ofrm1 = new Form1 and ofrm2 = new Form2 where Form1 and Form2 are the names of 2 forms. The code below is in the Form1 class. If I change the 'Form2' reference to ofrm2 an error on startup is generated. Similarly if I add a line with ofrm2.location = Form2.location it causes the same error, and if I don't change the code then the ofrm2 form doesn't move when Form1 is moved.

Any ideas please?  The error says:
System.TypeInitializationException was unhandled
The type initializer for modMain threw an exception.
Object reference not set to an instance of an object
Private Sub Form1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        Form2.Left = Me.Left + Me.Width
        Form2.Top = Me.Top
End Sub
 
Tried:
Private Sub Form1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        Form2.Left = Me.Left + Me.Width
        Form2.Top = Me.Top
       if IsReference(ofrm2) then
         ofrm.location = Form2.location
       end if
End Sub

Open in new window

0
ncw
Asked:
ncw
  • 5
  • 5
  • 2
  • +2
3 Solutions
 
käµfm³d 👽Commented:
Can you show the code where you are declaring/setting the ofrm1 & ofrm2 variables?
0
 
ncwAuthor Commented:
At the top of modMain I have:
Public ofrm1 as new Form1
Public ofrm2 as new Form2

In the code snippet in my question above, it should read ofrm2.location = Form2.location
0
 
CodeCruiserCommented:
Form2 is a class and not an object. So when you try to use Form2.Location then it generates the error. You need to use objects of class Form2 such as ofrm2 rather than the Form2 class itself.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
abelCommented:
If you have a WinForms application, why do you have a modMain function? Only if you create a console application you can start the application with a module.

Why do you place the form variables inside a module? They shouldn't be there (and cause trouble). Try to reference the forms to one another from the Form1.vb and the Form2.vb code.
0
 
abelCommented:
> Form2 is a class and not an object.

you can reference forms by their names in VB.NET as if they are objects. If you create a new form based on an existing form, you should use the variable that you assigned it to.
0
 
ncwAuthor Commented:
I'm using a modMain because it gives me more control and I believe a better structure to the application. But I was having problems referencing the forms by their names, I think it was from within one form to another.
0
 
abelCommented:
I'm afraid you're a bit on the wrong page here. The "better" way to gain more control is to use the application startup event, which is there for exactly that reason: more control: http://msdn.microsoft.com/en-us/library/t4zch4d2(VS.80).aspx

However, if you want to go this path, check out this article on how to do it: http://www.getdotnetcode.com/GdncStore/free/Articles/VB%20NET%20Sub%20Main%20Procedure.htm

A thread that previously discussed this matter, with some very good comments of Idle_Mind can be found here: http://www.experts-exchange.com/Programming/Languages/.NET/Q_23841489.html

Make it easy on yourself and stick to some of the coding guidelines, it will give you less trouble in the long run and others joining your project will also understand what you're doing....
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I agree with abel...Application.Startup() would give you more control than Sub Main (usually).  Instead of using Public variables in a Module, you could switch to using the "default instances" of the forms.  These are accessed by using the Forms respective names WITHOUT ever using the "New" keyword on them.  They are available in VB.Net 2005 (and above)...

Also, please show us how you are actually DISPLAYING the forms.

But specifically, in your code, this might work instead:
Private Sub Form1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
    If Not IsNothing(ofrm2) Then
        ofrm2.Location = New Point(Me.Location.X + Me.Width, Me.Location.Y)
    End If
End Sub

Open in new window

0
 
ncwAuthor Commented:
The structure in the 2nd link is not too different from what I have, I just need to move my sub main to a class and define the sub main as they show in the article. Otherwise I have to tick the 'Enable Application Framework' checkbox under properties, select a form as startup object and include a Startup sub as described in the 1st link. (I'm coming from VB6)

The following threw the same error:

    If Not IsNothing(ofrm2) Then
        ofrm2.Location = New Point(Me.Location.X + Me.Width, Me.Location.Y)
    End If
   
I will experiment and come back to you, thanks.
0
 
abelCommented:
You are saying "looks like". Just for the sake of getting your code correct, let's try to tackle this in two phases.

1. Make sure you have a WinForms application, not a Console app or something similar. Get it to work the "normal" way, with startup form selected in the Properties of the app.

2. Once we have that tackled, we'll help you with using a class for your startup Sub Main procedure (though it technically really does not matter where you have that Shared Sub placed: form or class, as long as it is not in a Module, because that will not work here).

-- Abel --
0
 
ncwAuthor Commented:
I'm back on this now. I confirm the project is a WinForms application. I understand what you say about using Application.Startup instead of a sub main, although I don't understand why you say it won't work with sub main in a module as for the time being it is exactly that and appears to work.

I think the article at http://www.devcity.net/Articles/100/1/.aspx is useful, it advocates creating instances of forms rather than referencing forms directly by their name (as in vb6).
0
 
CodeCruiserCommented:
@ncw,
As i said earlier, although its possible to reference the default instance using classname, its better to use instances because it avoids a lot of confusions.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This works fine for me:

Module Module1

    Public WithEvents ofrm1 As Form1
    Public WithEvents ofrm2 As Form2

    Private Sub ofrm2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles ofrm2.FormClosed
        ofrm2 = Nothing
    End Sub

    Public Sub Main()
        ofrm1 = New Form1
        Application.Run(ofrm1)
    End Sub

End Module

Public Class Form1

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        If IsNothing(ofrm2) Then
            ofrm2 = New Form2
            ofrm2.StartPosition = FormStartPosition.Manual
            ofrm2.Location = New Point(Me.Location.X + Me.Width, Me.Location.Y)
            ofrm2.Height = Me.Height
            ofrm2.Show(Me)
        End If
    End Sub

    Private Sub Form1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
        PositionForm2()
    End Sub

    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        PositionForm2()
    End Sub

    Private Sub PositionForm2()
        If Not IsNothing(ofrm2) Then
            ofrm2.Location = New Point(Me.Location.X + Me.Width, Me.Location.Y)
            ofrm2.Height = Me.Height
        End If
    End Sub

End Class


0
 
abelCommented:
I don't agree with deletion, this question has received ample coverage by Idle_Mind, CodeCruiser and me. Suggestion to close as follows, split between:

http:#24406946 (Idle_Mind: answer)
http:#24185191 (abel, assist)
http:#24184919 (CodeCruiser, assist)

0
 
ncwAuthor Commented:
Sorry it's been a while, still working on this project.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now