Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Custom Controls losing Event Handlers and WithEvents
 

Custom Controls losing Event Handlers and WithEvents

I've created various custom controls that inherit from the regular windows controls (textbox, combobox, datagrid), but provide a little extra functionality.  I have added my custom controls to the toolbox so when I am in the designer, I can just drag and drop them onto my forms.  The problem is when I do add them to my form, the designer generates the code for them (in the InitializeComponent Sub) without adding the "WithEvents" clause in the control declaration statement (Friend CustomControl1 as CustomControl).  So I have to go into the code and add the clause myself.  Then I create a Sub to handle the various events of the control.  The next time I change anything on the form in the designer, the InitializeComponent sub is regenerated, thus, I lose my WithEvents clause again and the Handles clause is dropped from my Sub.  My question is how do you force the WithEvents clause to generate when the designer writes the code for your custom controls?

Thanks,
--Patrick
MigrationUser 1  Thursday, June 24, 2004 11:43 AM
not sure how things work in VB - I think I understand the problem you are having, and what I always do is put a method at the bottom of my initializeForm, and this method does any manual tasks I need to do in InitializeComponent - otherwizer the code generator will possibly wipe out my code

hth
MigrationUser 1  Thursday, June 24, 2004 1:50 PM
I've found after weeks of looking into this off and on that my WithEvents cause does not go away when I declare a control's class directly under a namespace.

For example:

Namespace Test.Controls
    Class Control1
       ...
    End Class
End Namespace

This control will not lose the WithEvents or Handles clauses when added to the screen.

However, in this example:

Namespace Test
    Class Controls
        Class Control1
        ...
        End Class

     Public Shared Function SomeFunction() as Boolean
     ...
    End Class
End Namespace

The declaration for Control1 will not have an WithEvents clause when added to screen through the toolbox.  If you manually add it, the clause will be deleted when any changes are made to the design of the form.

Does this sound right to anyone?  Can anyone explain?

Thanks,
--Patrick
MigrationUser 1  Tuesday, August 03, 2004 5:13 PM

You can use google to search for other answers

Custom Search

More Threads

• Unable to run a VB 2008 program after changes are made without being forced to save it.
• TABCONTROL AND CALCMANAGER
• Designer resizing control automatically
• List<string> Can not be edited?
• can web user control be placed any where on a webform
• Host Designer Questions
• Suppress components to be shown in toolbox
• Regionmaster Controls updated
• Changing font of ErrorProvider and DataGridViewRow.ErrorText
• How to make a property not show up in propertyGrid