Problem with Handles

Baldomero

New member
Joined
Oct 16, 2023
Messages
3
Programming Experience
1-3
I'm beginning an app in VB with the IDE of Visual Studio 2019 in Window 11 and I get an error message that never had appeared to me. It is in Form1.
VB.NET:
Imports System.Data.OleDb

Public Class Form1

    Private conexion As New OleDbConnection("Provider=Microsoft,ACE.OleDb.12.0.; Data Source=C:\users\PC GAMING\Documents\inversiones.accdb")

    Private Sub BtnConectar_Click(sender As Object, e As EventArgs) Handles BtnConectar.Click
When I execute , I get the link woth the BD but also I get the following error message:
"The clause Handles requires a variable withEvents definite of the container type o in one of its base types."
Wwhat can I do? Thank you.
 
Please don't post unformatted code snippets. They are too hard to read. I'm sure that you've used a text editor before and this one is just like any other, with buttons on the tool bar for formatting.
 
It means that you have a method that is declared to handle an event of a field named BtnConectar but there is no such field declared WithEvents in that class. Either get rid of that event handler or make sure that it's handling an event of something that exist.
 
Thank you. I'll try.

I don't know if what I have done follows your advise. I have written before the line that demanded withEvents:
Private withEvents BtnConectar As Button()
Private Sub BtnConectar_Click(sender As..., e As EventArgs) BtnConectar.Click.
And it has worked OK
But then I have started the same app wth a different name and it has worked perfectly without this "withEvents". Misteries of IT. Thank you.
 
What would be the point of that? The user isn't going to see a Button on the form if you just declare a field in your code like that and surely that's the point. Get rid of that declaration and actually add a Button to your form in the designer with that name. The two will be linked automatically via that Handles clause. If you open the designer code file then you'll be able to see the field declaration with WithEvents and where the Button is configured and added to the form. To access the designer code file, click the Show All Files button at the top of the Solution Explorer and then expand the node for your form.

I don't know how you ended up with an orphan Handles clause in the first place. Maybe it was a quirk of VS but it usually happens because people copy code from the internet without understanding what it does or reading the instructions with it that tell them to add a control to their form. What you should have done was added your Button to the form first, then double-clicked it to generate the event handler with the appropriate Handles clause, then put the code in the event handler, either by typing or by copying and pasting the method body only.
 

Latest posts

Back
Top