Visual Studio .NET WithEvents Variable Definied?

Hello guys,
I am currently working on a bot for Guitar Hero by using an Arduino Uno. However, I have a small problem with the Visual Studio code. I am getting the following error and don't know how to solve it:

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

And here is the entire Visual Studio code:

Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
Timer2.Start()
Timer3.Start()
Timer4.Start()
Timer5.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim BMP As New Drawing.Bitmap(1, 1)
Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
GFX.CopyFromScreen(New Drawing.Point(698, 839), _
New Drawing.Point(0, 0), BMP.Size)
Dim Pixel As Drawing.Color = BMP.GetPixel(0, 0)
CPpanel.BackColor = Pixel
Redtxt.Text = Pixel.R
Greentxt.Text = Pixel.G
Bluetxt.Text = Pixel.B
TextBox1.Text = Pixel.R & Pixel.G & Pixel.B
If Greentxt.Text > 100 Then
TextBox2.Text = "ja"
Else
TextBox2.Text = "nee"
End If
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Dim BMP2 As New Drawing.Bitmap(1, 1)
Dim GFX2 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP2)
GFX2.CopyFromScreen(New Drawing.Point(824, 839), _
New Drawing.Point(0, 0), BMP2.Size)
Dim Pixel2 As Drawing.Color = BMP2.GetPixel(0, 0)
CPpanel2.BackColor = Pixel2
Redtxt2.Text = Pixel2.R
Greentxt2.Text = Pixel2.G
Bluetxt2.Text = Pixel2.B
TextBox6.Text = Pixel2.R & Pixel2.G & Pixel2.B
If Redtxt2.Text > 100 Then
TextBox5.Text = "ja"
Else
TextBox5.Text = "nee"
End If
End Sub

Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Dim BMP3 As New Drawing.Bitmap(1, 1)
Dim GFX3 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP3)
GFX3.CopyFromScreen(New Drawing.Point(954, 839), _
New Drawing.Point(0, 0), BMP3.Size)
Dim Pixel3 As Drawing.Color = BMP3.GetPixel(0, 0)
CPpanel3.BackColor = Pixel3
Redtxt3.Text = Pixel3.R
Greentxt3.Text = Pixel3.G
Bluetxt3.Text = Pixel3.B
TextBox11.Text = Pixel3.R & Pixel3.G & Pixel3.B
If (Redtxt3.Text > 100 And Greentxt3.Text > 100) And Bluetxt3.Text < 100 Then
TextBox10.Text = "ja"
Else
TextBox10.Text = "nee"
End If
End Sub

Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
Dim BMP4 As New Drawing.Bitmap(1, 1)
Dim GFX4 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP4)
GFX4.CopyFromScreen(New Drawing.Point(1079, 839), _
New Drawing.Point(0, 0), BMP4.Size)
Dim Pixel4 As Drawing.Color = BMP4.GetPixel(0, 0)
CPpanel4.BackColor = Pixel4
Redtxt4.Text = Pixel4.R
Greentxt4.Text = Pixel4.G
Bluetxt4.Text = Pixel4.B
TextBox16.Text = Pixel4.R & Pixel4.G & Pixel4.B
If (Bluetxt4.Text > 100 And Redtxt4.Text < 180) Or ((Bluetxt4.Text > 200 And Redtxt4.Text > 200) And Greentxt4.Text > 200) Then
TextBox15.Text = "ja"
Else
TextBox15.Text = "nee"
End If
End Sub

Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
Dim BMP5 As New Drawing.Bitmap(1, 1)
Dim GFX5 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP5)
GFX5.CopyFromScreen(New Drawing.Point(1203, 839), _
New Drawing.Point(0, 0), BMP5.Size)
Dim Pixel5 As Drawing.Color = BMP5.GetPixel(0, 0)
CPpanel5.BackColor = Pixel5
Redtxt5.Text = Pixel5.R
Greentxt5.Text = Pixel5.G
Bluetxt5.Text = Pixel5.B
TextBox21.Text = Pixel5.R & Pixel5.G & Pixel5.B
If Redtxt5.Text > 150 And Greentxt5.Text > 150 Then
TextBox20.Text = "ja"
Else
TextBox20.Text = "nee"
End If

TextBox3.Text = MousePosition.X : TextBox4.Text = MousePosition.Y
End Sub

Private Sub btnOpenCOMPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenCOMPort.Click
If TextBox2.Text = "ja" Then
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
SerialPort1.PortName = "COM4"
SerialPort1.Open()
SerialPort1.Write("?")
Else
If SerialPort1.IsOpen Then
SerialPort1.DtrEnable = True
SerialPort1.DtrEnable = False
SerialPort1.Close()
End If
End If
End Sub

Private Sub ArduinoSend_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles ArduinoSend.DoWork
Dim data As Byte = 0

If Not (e.Argument.ToString = "") Then
If e.Argument.ToString.Contains("G") Then
data = data + &H40
End If
If e.Argument.ToString.Contains("R") Then
data = data + &H20
End If
If e.Argument.ToString.Contains("Y") Then
data = data + &H10
End If
If e.Argument.ToString.Contains("B") Then
data = data + &H8
End If
If e.Argument.ToString.Contains("O") Then
data = data + &H4
End If
data = data + &H2
End If
If SerialPort1.IsOpen Then
SerialPort1.Write(ChrW(data))
End If
End Sub

Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text = "ja" Then btnOpenCOMPort.PerformClick()
End Sub

