Doug Null
Doug Null

Reputation: 8327

vb.net: how to define a UI control's event handler sub in a module, and not in the form with the UI control

I want my user interface event handler to be in a module, and not in my user interface form.

For example, I added a button to my form. At design time, I double clicked it and then moved the resulting handler sub to a module file.

But now a red squiggly underlines "_MainForm.Button_settings_account_save.Click" in the handler sub and shows this error:
'Handles' in modules must specify a 'WithEvents' variable qualified with a single identifier

    Private Sub Button_settings_account_save_Click(sender As Object, e As EventArgs) Handles _MainForm.Button_settings_account_save.Click   
          stop
End Sub

Upvotes: 0

Views: 138

Answers (1)

Steve
Steve

Reputation: 216313

You can use the AddHandler approach instead of letting the WinForms designer add the Handles to your button click method.

In the form constructor, after the call to InitialzeComponents or in the Form_Load event handler you could add

AddHandler Button_settings_account_save.Click, AddressOf YourModuleName.YourEventHandlerMethod

Upvotes: 4

Related Questions