By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

Error Opening Form from another Form

P: n/a
I must not be understanding something about the use of forms in VB.Net that
is different from VB. I have one form running in the background with an icon
in the task bar. Right clicking on the icon gives you a list of options, one
of which is to show an About form. After this form shows I get an error
saying "Object reference not set to an instance of the object." I have tried
opening the form with

frmAbout.Show
frmAbout.ShowDialog

And
Dim frmAbout2 As New frmAbout

frmAbout2.ShowDialog()

frmAbout2.Show

But I still get the same error. How do I fix this problem?

Thanks,

Norm
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Norm" <No****@newsgroups.nospam> schrieb:
I must not be understanding something about the use of forms in VB.Net that
is different from VB. I have one form running in the background with an
icon
in the task bar. Right clicking on the icon gives you a list of options,
one
of which is to show an About form. After this form shows I get an error
saying "Object reference not set to an instance of the object." I have
tried
opening the form with
Where exactly does the error occur? Does the IDE point to a certain line?
Which controls are you using on the form?
And
Dim frmAbout2 As New frmAbout
[...]
frmAbout2.Show


This should basically work...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
The complete error is
An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object.

The IDE highlights the original line calling the second form after the form
shows.

I have tried this with several different forms as frmAbout and get the same
error.

Norm
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
"Norm" <No****@newsgroups.nospam> schrieb:
I must not be understanding something about the use of forms in VB.Net that is different from VB. I have one form running in the background with an
icon
in the task bar. Right clicking on the icon gives you a list of options,
one
of which is to show an About form. After this form shows I get an error
saying "Object reference not set to an instance of the object." I have
tried
opening the form with


Where exactly does the error occur? Does the IDE point to a certain line?
Which controls are you using on the form?
And
Dim frmAbout2 As New frmAbout
[...]
frmAbout2.Show


This should basically work...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
Hi

Thanks for your information.
To troubleshooting the problem, can you help to build a simplest reproduce
sample and post in the newsgroup, if you have any concern, you may also
send to me via removing the "online" from the my email address.

Basically, show one form in another form will not cause the exception, I
think we need more information to troubleshooting the problem, thanks for
your efforts in advance.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #4

P: n/a
Peter,

Thanks for the information, I am going to start over with a new blank form
and see how far I can get before seeing any errors. There is something weird
going on as I was able to get everything else to work, but after left
clicking on several shortcuts I would get the same error on my main form.

This has become a learning process harder than the VB6 was, but I am
determined to continue to learn.

I will let you know if I run into this error again and maybe I can track it
down a little closer. When I run the debugger the program will start without
any errors, but when I run the build executable the program throws an
exception at start up.

Thanks,
Norm
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:R2*************@TK2MSFTNGXA01.phx.gbl...
Hi

Thanks for your information.
To troubleshooting the problem, can you help to build a simplest reproduce
sample and post in the newsgroup, if you have any concern, you may also
send to me via removing the "online" from the my email address.

Basically, show one form in another form will not cause the exception, I
think we need more information to troubleshooting the problem, thanks for
your efforts in advance.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #5

P: n/a
Norm wrote:
Thanks for the information, I am going to start over with a new blank form
and see how far I can get before seeing any errors. There is something weird


If you do get an error, print out the stack trace, which should provide
more detailed information. You can use code similar to the follow to
get the stack trace:

Try

'Code here that may generate an exception

Catch ex As Exception
Dim s As String
While Not ex Is Nothing
s &= ex.Message & ": " & ex.StackTrace
ex = ex.InnerException
End While
MsgBox(s)
End Try

Nov 21 '05 #6

P: n/a
Chris,

I have been unable to catch the exception, as it is now showing on the Main
form class Name, rather than in the code anywhere that I can find.

But if I remark this line out I don't get the error.
Call Hook(Me.Handle.ToInt32)

Which calls this function

Public Sub Hook(ByVal gHW As Integer)

lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)

End Sub

That I had to delegate as below

