none
Handles clause requires a WithEvents variable defined in the containing type or one of its base types RRS feed

  • Question

  • I created a lable to handle DateTime = Now, It works fine. but when I moved it inside a FormView I Get the following Messege.  Handles clause requires a WithEvents variable defined in the containing type or one of its base types
    Sunday, August 23, 2009 2:07 PM

Answers

  • I created a lable to handle DateTime = Now, It works fine. but when I moved it inside a FormView I Get the following Messege.  Handles clause requires a WithEvents variable defined in the containing type or one of its base types

    Hi Jamal,

    Have you written code for a custom control based on a LABEL?

    If so please post the code here and we can check it for you.

    You could;

    1) Try running your code if it includes any custom controls.

    2) If the code is successful then it will be added to our ToolBox, you will
     notice them added to the ToolBox when you stop running the program.


    Unless you have made some bad error in your code or deleted something you shouldn't have then the message;

    Handles clause requires a WithEvents variable defined in the containing type or on of its base types.

    ....means that the container ( usually a FORM ) will have a line such as.>>

    Friend WithEvents Label1 As Label

    Label1 = New Label

    'Other code that defines the LABEL after it is created followed by.>>

    Me.Controls.Add(Label1)


    This is normally done for you by the designer and the code is added to the

    Form1.Designer.Vb

    file or for whichever container the control is in.


    ++++++++++++++++++++++++++++++++++++++++++++++++

    If you want to add a control at run time ( instead ) you can do it like this.>>

    Option Strict On
    
    Public Class Form1
    
        Friend WithEvents Label1 As Label
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Label1 = New Label
            Label1.Location = New Point(20, 30)
            Label1.MinimumSize = New Size(100, 25)
            Label1.Text = "Hi Jamal!! :-)"
            Me.Controls.Add(Label1)
    
        End Sub
    End Class

    Warning: Do not manually edit the Form1.Designer.Vb file or any other Designer.Vb file
     if you do not know what you are doing.

    You can open the files to see them if you select the SHOW ALL FILES icon
     in the SOLUTION EXPLORER window.


    Press CTRL + ALT + L or CTRL + R or select SOLUTION EXPLORER from the VIEW menu.


    >>





    You can view the file by double-clicking on it. :-)


    Regards,

    John
    Monday, August 24, 2009 3:51 AM

All replies

  • Whichever control the Handles clause refers to must be defined with the WithEvents clause.
    Sunday, August 23, 2009 2:11 PM
  • Can Some one write an example code Please
    Sunday, August 23, 2009 10:43 PM
  • Your example would be the most appropriate as it generates the error message.  Remove the proprietary elements form your code and post the code applicable to the problem using the code formatter.
    Sunday, August 23, 2009 10:47 PM
  • I created a lable to handle DateTime = Now, It works fine. but when I moved it inside a FormView I Get the following Messege.  Handles clause requires a WithEvents variable defined in the containing type or one of its base types

    Hi Jamal,

    Have you written code for a custom control based on a LABEL?

    If so please post the code here and we can check it for you.

    You could;

    1) Try running your code if it includes any custom controls.

    2) If the code is successful then it will be added to our ToolBox, you will
     notice them added to the ToolBox when you stop running the program.


    Unless you have made some bad error in your code or deleted something you shouldn't have then the message;

    Handles clause requires a WithEvents variable defined in the containing type or on of its base types.

    ....means that the container ( usually a FORM ) will have a line such as.>>

    Friend WithEvents Label1 As Label

    Label1 = New Label

    'Other code that defines the LABEL after it is created followed by.>>

    Me.Controls.Add(Label1)


    This is normally done for you by the designer and the code is added to the

    Form1.Designer.Vb

    file or for whichever container the control is in.


    ++++++++++++++++++++++++++++++++++++++++++++++++

    If you want to add a control at run time ( instead ) you can do it like this.>>

    Option Strict On
    
    Public Class Form1
    
        Friend WithEvents Label1 As Label
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Label1 = New Label
            Label1.Location = New Point(20, 30)
            Label1.MinimumSize = New Size(100, 25)
            Label1.Text = "Hi Jamal!! :-)"
            Me.Controls.Add(Label1)
    
        End Sub
    End Class

    Warning: Do not manually edit the Form1.Designer.Vb file or any other Designer.Vb file
     if you do not know what you are doing.

    You can open the files to see them if you select the SHOW ALL FILES icon
     in the SOLUTION EXPLORER window.


    Press CTRL + ALT + L or CTRL + R or select SOLUTION EXPLORER from the VIEW menu.


    >>





    You can view the file by double-clicking on it. :-)


    Regards,

    John
    Monday, August 24, 2009 3:51 AM
  • Thank you John Anthony Oliver, My Problem has been solved 
    Tuesday, August 25, 2009 12:54 AM