locked
"Variable" is not declared. it may be inaccessible due to its protection level

    Question

  •  This is my code:::::

    Public Class Form1

        Private Declare Function Key Lib "user32" Alias "GetAsyncKeyState" (ByVal Key As Keys) As Keys

        Dim Count As Integer = 0

        Private Sub points()

            Dim PosX As Integer = Form1.MousePosition.X

            Dim PosY As Integer = Form1.MousePosition.Y

            Dim Point = "X: " & PosX & " Y: " & PosY

            If Count = 1 Then

                Label1.Text = Point

            ElseIf Count = 2 Then

                Label2.Text = Point

            ElseIf Count = 3 Then

                Label3.Text = Point

            ElseIf Count = 4 Then

                Label4.Text = Point

            End If

        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            If Key(Keys.LButton) Then

                Count = Count + 1

                points()

            End If

            If Count = 4 Then

                Count = 0

            End If

        End Sub

    End Class

    This is my error message:

    Error 1 'Label1' is not declared. It may be inaccessible due to its protection level.

    Error 2 'Label2' is not declared. It may be inaccessible due to its protection level.
    Error 3 'Label3' is not declared. It may be inaccessible due to its protection level.
    Error 4 'Label4' is not declared. It may be inaccessible due to its protection level.

    I don't know how to fix this error.

    Thanks for the help.

    Chasman


    Monday, June 11, 2012 8:40 PM

Answers

  • Is it an inherited Form? Do you have these labels on the Form?

    If I add the 4 labels, the error message I get is that Option Strict On, which is the recommended setting, doesn't allow implicit conversions from bool to System.Windows.Forms.Keys.

    The function return value must be of type Short, not Keys.

    You may consider putting the labels into an array so you can access them by Labels(Count-1) instead of these If statements (that could also be replaced by Select Case).

    I'd use the original name GetAsyncKeyState as it's well-known, instead of just "Key".

    EDIT: I Forgot to mention: Look at http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx to see how to correctly interpret the function return value.


    Armin

    Monday, June 11, 2012 8:55 PM
  • Got this message:

    "Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types." 

    Ok. Your initial post already contained "Handles...". Therefore, and because you didn't mention this error in that post, I assumed you already have a timer on the Form. If not, you must add it. Just add it from the toolbox to the Form as you did with the labels.

    Armin

    • Marked as answer by ChasmanBoerne Tuesday, June 12, 2012 2:22 AM
    • Unmarked as answer by ChasmanBoerne Tuesday, June 12, 2012 2:24 AM
    • Marked as answer by ChasmanBoerne Tuesday, June 12, 2012 2:25 AM
    Tuesday, June 12, 2012 12:13 AM

All replies

  • Is it an inherited Form? Do you have these labels on the Form?

    If I add the 4 labels, the error message I get is that Option Strict On, which is the recommended setting, doesn't allow implicit conversions from bool to System.Windows.Forms.Keys.

    The function return value must be of type Short, not Keys.

    You may consider putting the labels into an array so you can access them by Labels(Count-1) instead of these If statements (that could also be replaced by Select Case).

    I'd use the original name GetAsyncKeyState as it's well-known, instead of just "Key".

    EDIT: I Forgot to mention: Look at http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx to see how to correctly interpret the function return value.


    Armin

    Monday, June 11, 2012 8:55 PM
  • Please excuse this newbie reply.  I did not put the labels on the form.  After I did that, all errors are gone.  Thanks for your help Armin.  I'm just learning VB.

    Also, thanks for the array recommendation.  I need to access more than four points and it would be cumbersome to have a bunch of "If-then" statements.

    How would you code for the array?

    Chasman

    Monday, June 11, 2012 9:16 PM
  • Hi Chasman,

    I'd write it this way:

    Public Class Form1
    
       Private Declare Function GetAsyncKeyState Lib "user32" (ByVal Key As Keys) As Short
    
       Private Count As Integer
       Private ReadOnly _Labels As Label()
    
       Public Sub New()
    
          InitializeComponent()
    
          _Labels = New Label() {Label1, Label2, Label3, Label4}
    
       End Sub
    
       Private Sub points()
    
          Dim Pos = MousePosition
          Dim Point = "X: " & Pos.X & " Y: " & Pos.Y
    
          _Labels(Count).Text = Point
    
       End Sub
    
       Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
          If (GetAsyncKeyState(Keys.LButton) And &H8000S) <> 0 Then
             Count = (Count + 1) Mod _Labels.Length
             points()
          End If
    
       End Sub
    
    End Class

    I do not question your overall approach because I think it's mainly about getting practice.

    Just ask if something's unclear about the modifications made.


    Armin

    Monday, June 11, 2012 9:28 PM
  • Armin,

    Your code above brings up the form.  I have the labels on the form, but it does not record the position of the mouse clicks.  I don't see the problem in your code.  I am using VB 2010 Express.  Thanks.

    Chasman

    Monday, June 11, 2012 10:09 PM
  • Your code above brings up the form.  I have the labels on the form, but it does not record the position of the mouse clicks.  I don't see the problem in your code.  I am using VB 2010 Express.  Thanks.

    Oops, I'm sorry, I've deleted something to make it compile. Just add it again:

       Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    EDIT: Maybe you will also have to change "And &H8000S" to "And 1".


    Armin



    Monday, June 11, 2012 10:33 PM
  • I didn't want you to work this hard.

    I added "Handles Timer1.Tick" to the end of that line and changed "And &H8000S" to "And 1" but it still doesn't record the clicks.

    Chasman

    Monday, June 11, 2012 11:55 PM
  • Got this message:

    "Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types." 

    Monday, June 11, 2012 11:56 PM
  • Got this message:

    "Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types." 

    Ok. Your initial post already contained "Handles...". Therefore, and because you didn't mention this error in that post, I assumed you already have a timer on the Form. If not, you must add it. Just add it from the toolbox to the Form as you did with the labels.

    Armin

    • Marked as answer by ChasmanBoerne Tuesday, June 12, 2012 2:22 AM
    • Unmarked as answer by ChasmanBoerne Tuesday, June 12, 2012 2:24 AM
    • Marked as answer by ChasmanBoerne Tuesday, June 12, 2012 2:25 AM
    Tuesday, June 12, 2012 12:13 AM
  • Thanks Armin.  You've been very helpful.

    Chasman

    Tuesday, June 12, 2012 2:23 AM