Resolved Controls and Events

RobertbNZ

Active member
Joined
Jun 26, 2020
Messages
37
Programming Experience
Beginner
I'm developing C# skills after years of working with VB.NET. I am having difficulty finding out what events are available with a control, and how to relate an event handler to the event that I want to use.

In VB I'd click a control on a form and the default event handler would be added. For example, click on a textbox and this is added
VB.NET:
    Private Sub txt1ry_TextChanged_1(sender As Object, e As EventArgs) Handles txt1ry.TextChanged

    End Sub
Of course exactly the same happens with C#
C#:
        private void txtLastName_TextChanged(object sender, EventArgs e)
        {
 
        }
The C# code even looks a bit neater. But what if I want another event type? With VB.NET I could click anywhere in the event handler and Visual studio would show me a list of all the event handlers that are available for this control
1597895192050.png


I can click one of these to insert another event handler for the control, or (as you can see from the code fragment in the image) I could change the Handles clause to switch my original TextChanged code to a different event.

I'm having difficulty doing something similar with C#. If I click on the event handler for txtLastName_TextChanged, I see this, which does not give me the list of events relevant to txtLastName that I found so convenient in VB.NET.
1597896063188.png


Also, I find that if I delete the unwanted event handlers like txtPhoneo_TextChanged my form won't compile any more. If I add an event handler like txtLastName_Validated it is ignored, it is not automatically hooked up. Even when I do know the event names, I don't know how to hook it up.

So what don't I understand? I'm guessing that there are simple techniques, or options in Visual Studio that I haven't set up correctly, that will make C# as convenient as VB.NET.

Thank you, Robert Barnes.
 
The second option you mention, i.e. the navigation bar above the code window, cannot be used to generate event handlers in C#. It works in VB by finding fields declared WithEvents and there is no such thing or equivalent in C#. VB has a third option for creating event handlers for controls and the same option exists in C#. That option is the Properties window in the designer. The toolbar in that window has buttons that will switch between properties and events. You simply click the Events button and you can then double-click an event to generate an event handler or you can select an existing event from the drop-down. In both VB and C#, this option also allows you to generate or select an event handler for multiple controls at the same time, which cannot be done automatically any other way.

Deleting event handlers is done in the Properties window too. Because C# has no equivalent to WithEvents and Handles, you can't simply delete a method with a Handles clause to detach an event handler. C# uses the += operator to attach event handlers, which works in much the same way as the AddHandler keyword in VB. When the designer generates an event handler, a line of code that registers the event handler is added to the designer code file. If you simply delete the method, that line remains and thus refers to a method that no longer exists, hence the compilation error. Just as you do to clear a property value, you can clear an event handler in the Properties window by right-clicking it and selecting Reset. Doing that will only remove the designer code though, so you do still need to delete the method itself for yourself.
 
Aha! Thanks John for a very quick, simple, and correct answer! It's very simple now I know how. Afterthought: creating a new event handler for a control is now very simple, but deleting it (when I discovered that it wasn't the one I wanted) is NOT simple. I thought that simply by using right-click / Reset I'd be able to delete the equivalent of the VB HANDLES clause, and then I could delete the event handler code. Not so! I'm still searching. I'll come back to it tomorrow.
 
Last edited:
I thought that simply by using right-click / Reset I'd be able to delete the equivalent of the VB HANDLES clause
That would be hard, given that there is no equivalent. In VB, a Handles clause is associated with a field declared WithEvents. The event handler always handles the specified event for the object assigned to that field. Change the value of the field and the method is now handling the event for a different object. There is no equivalent to that in C#. The VB AddHandler statement associates an event handler with a specific object and the C# += operator does the same thing.

Let's say that, in VB, you add a Label to a form and then add a handler for the MouseClick event via the Properties window. In the designer code file, you get this:
VB.NET:
Friend WithEvents Label1 As Label
and this:
VB.NET:
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(0, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label1"
and in the user code file you get this:
VB.NET:
Private Sub Label1_MouseClick(sender As Object, e As MouseEventArgs) Handles Label1.MouseClick

End Sub
If you do the same thing in C#, you get this:
C#:
private System.Windows.Forms.Label label1;
this:
C#:
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.label1_MouseClick);
and this:
C#:
private void label1_MouseClick(object sender, MouseEventArgs e)
{

}
As you can see, no Handles clause in C# but a registration in the designer code. After doing that, I went into the Properties window in both projects, right-clicked the MouseClick event and selected Reset. In both cases, the method was deleted from the user code file and the registration was deleted from the designer code file in C# too.

I tried the same thing again but, this time, I added a call to MessageBox.Show in the event handlers. In that case, the registration in the designer code file was removed in C# and the Handles clause was removed in VB but neither event handler was deleted. I reattached the event handlers using the drop-downs in the Properties windows, removed the method calls from the event handlers and then detached the event handlers using Reset again. This time, the whole method was deleted in C# while only the Handles was deleted in VB. This was all in VS 2019.
 
Thanks John. I've sorted out why it wasn't working for me: I was looking at the properties and clicking Reset WHILE THE PROJECT WAS RUNNING. Duh!!!!
 
Back
Top Bottom