|
-
Mar 29th, 2023, 10:43 PM
#1
Thread Starter
Addicted Member
Why on earth am I getting this error????
Based on some suggestions I have received from this board, I am rewriting my program using a DataGridView as opposed to an array of buttons. Basically, things are going okay, but the program needs to know if a cell clicked on was clicked with the left or right mouse button.
Based on some searching around, I came upon something that seemed like a reasonable answer. Here is the code (greatly simplified):
Code:
Public Class frmDGVTest
Dim dgTest As New DataGridView
Private Sub frmDGTest_Load(sender As Object, e As EventArgs) Handles Me.Load
dgTest.ColumnCount = 9
dgTest.RowCount = 9
<etc>
End Sub
Private Sub dgTest_CellMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles dgTest.CellMouseDown
If e.Button = MouseButtons.Left Then
<stuff>
End If
End Sub
End Class
The problem is that I get this error:
Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
I simply do not understand this since I essentially copied the code directly. What did I do wrong?
-
Mar 29th, 2023, 10:57 PM
#2
Re: Why on earth am I getting this error????
-
Mar 29th, 2023, 11:23 PM
#3
Re: Why on earth am I getting this error????
I don't want to discourage people from using this forum when they need to but it should be for stuff that you can't work out by other means. If you had simply copied that error message into a search engine then you'd have been able to answer your own question.
Why are you creating the grid in code in the first place though? If you had added the grid to the form in the designer then, as is always the case, the designer would have generated the field and it would have been declared WithEvents. You can look at the designer code file for any form with controls and see that for yourself. My guess is that you copied some code from the internet without really understanding what it's doing. If you want to paste code for a control, it's a good idea to add that control first. Like just about all controls, the default for that should be doing it in the designer. You should only create controls in code if you have a specific reason for doing so.
-
Mar 30th, 2023, 11:25 AM
#4
Thread Starter
Addicted Member
Re: Why on earth am I getting this error????
jmcilhinney - Thank you! I changed the dgTest declarations line to
Code:
Dim WithEvents dgTest As New DataGridView
and it now works just fine.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|