none
I keep having this error with a part of a code

    Question


  •     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    This is the specific line I am having issues with.

    Severity    Code    Description    Project    File    Line    Suppression State
    Error    BC30506    

    Handles clause requires a WithEvents variable defined in the containing type or one of its base types.    WindowsApplication3    c:\users\samie\onedrive\documents\visual studio 2015\Projects\WindowsApplication3\WindowsApplication3\Form1.vb    3    Active

    I am really not sure what to do. Below is the rest of the code.

    Public Class DroneDogs     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click         'Declare base costs/sales tax, as well as all dog types         Dim intNumBeef, intNumPork, intNumTurkey, intNumTotal As Integer         Dim dblSubtotal, dblSalesTax, dblTotalCost As Double         Dim txtTotalCost         Dim txtSubtotal         Dim txtBeefDogs         Dim txtPorkDogs         Dim txtTurkeyDogs         Dim txtSalesTax         Const DBL_DOG_PRICE As Double = 1.99         Const DBL_SALES_TAX As Double = 0.07         'Converts values from text boxes to correct dog types, as well as calculates total dogs         intNumBeef = Convert.ToInt32(txtBeefDogs.Text)         intNumPork = Convert.ToInt32(txtPorkDogs.Text)         intNumTurkey = Convert.ToInt32(txtTurkeyDogs.Text)         intNumTotal = intNumBeef + intNumPork + intNumTurkey         'Calculate Subtotal, Sales Tax, and Total Cost         dblSubtotal = intNumTotal * DBL_DOG_PRICE         dblSalesTax = dblSubtotal * DBL_SALES_TAX         dblTotalCost = dblSubtotal + dblSalesTax         'Push SubTotal, SalesTax, and TotalCost to bottom text boxes         txtSubtotal.Text = dblSubtotal.ToString("c2")         txtSalesTax.Text = dblSalesTax.ToString("c2")         txtTotalCost.Text = dblTotalCost.ToString("c2")     End Sub  

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            'Code for exit

        End Sub
    End Class



    To add I am using Visual Studio 2015
     



    • Edited by SamieDucky Wednesday, April 26, 2017 10:32 PM
    Wednesday, April 26, 2017 6:12 PM

All replies

  • Samie,

    Next time please click on the tool to put your code into a code block.

    *****

    What line is it exactly?

    I see two button click event handler subs - do you mean one of those? They both look to be correct.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, April 26, 2017 8:42 PM
  • Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

    That means that 'Button1' refers to an object that does not support events.  If you created these buttons with code, did you use the 'WithEvents' specifier?  Or, if you created them in the designer have they been deleted or renamed, or has your designer file become corrupted? Or are you trying to write the code before you have laid out the GUI (eg, pasting from another source)?

    Wednesday, April 26, 2017 9:17 PM
  • Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

    That means that 'Button1' refers to an object that does not support events.  If you created these buttons with code, did you use the 'WithEvents' specifier?  Or, if you created them in the designer have they been deleted or renamed, or has your designer file become corrupted? Or are you trying to write the code before you have laid out the GUI (eg, pasting from another source)?

    I'm glad the forum is working again - now we can debate each other. ;-)

    I don't see how the button being deleted or renamed could cause this. Either of those would result in a compile error, not a run-time error - or am I wrong?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, April 26, 2017 9:33 PM
  • Hi,

    please show the code for the definition of the buttons. And: What version of VisualStudio do you use?

    Regards,

      Thorsten

    Wednesday, April 26, 2017 10:00 PM
  • I don't see how the button being deleted or renamed could cause this. Either of those would result in a compile error, not a run-time error - or am I wrong?

    If it's a run time error than that's a completely different situation.   What is there in the OP that makes you suspect the runtime?

    Deleting a control shouldn't cause this error, because the IDE tracks deletions. It depends on how it got deleted.

    Wednesday, April 26, 2017 10:19 PM
  • If it's a run time error than that's a completely different situation.   What is there in the OP that makes you suspect the runtime?

    I've seen it before and I was thinking that it was an exception, but you're right - it's a compile error.

    It's been quite a while and I don't remember how I ended up with it. We'll see. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, April 26, 2017 10:25 PM
  • I posted it in a code block the specific line of code I am having issues with is at the very top of the question

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Wednesday, April 26, 2017 10:29 PM
  • Actually, I *might* remember.

    *****

    Samie,

    Did you at one point have the button in the form and move it to a container control like a panel or groupbox?

    I seem to remember that it was something like that.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, April 26, 2017 10:30 PM
  • I posted it in a code block the specific line of code I am having issues with is at the very top of the question

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    That should work. Is the scope wrong maybe?

    Where's button1 and where's the that line of code?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, April 26, 2017 10:35 PM
  • Yes the code has been pasted from another source. Where do I find where the GUI is?
    Wednesday, April 26, 2017 10:40 PM
  • Yes the code has been pasted from another source. Where do I find where the GUI is?

    In the IDE right click the DroneDogs.vb item in the Solution Explorer, and select View Designer. Make sure that your GUI includes a Button1 and a Button2.

    https://msdn.microsoft.com/en-us/library/dd492132.aspx

    Wednesday, April 26, 2017 10:43 PM
  • Yes the code has been pasted from another source. Where do I find where the GUI is?

    I don't know who you're talking to but do this as a test:

    Comment out the existing code. Double-click the button in the designer (the default event is the click) and let's see where VS puts the handler sub.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, April 26, 2017 10:43 PM