-
Apr 4th, 2021, 08:13 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Button2 not referenced !
Hi,
Form1 has two buttons, Button1 and Button2, and a label, Label1. They're all stilled called by their generated names.
There's not much code and yet starting the app. I get an error... What on earth is going on ?
vb.net Code:
Public Class Form1
ReadOnly yb As Integer = Button2.Location.Y
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Nothing
End Sub
Private Sub AnimateBack() Handles Button2.MouseUp
With Button2
.Location = New Point(.Location.X, yb)
.Height = 38
End With
End Sub
Private Sub AnimateDown() Handles Button2.MouseDown
With Button2
.Location = New Point(.Location.X, yb + 10)
.Height = 28
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
Error !
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Zing
StackTrace:
at Zing.Form1..ctor() in D:\~VB Trials\Zing\Form1.vb:line 3
How can Button2 not be referenced ?
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Apr 4th, 2021, 08:55 AM
#2
Re: Button2 not referenced !
You can't reference controls before they are created, and the declarations section actually runs before the controls are created.
Set the value of yb in the Load event instead.
-
Apr 4th, 2021, 04:31 PM
#3
Re: Button2 not referenced !
Code:
ReadOnly yb As Integer = Button2.Location.Y
As si_the_geek says, at that point in your app. Button2 hasn't been declared.
If you want a readonly value at Form scope...
Code:
Const yb As Integer = 12
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 4th, 2021, 06:53 PM
#4
Thread Starter
PowerPoster
Re: Button2 not referenced !
Along with the sunshine there has to be a little rain sometime.
-
Apr 4th, 2021, 11:32 PM
#5
Re: Button2 not referenced !
Originally Posted by .paul.
As si_the_geek says, at that point in your app. Button2 hasn't been declared.
That's not really true. Button2 is a variable and it certainly has been declared at that point. The actual problem is that the Button object has not been created and assigned to that variable at that point, so the variable is Nothing.
-
Apr 5th, 2021, 04:59 AM
#6
Thread Starter
PowerPoster
Re: [RESOLVED] Button2 not referenced !
Thanks John,
That sounds much more plausible.
I thought it was odd when I asked the question, I couldn't understand why it wasn't declared, after all... It had already generated Mouse Up and Mouse Down methods.
Poppa
Along with the sunshine there has to be a little rain sometime.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|