What does “WithEvents variable” mean in VB?
Clash Royale CLAN TAG#URR8PPP
What does “WithEvents variable” mean in VB?
I use Visual Studio using language: VB in the case to run my code I come up with this errors:
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
Did you import a form and not include the designer.vb file?
– Parrish Husband
4 hours ago
@Han thanks for the link is very interesting but it shows this error: "Expression expected." "WithEvents' variables cannot be typed as arrays."
– mpes.
3 hours ago
@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.
3 hours ago
That code desperately need
Option Strict On
at the top of the file– Plutonix
1 hour ago
Option Strict On
1 Answer
1
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.
Add
Existing Item
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):
'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.
'TextBox1' is not declared
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
Handles Button1.Click
Go back to the designer. Drag the button and textbox (see red box) from the toolbox into the form. Place them anywhere you like.
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.
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.
1
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
See this link.
– Han
4 hours ago