Results 1 to 4 of 4

Thread: Why on earth am I getting this error????

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    Question 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?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Why on earth am I getting this error????

    The error is telling you exactly what is wrong.

    https://learn.microsoft.com/en-us/do...ers/withevents

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    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
  •  



Click Here to Expand Forum to Full Width