End Class

This isn't really the right place to be asking for help with VB. Did you try googling for the error? The solution is there.

http://www.google.co.uk/search?q=Handles+clause+requires+a+WithEvents+variable+defined+in+the+containing+type+or+one+of+its+base+types

I am currently working on a bot for Guitar Hero by using an Arduino Uno.

Why?

dxw00d:
This isn't really the right place to be asking for help with VB. Did you try googling for the error? The solution is there.

http://www.google.co.uk/search?q=Handles+clause+requires+a+WithEvents+variable+defined+in+the+containing+type+or+one+of+its+base+types

I am currently working on a bot for Guitar Hero by using an Arduino Uno.

Why?

I have been searching on Google obviously, but I still can't figure out how to fix the error as nothing I try seems to work. Secondly, I am making the bot for a school project for information science. It's not to abuse leaderbords or something and it doesn't even need to be able to succeed 100% on Expert, it's just to see if I am able to build something like that as it's very challenging, but yet interesting and awesome project.

Just wondered. Sounds like an interesting project.

You might do better asking in the 'Interfacing with software on a computer' forum, if you haven't already. There may be VB experts there.

http://arduino.cc/forum/index.php/board,12.0.html

dxw00d:
Just wondered. Sounds like an interesting project.

You might do better asking in the 'Interfacing with software on a computer' forum, if you haven't already. There may be VB experts there.

http://arduino.cc/forum/index.php/board,12.0.html

Thanks for the help, I will try it there as well :smiley:

I have made some changes and got rid of the ArduinoSend.DoWork. Instead I am now using the following code to send a 'value' to the Arduino:

Private Sub btnOpenCOMPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenCOMPort.Click
If TextBox2.Text = "ja" Or TextBox5.Text = "ja" Or TextBox10.Text = "ja" Or TextBox15.Text = "ja" Or TextBox20.Text = "ja" Then

If SerialPort1.IsOpen Then
SerialPort1.Close()
End If

SerialPort1.PortName = "COM4"
SerialPort1.Open()
If TextBox2.Text = "ja" Then SerialPort1.Write("1")
If TextBox5.Text = "ja" Then SerialPort1.Write("2")
If TextBox10.Text = "ja" Then SerialPort1.Write("4")
If TextBox15.Text = "ja" Then SerialPort1.Write("8")
If TextBox20.Text = "ja" Then SerialPort1.Write("9")

End If
End Sub

Private Sub TextBox5_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox5.TextChanged
If TextBox5.Text = "ja" Then btnOpenCOMPort.PerformClick()
End Sub

Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text = "ja" Then btnOpenCOMPort.PerformClick()
End Sub

Private Sub TextBox10_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox10.TextChanged
If TextBox10.Text = "ja" Then btnOpenCOMPort.PerformClick()
End Sub

Private Sub TextBox15_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox15.TextChanged
If TextBox15.Text = "ja" Then btnOpenCOMPort.PerformClick()
End Sub

Private Sub TextBox20_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox20.TextChanged
If TextBox20.Text = "ja" Then btnOpenCOMPort.PerformClick()
End Sub

And here is the code I use in the Arduino:

void setup() {
Serial.begin(9600); // set serial speed
pinMode(13, OUTPUT); // set LED as output
pinMode(12, OUTPUT); // set LED as output
pinMode(11, OUTPUT); // set LED as output
pinMode(10, OUTPUT); // set LED as output
pinMode(9, OUTPUT); // set LED as output
pinMode(8, OUTPUT); // set LED as output
pinMode(3, OUTPUT); // set LED as output

}

void loop(){
while (Serial.available() == 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number

if (val == 1) { // test for command 1 then turn on LED
digitalWrite(13, HIGH); // turn on LED
delay(10);
digitalWrite(13, LOW);
}
if (val == 2) { // test for command 1 then turn on LED
digitalWrite(12, HIGH); // turn on LED
delay(10);
digitalWrite(12, LOW);
}
if (val == 4) { // test for command 1 then turn on LED
digitalWrite(11, HIGH); // turn on LED
digitalWrite(10, HIGH);
delay(10);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
if (val == 8) { // test for command 1 then turn on LED
digitalWrite(9, HIGH); // turn on LED
delay(10);
digitalWrite(9, LOW);
}
if (val == 9) { // test for command 1 then turn on LED
digitalWrite(8, HIGH); // turn on LED
analogWrite (3, 80);
delay(10);
digitalWrite(8, LOW);
digitalWrite(3,LOW);
}

}

Everything works fine now, until 2 notes come on the same time (for example val == 1 and val == 2 together at the same time). The Arduino will only light up 1 LED instead of both LEDs. The Arduino seems to randomly pick which one he lights up. I've been trying a couple of different methods as in combining the vals and have a seperate TextBox in Visual Basic that sends for example "7" to the Arduino but I can't get it to work. Any ideas?

The way you have it written, each time through loop(), the Arduino will only pull one byte from Serial. If you want to send two or more notes simultaneously, it would be better to encode them into a single byte, using a bit for each note, perhaps using the bitread() function.

Use code tags, rather than quote tags, then you won't get smileys appearing.