In order to make my program more elegant and better organized in concrete case I would like to change DataGridView1variable
with referenced variable on top of my Form1
class
Private aDgv As DataGridView
And assign value in Form1_Load
aDgv = DataGridView1
After that I can use aDgv
variable across that Form
.
Except in such case:
Private Sub aDgv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles aDgv.KeyDown
aDgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'etc...
End Sub
Where I get an error:
Handles clause requires a
WithEvents
variable defined in the containing type or one of its base types. AndaDgv
variable afterHandles
clause is blue underlined.
What to do to get rid of error and get Handles aDgv.SomeEvent
to work?
Of course, with referenced aDgv
instead of original control name DataGridView1
.
The minimal answer is to add WithEvents
to aDgv
:
Private WithEvents aDgv As DataGridView
The answer of Mark Hurd also works for me. But here's a detailed way on how to do that for beginners like I am.
designer.vb
WithEvents
word after the Friend
wordIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With