开发者

Visual Basic 2010/ Sub Procedure

I copied the exact codes from my textbook and I have the following errors.

Error   3   'txtOutput' is not declared. It may be inaccessible due to its protection level.    C:\Users\Woong-Sup\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb    13  17  WindowsApplication1

Error   4   'txtOutput' is not declared. It may be inaccessible due to its protection level.    C:\Users\Woong-Sup\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb    15  17  WindowsApplication1

Error   1   Handles clause requires a WithEvents variable defined in the containing type or one of its base types.  C:\Users\Woong-Sup\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb    2   44  WindowsApplication1

Could anyone please tell me what the problem is? And I would appreciate if I could add you on my msn and ask some more questions! thanks

Public Class Form1
    Private Sub btnDisplay_C开发者_如何转开发lick() Handles btnDisplay.Click
        Dim anyDate As Date
        anyDate = CDate(InputBox("Input a date. (mm/dd/yyyy)"))
        ShowCentury(anyDate)
    End Sub

    Sub ShowCentury(ByVal anyDate As Date)
        Select Case anyDate
            Case Is >= #1/1/2000#
                txtOutput.Text = "twenty-first century"
            Case Is >= #1/1/1900#
                txtOutput.Text = "twentieth century"
            Case Else
                txtOutput.Text = "prior to the twentieth century"
        End Select
    End Sub
End Class


This is a partial class; it won't stand on its own. The "Form1" form this class is a part of (you do have a form named "Form1", right?) needs a button named "btnDisplay" and a textbox named "txtOutput".

If you have such a form, and the above controls exist on it, ensure that the "GenerateMember" property is set to True for both of them.


txtOutput should be a textbox on your form (form1 above)


This is an old question, but interesting. I found one possible issue...

When I paste the code in Form1 and then add the Button and the Textbox I get the same errors even when I rename them according to the names in code.

However, when I add the Button and Textbox first and then paste the code, it works fine after renaming the Button and Textbox.

Another oddity is that when I pasted the code first and then added the Button and Textbox (like I did in the first example), I could copy the Button name and Textbox name from the code and paste them in the Properties window and it would work. Typing the names in by the keyboard would not work.

I don't know why it works that way, but at least it partially helps explain this issue.

I presume it's an error in the IDE or compiler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