0

I use Visual Studio using language: VB in the case to run my code I come up with this errors:

  • (8:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (9:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (9:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (11:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (12:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (12:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (14:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (15:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (15:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (17:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (18:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (18:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (20:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (21:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (21:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (23:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (24:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (24:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (26:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (27:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (27:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (29:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (30:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (30:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (32:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (33:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (33:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (35:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (36:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (36:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (38:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (39:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (39:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (41:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (42:0) 'Val' is not declared. It may be inaccessible due to its protection level.
  • (42:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (43:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (46:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (47:0) 'Val' is not declared. It may be inaccessible due to its protection level.
  • (47:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (48:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (51:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (52:0) 'Val' is not declared. It may be inaccessible due to its protection level.
  • (52:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (53:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (56:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (57:0) 'Val' is not declared. It may be inaccessible due to its protection level.
  • (57:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (58:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (61:0) Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • (62:0) 'Val' is not declared. It may be inaccessible due to its protection level. -(62:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • (75:0) 'TextBox1' is not declared. It may be inaccessible due to its protection level.

I tried to run this code:

Public Class Form1

    Dim firstNumber As Single
    Dim secondNumber As Single
    Dim answerNumber As Single
    Dim arithmeticprocess As String

    Private Sub button1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = TextBox1.Text & 1
    End Sub
    Private Sub button2_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = TextBox1.Text & 2
    End Sub
    Private Sub button3_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Text = TextBox1.Text & 3
    End Sub
    Private Sub button4_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Text = TextBox1.Text & 4
    End Sub
    Private Sub button5_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox1.Text = TextBox1.Text & 5
    End Sub
    Private Sub button6_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox1.Text = TextBox1.Text & 6
    End Sub
    Private Sub button7_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        TextBox1.Text = TextBox1.Text & 7
    End Sub
    Private Sub button8_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        TextBox1.Text = TextBox1.Text & 8
    End Sub
    Private Sub button9_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        TextBox1.Text = TextBox1.Text & 9
    End Sub
    Private Sub button10_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        TextBox1.Text = TextBox1.Text & 0
    End Sub
    Private Sub button11_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        TextBox1.Text = TextBox1.Text & " . "
    End Sub
    Private Sub button12_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        firstNumber = Val(TextBox1.Text)
        TextBox1.Text = "0"
        arithmeticprocess = "+"
    End Sub
    Private Sub button13_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        firstNumber = Val(TextBox1.Text)
        TextBox1.Text = "0"
        arithmeticprocess = "-"
    End Sub
    Private Sub button14_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        firstNumber = Val(TextBox1.Text)
        TextBox1.Text = "0"
        arithmeticprocess = "*"
    End Sub
    Private Sub button15_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        firstNumber = Val(TextBox1.Text)
        TextBox1.Text = "0"
        arithmeticprocess = "/"
    End Sub
    Private Sub button16_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        secondNumber = Val(TextBox1.Text)
        If arithmeticprocess = "+" Then
            answerNumber = firstNumber + secondNumber
        End If
        If arithmeticprocess = "-" Then
            answerNumber = firstNumber + secondNumber
        End If
        If arithmeticprocess = "*" Then
            answerNumber = firstNumber + secondNumber
        End If
        If arithmeticprocess = "/" Then
            answerNumber = firstNumber + secondNumber
        End If
        TextBox1.Text = answerNumber
    End Sub
End Class
Craig W.
  • 17,838
  • 6
  • 49
  • 82
mpes.
  • 71
  • 9
  • 1
    See this [link](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/withevents). – Han Sep 05 '18 at 19:58
  • 3
    Did you import a form and not include the designer.vb file? – Parrish Husband Sep 05 '18 at 20:02
  • @Han thanks for the link is very interesting but it shows this error: "Expression expected." "WithEvents' variables cannot be typed as arrays." – mpes. Sep 05 '18 at 21:32
  • @ParrishHusband I import an empty form file, also I suppose the solution is the Protected WithEvents, can you put an code example about Protected WithEvents? – mpes. Sep 05 '18 at 21:49
  • 3
    That code desperately need `Option Strict On` at the top of the file – Ňɏssa Pøngjǣrdenlarp Sep 05 '18 at 23:45
  • @Plutonix that's nice! it was off the option strict, so now I can see exactly what caused the problem. – mpes. Sep 07 '18 at 15:17

1 Answers1

2

In a Visual Basic WinForm, a Form object has at least 2 files (sometime 3 files). Let's say the Form is Form1, it has Form1.vb and Form1.Designer.vb. The Form1.vb is where you put your code. The Form1.Designer.vb is where Visual Studio (or other IDE) put the generated code for objects inside the form (buttons, textboxes, comboboxes, grids, etc). Each object/variable must be declared either in Form1.vb (written by you) or Form1.Designer.vb (generated by IDE). Do not write any code in the SomeFormName.Designer.vb because you're not supposed to write anything in it, it's the IDE who writes in it.

If you have the complete form files, just add it from Visual Studio. Right click your project, then click Add, then click Existing Item (see picture 1). Choose the form name (see picture 2), eg. Form2.vb and the form (including the Designer.vb) will be added to your project.

enter image description here enter image description here

If you only have the code, you could try to add the objects manually one by one. Let's say I copy your code like this (see picture 3). It shows 3 errors because the IDE can't find objects named TextBox1 and Button1 (see red lines in red box). There is a button to show all files in your project in the green box. By default, the Form1.Designer.vb files are hidden. Once you click it, the Form1.Designer.vb is visible (see blue box).

There are 3 error messages (2 kinds, one of them is a duplicate of the previous one):

  • Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
  • 'TextBox1' is not declared. It may be inaccessible due to its protection level.
  • 'TextBox1' is not declared. It may be inaccessible due to its protection level.

'TextBox1' is not declared is clear enough. You need a textbox named TextBox1. In Handles clause requires a WithEvents variable defined in the containing type or one of its base types. error, see in the code near the word Handles. It says Handles Button1.Click. It means you need a button named Button1 and you need to create a handle for its Click event. You need to wire the Button1's Click event to your code.

enter image description here

Go back to the designer. Drag the button and textbox (see red box) from the toolbox into the form. Place them anywhere you like.

enter image description here

Click the button in the form, and see the property window (see red box). Make sure that the button's name is Button1 (like in the code). If it's different, change it in the property window. Do the same for the textbox. Make sure the textbox's name is TextBox1.

enter image description here

Now the red lines disappear and you can compile and run the code. Try running the code and click the button. It will add a 1 character in the textbox. You can try to add other lines and add more objects when you encounter errors. This method is not guaranteed that the code will run properly because when you add the object, the object has default properties. The original code might have some changes in the object's properties.

Judging from the code, this is a calculator sample. There are 16 buttons named Button1, Button2, Button3, ..., till Button 16. Also a textbox named Textbox1. You can change the Button's text property so it shows the correct text. Button1 till Button9 are 1 till 9, Button10 is 0, Button11 is ., Button12 till Button15 are +-*/ signs and Button16 is = sign.

Small bug

There's a small calculation bug when pressing the equal button. The original code always add the firstNumber and secondNumber regardless the arithmeticprocess value (the sign). It should be like this:

Private Sub button16_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
    secondNumber = Val(TextBox1.Text)
    If arithmeticprocess = "+" Then
        answerNumber = firstNumber + secondNumber
    End If
    If arithmeticprocess = "-" Then
        answerNumber = firstNumber - secondNumber
    End If
    If arithmeticprocess = "*" Then
        answerNumber = firstNumber * secondNumber
    End If
    If arithmeticprocess = "/" Then
        answerNumber = firstNumber / secondNumber
    End If
    TextBox1.Text = answerNumber
End Sub

Improvement

Since I noticed lots of redundancy in the code, I tried to simplify it. Here's what I changed:

  1. Add Option Strict On. Read here for more information.
  2. Change the buttons' names into something more meaningful. Example: Button1 changed to ButtonNumber1, Button11 changed to ButtonDot, Button12 changed to ButtonAdd, Button16 changed to ButtonEqual. Do this for the 16 buttons.
  3. Change firstNumber, secondNumber and answerNumber type to double. Val() function returns a double so it's safer to change the variable type than converting the value of double into single (double has larger range than single).
  4. Use a single event handler for button with number 0-9 and the dot. I set the text property of these buttons to hold the numbers and the dot. The code will read the text property of the button and append it to the text property of the textbox.
  5. Use a single event handler for the math operations (+-*/). Same trick in number 4. I set the button's text property to hold the math operation sign.
  6. Use ToString() method when assigning answerNumber back into the textbox. It's required because of the Option String On.

The code:

Option Strict On

Public Class Form1

    Dim firstNumber As Double, secondNumber As Double, answerNumber As Double
    Dim arithmeticProcess As String

    Private Sub ButtonNumber_Click(sender As Object, e As EventArgs) Handles ButtonNumber1.Click,
        ButtonNumber2.Click, ButtonNumber3.Click, ButtonNumber4.Click, ButtonNumber5.Click,
        ButtonNumber6.Click, ButtonNumber7.Click, ButtonNumber8.Click, ButtonNumber9.Click,
        ButtonNumber0.Click, ButtonDot.Click

        Dim buttonSender = CType(sender, Button)
        TextBoxResult.Text += buttonSender.Text

    End Sub

    Private Sub ButtonOperation_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click,
            ButtonSubtract.Click, ButtonMultiply.Click, ButtonDivide.Click

        firstNumber = Val(TextBoxResult.Text)
        TextBoxResult.Text = "0"
        Dim buttonSender = CType(sender, Button)
        arithmeticProcess = buttonSender.Text

    End Sub

    Private Sub ButtonEqual_Click(sender As Object, e As EventArgs) Handles ButtonEqual.Click

        secondNumber = Val(TextBoxResult.Text)
        Select Case arithmeticProcess
            Case "+"
                answerNumber = firstNumber + secondNumber
            Case "-"
                answerNumber = firstNumber - secondNumber
            Case "*"
                answerNumber = firstNumber * secondNumber
            Case "/"
                answerNumber = firstNumber / secondNumber
        End Select
        TextBoxResult.Text = answerNumber.ToString()

    End Sub

End Class

The form:

enter image description here

Han
  • 3,052
  • 2
  • 22
  • 31
  • Great illustration that helps a lot because when I tried to click a Button1(from Form1.vb[Design]) doesn't give the number 1 that cause because I copy the 3 Buttons. I make again all buttons and works. Also, 5 Buttons couldn't work which is the Buttons minus, plus, multiply, divide,equal. The word "doesn't work" means when I press the button16(=) give the wrong answer in every tries +,-,*,/. See an example:I press 2+1=4. – mpes. Sep 07 '18 at 16:24
  • 1
    The addition is not wrong. It's the other signs that are wrong. It always use + when calculating the answer. I have added the correct calculation in my answer. – Han Sep 07 '18 at 18:46
  • And yes thanks so much, it works! just one question why do you replace Byval and System? – mpes. Sep 07 '18 at 21:18
  • You can pass an argument to a procedure by value (ByVal) or by reference (ByRef). The default in Visual Basic is to pass arguments by value. See this [link](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/passing-arguments-by-value-and-by-reference) for details. – Han Sep 07 '18 at 21:27
  • 1
    I omit System in System.Object and System.EventsArgs because it can be simplified by importing the namespace. Let's say I use a class with long namespace, eg. System.Data.SqlClient.SqlCommand. I import the namespace System.Data.SqlClient, then I can just use SqlCommand instead of the long name. Read more about [namespace](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/namespaces). Namespace is used to organize classes. – Han Sep 07 '18 at 21:33