Help me resolve this.

  • Thread starter Thread starter Bill English
  • Start date Start date
B

Bill English

I'm 14 and just started programming last week, so this is
probably an easy question.

How do I resolve this error:

An unhandled exception of
type 'System.NullReferenceException' occurred in Software
Inventory (My Version).exe

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

I have a form called form1 with a mainmenu that has a
button that shows a form called fmOpacity, which I want
to control opacity of form1. This is the code I used in
fmOpacity (tb is a trackbar):

Private Sub TrackBar1_Scroll(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
tb.Scroll
'
'Sets main form's transparency.
'
Dim trans As Form1
If tb.Value = 0 Then trans.Opacity = 0.1
If tb.Value = 1 Then trans.Opacity = 0.2
If tb.Value = 2 Then trans.Opacity = 0.3
If tb.Value = 3 Then trans.Opacity = 0.4
If tb.Value = 4 Then trans.Opacity = 0.5
If tb.Value = 5 Then trans.Opacity = 0.6
If tb.Value = 6 Then trans.Opacity = 0.7
If tb.Value = 7 Then trans.Opacity = 0.8
If tb.Value = 8 Then trans.Opacity = 0.9
If tb.Value = 9 Then trans.Opacity = 1
End Sub
 
You have to declare objects as new
Change the line "Dim trans As Form1" to "Dim trans As New Form1" and you
should be okay.
 
I am having the same problem, except the debugger halts at the first line: "Public Class Form1". It does it even on a blank form with no controls or code of any kind.
 
I tried that already, it doesn't produce an error, but it
doesn't do anything either.

The reason that doesn't do anything is that "Dim trans As New Form1"
creates a new -copy- of the form, when what you want to do is modify the
original. So, changing the transparency affects the copy of the form, which
isn't even on the screen yet. Luckily, it's not too hard to do. Inside the
code for your fmOpacity, add this:

Public OwnerForm As Form1

Public Sub New(ownerForm As Form1)
Me.OwnerForm = ownerForm
End Sub

Then, in your TrackBar scroll event, change the lines like:

If tb.Value = 0 Then trans.Opacity = 0.1

to

If tb.Value = 0 Then OwnerForm.Opacity = 0.1

And then you're almost done. Last thing: in the MainMenu where you open
fmOpacity, change it from

Dim MyForm [or whatever you called it] As New fmOpacity

to

Dim MyForm As New fmOpacity(Me)

And that should do it! All that's doing is keeping track of what the
"real" Form1 is, so that it knows which form to set the opacity for. Hope
this makes sense!

Jeremy
 
Okay, I did all of that, but now when I click the button
in my main menu to show fmOpacity, I get a blank form.

-----Original Message-----
I tried that already, it doesn't produce an error, but it
doesn't do anything either.

The reason that doesn't do anything is that "Dim trans As New Form1"
creates a new -copy- of the form, when what you want to do is modify the
original. So, changing the transparency affects the copy of the form, which
isn't even on the screen yet. Luckily, it's not too hard to do. Inside the
code for your fmOpacity, add this:

Public OwnerForm As Form1

Public Sub New(ownerForm As Form1)
Me.OwnerForm = ownerForm
End Sub

Then, in your TrackBar scroll event, change the lines like:

If tb.Value = 0 Then trans.Opacity = 0.1

to

If tb.Value = 0 Then OwnerForm.Opacity = 0.1

And then you're almost done. Last thing: in the MainMenu where you open
fmOpacity, change it from

Dim MyForm [or whatever you called it] As New fmOpacity

to

Dim MyForm As New fmOpacity(Me)

And that should do it! All that's doing is keeping track of what the
"real" Form1 is, so that it knows which form to set the opacity for. Hope
this makes sense!

Jeremy

.
 
Jeremy's response is perfect and there isn't much to add to it without
seeing all your code. If you repost your current code we can have another
look at it and not have to scroll through the post trying to piece it
together...

Cheers,
Lubos

Okay, I did all of that, but now when I click the button
in my main menu to show fmOpacity, I get a blank form.

-----Original Message-----
I tried that already, it doesn't produce an error, but it
doesn't do anything either.

The reason that doesn't do anything is that "Dim trans As New Form1"
creates a new -copy- of the form, when what you want to do is modify the
original. So, changing the transparency affects the copy of the form, which
isn't even on the screen yet. Luckily, it's not too hard to do. Inside the
code for your fmOpacity, add this:

Public OwnerForm As Form1

Public Sub New(ownerForm As Form1)
Me.OwnerForm = ownerForm
End Sub

Then, in your TrackBar scroll event, change the lines like:

If tb.Value = 0 Then trans.Opacity = 0.1

to

If tb.Value = 0 Then OwnerForm.Opacity = 0.1

And then you're almost done. Last thing: in the MainMenu where you open
fmOpacity, change it from

Dim MyForm [or whatever you called it] As New fmOpacity

to

Dim MyForm As New fmOpacity(Me)

And that should do it! All that's doing is keeping track of what the
"real" Form1 is, so that it knows which form to set the opacity for. Hope
this makes sense!

Jeremy

.
 
Bill, you need a reference to Form1. This will work for you.

'-- Put this in your opacity form

Dim trans As Form1

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles tb.Scroll

If tb.Value = 0 Then trans.Opacity = 0.1
If tb.Value = 1 Then trans.Opacity = 0.2
If tb.Value = 2 Then trans.Opacity = 0.3
If tb.Value = 3 Then trans.Opacity = 0.4
If tb.Value = 4 Then trans.Opacity = 0.5
If tb.Value = 5 Then trans.Opacity = 0.6
If tb.Value = 6 Then trans.Opacity = 0.7
If tb.Value = 7 Then trans.Opacity = 0.8
If tb.Value = 8 Then trans.Opacity = 0.9
If tb.Value = 9 Then trans.Opacity = 1
End Sub

Public WriteOnly Property Control() As Form1

Set(ByVal Value As Form1)

trans = Value
End Set
End Property



--Put this in your Form1 click event
Dim frm As New Form2()
frm.Control = Me
frm.ShowDialog()
 
Back
Top