none
I NEED HELP!!! RRS feed

  • Question

  • Hello, their fellow coders I have a bit of a problem you see the Bolded and underlined bit of code says Handles clause requires a WithEvents variable defined in the containing type or one of its base types. If someone could help me that would be greatly appreciated 

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim <g class="gr_ gr_37 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="37" id="37">Therandom</g> As New Random
            If RadioButton1.Checked = True Then
                Label1.Visible = False
                Label2.Visible = False
                Label3.Visible = False
                Label1.Text = Therandom.Next(1, 10)
                If TextBox1.Text = Label1.Text Then
                    Label1.Visible = True
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You Are Correct!"
                    Label3.ForeColor = Color.Green
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                Else
                    Label1.Visible = True
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You Lose!"
                    Label3.ForeColor = Color.Red
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                End If
            ElseIf RadioButton2.Checked = True Then
                Label1.Visible = False
                Label2.Visible = False
                Label3.Visible = False
                Label1.Text = Therandom.Next(1, 50)
                If TextBox1.Text = Label1.Text Then
                    Label1.Visible = True
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You Are Correct!"
                    Label3.ForeColor = Color.Green
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                Else
                    Label1.Visible = True
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You Lose!"
                    Label3.ForeColor = Color.Red
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                End If
            ElseIf RadioButton3.Checked = True Then
                Label1.Visible = False
                Label2.Visible = False
                Label3.Visible = False
                Label1.Text = Therandom.Next(1, 100)
                If TextBox1.Text = Label1.Text Then
                    Label1.Visible = True
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You Are Correct!"
                    Label3.ForeColor = Color.Green
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                Else
                    Label1.Visible = True
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You Lose!"
                    Label3.ForeColor = Color.Red
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                End If

            ElseIf RadioButton4.Checked = True Then
                Label1.Visible = False
                Label2.Visible = False
                Label3.Visible = False
                Label1.Text = Therandom.Next(1, 1000)
                If TextBox1.Text = Label1.Text Then
                    Label1.Visible = True
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You Are Correct!"
                    Label3.ForeColor = Color.Green
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                Else
                    Label1.Visible = True
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You Lose!"
                    Label3.ForeColor = Color.Red
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                End If
            End If
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            TextBox1.Enabled = True
            Button1.Enabled = True
        End Sub

    End Class

    Tuesday, November 28, 2017 10:22 PM

All replies

  • I have a bit of a problem you see the Bolded and underlined bit of code says Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

    Button2 has been created without events.  You need to review the code that creates Button2 (it might be in the designer) and ensure that it is created with events.
    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/withevents

    Your message title is not very descriptive.   If you want prompt responses you should always indicate the topic in the title.

    Tuesday, November 28, 2017 10:28 PM
  • Hi

    Is there a Button2 on the Form?

    If so,then in the Designer, double click on it and see if it opens the code window at/in that Sub you have - or,maybe it will open the code window with a new empty Button2.Click Handler.

    If it opens a new Handler, then copy your code from the bad one to the new one and delete the whole of the old one. Rebuild and try running again.

    *

    Also, if still having issues you will need to say how you got the 'old' handler - was it from the Designer -or what? Is that some code just copied/pasted from somewhere else?


    Regards Les, Livingston, Scotland

    Tuesday, November 28, 2017 10:29 PM
  • Miles,

    In your project, at the top of Solution Explorer, you should see a glyph to indicate whether or not to show all files:

    Click that and let it show all files then look for your form's name ".designer.vb". For example on mine it's "Form1.designer.vb".

    When you find it, open it in code view and look around carefully. You should see "Button2" defined with a number of properties (like the size, location, text, etc.) and eventually you should see this shown:

    Friend WithEvents Button2 As System.Windows.Forms.Button

    If you don't then that's why you're getting the compile error - we'll explore that part once you confirm whether or not it's there.


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

    Tuesday, November 28, 2017 11:38 PM
  • Hi Miles,

    I guess you add Button2 control in the form, and then you want to add Button2.click event, am I right?

    If yes, you don't need to handles Button2.click, you just need to add this handles, like this:

    Private Sub Form14_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim Button6 As New Button
            AddHandler Button6.Click, AddressOf Button6_Click
            Me.Controls.Add(Button6)
    
        End Sub
        Private Sub Button6_Click(sender As Object, e As EventArgs)
            MessageBox.Show("OK")
        End Sub

    If not, the WithEvents keyword allows the control to hook up events, using the Handles syntax, with its owner. When adding components in the designer it automatically generates this for you but when creating them yourself you must include the WithEvents if you intend to use events with the component.

    Add WithEvents in .designer.

     Friend WithEvents Button6 As Button

    About WithEvent, please refer to :

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

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, November 29, 2017 2:32 AM
    Moderator