Error Opening Form from another Form

N

Norm

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
 
H

Herfried K. Wagner [MVP]

Norm said:
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...
 
N

Norm

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 said:
Norm said:
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...
 
P

Peter Huang [MSFT]

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.
 
N

Norm

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
 
C

Chris Dunaway

Norm said:
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
 
N

Norm

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
 
P

Peter Huang [MSFT]

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.
 
N

Norm

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
 
N

Norm

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
 
P

Peter Huang [MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top