Here is what I wrote:
Public Class Form1
Public Sub Label1_MouseHover(sender As Object, e As System.EventArgs) Handles Label1.MouseHover
Dim Label2 As New Label
Label2.Location = New Point(158, 87)
Label2.Text = "lol"
Me.Controls.Add(Label2)
End Sub
Public Sub Label1_MouseLeave(sender As Object, e As System.EventArgs) Handles Label1.MouseLeave
Me.Controls.Remove(Label2)
End Sub
End Class
So, I wanted to create a new label when I get my mouse on the other one, and when I leave label with my mouse, I want that newly created control to disappear.
With this code, it says: “‘Label2’ is not declared. It may be inaccessible due to its protection level.” Probably because Label2 is not actually in registry until I create it when I run the program. Can somebody help me then?
The variable
Label2is local to the Function it is declared in – this means you can only access it inside this function. You could use a variable at the module level (declare it private if you don’t want to use it from other modules too). See the MSDN Scope in Visual Basic article.