Delegate Function WindowProcDelegate(ByVal hwnd As Integer, ByVal msg As
Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

This is used in connection with a hotkey F10 to show and hide the desktop.

Norm

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Norm wrote:
Thanks for the information, I am going to start over with a new blank form and see how far I can get before seeing any errors. There is something
weird
If you do get an error, print out the stack trace, which should provide
more detailed information. You can use code similar to the follow to
get the stack trace:

Try

'Code here that may generate an exception

Catch ex As Exception
Dim s As String
While Not ex Is Nothing
s &= ex.Message & ": " & ex.StackTrace
ex = ex.InnerException
End While
MsgBox(s)
End Try

Nov 21 '05 #7

P: n/a
Hi

I think you may not need to use the subclass(ie call the SetWindowLong
API), you may try to take a look at the link below about how to handle the
keys in .NET.
Keyboard event handling in .NET applications by Alfred Mirzagitov
http://thecoadletter.com/article/0,1410,30129,00.html

Also what is concrete job in winproc, did you do the aboutFrm.Show in the
F10 KeyDown?
Anyway I suggest you try the article above first, if that did not work,
please post/send your reproduce code.
e.g.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.F10 Then
Dim fm As New Form2
fm.Show()
End If
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #8

P: n/a
Peter,

Trapping the key stroke from the form won't work for me, as the main form is
hidden and has an icon showing in the system task bar that shows a pop up
menu when clicked.

The program hides all the desktop icons, but allows you to use them through
the pop up menu.

I want to capture all keystrokes and when F10 is pressed to show the desktop
icons and when pressed again hide the desktop icons. I assume I will need to
do some type of message trapping, which is what I was doing with Hook and
WindowProc, which does not want to work with VB.Net.

Norm
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:pr*************@TK2MSFTNGXA01.phx.gbl...
Hi

I think you may not need to use the subclass(ie call the SetWindowLong
API), you may try to take a look at the link below about how to handle the
keys in .NET.
Keyboard event handling in .NET applications by Alfred Mirzagitov
http://thecoadletter.com/article/0,1410,30129,00.html

Also what is concrete job in winproc, did you do the aboutFrm.Show in the
F10 KeyDown?
Anyway I suggest you try the article above first, if that did not work,
please post/send your reproduce code.
e.g.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.F10 Then
Dim fm As New Form2
fm.Show()
End If
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #9

P: n/a
I don't know if this was the best way, but I got it to work by Unhooking the
form while it was showing and then when it was hidden again calling the Hook
function.

Norm
"Norm" <No****@newsgroups.nospam> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
Peter,

Trapping the key stroke from the form won't work for me, as the main form is hidden and has an icon showing in the system task bar that shows a pop up
menu when clicked.

The program hides all the desktop icons, but allows you to use them through the pop up menu.

I want to capture all keystrokes and when F10 is pressed to show the desktop icons and when pressed again hide the desktop icons. I assume I will need to do some type of message trapping, which is what I was doing with Hook and
WindowProc, which does not want to work with VB.Net.

Norm
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:pr*************@TK2MSFTNGXA01.phx.gbl...
Hi

I think you may not need to use the subclass(ie call the SetWindowLong
API), you may try to take a look at the link below about how to handle the keys in .NET.
Keyboard event handling in .NET applications by Alfred Mirzagitov
http://thecoadletter.com/article/0,1410,30129,00.html

Also what is concrete job in winproc, did you do the aboutFrm.Show in the F10 KeyDown?
Anyway I suggest you try the article above first, if that did not work,
please post/send your reproduce code.
e.g.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.F10 Then
Dim fm As New Form2
fm.Show()
End If
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no

rights.


Nov 21 '05 #10

P: n/a
Hi

I reviewed the thread and find that there is a similar issue in the
newsgroup below.Now I have replied to you, you may go and take a look.
Subject: RE: How to trap HotKey When Form is Hidden
Newsgroups: microsoft.public.dotnet.languages.vb

Are they the same question, if I have any concern, please feel free to post
here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.