In the Visual Studio form designer, you can add an event handler on the Properties window in the "Events" list by double-clicking it. You can also add an event handler — at least in VB.NET — in the code view by selecting the associated "Events" item in the left-hand drop-down and then the desired event in the right-hand drop-down. My question is: how is it that some events that are only available via the latter technique and not the former? For example, the HandleCreated
event is available in the code view:
But not in the designer:
This is fine in VB.NET because I can always use the first technique to quickly generate the event handlers. However, in C#, the first technique is not possible, yet the problem still exists; that is, some events are not present in the designer list in the Properties window. The only other way I know of creating the event handler is to manually add it, which includes manually wiring up the event handler.
Is there something technical that makes it so that some events are missing from the designer Events list in the Properties window? Given that that is true, how can I quickly generate event handlers in C# like I can in VB.NET?
In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.
From the Class Name drop-down list at the top of the Code Editor, select the object that you want to create an event handler for. From the Method Name drop-down list at the top of the Code Editor, select the event. Visual Studio creates the event handler and moves the insertion point to the newly created event handler.
Event Handling Using Controls All ASP.NET controls are implemented as classes, and they have events which are fired when a user performs a certain action on them. For example, when a user clicks a button the 'Click' event is generated. For handling events, there are in-built attributes and event handlers.
Well, it is hidden intentionally. If you look at Control class, HandleCreated
event is marked with Browsable(false)
which means not to show it in properties window.
[Browsable(false)] [EditorBrowsable(EditorBrowsableState.Advanced)] public event EventHandler HandleCreated;
If you ask me why? I don't know the answer. It is a design decision, which any of the person from design team have to answer that.
My guess is that they hide events which are not of more importance. How many times you need to subscribe HandleCreated
? You typically subscribe Load
event.
Some other good examples of events which falls under same category are ControlAdded
, ControlRemoved
etc. As you can see these are not very much important, they are hidden from designer.
How to easily subscribe the event in C# editor as like VB.net?
You cannot get the combobox for events as you get for VB.net; Closest what you can get is the ide support which can autocomplete for you (which is already mentioned in @HenkHolterman's answer; now deleted).
type this.HandleCreated +=
, then tab, tab.
Ide will hook up the events for you.
The VB.NET and the C# IDEs only look superficially the same. They worked from a pretty decent look-and-feel specification. But that's where the resemblance stops, they were created by two very different groups at Microsoft and have drastically different code-bases. Otherwise a survival strategy for large software companies, big groups don't work.
Most importantly, they had dramatically different goals. The C# team had the luxury of starting completely from scratch, always nice when you don't have anybody to keep happy and have no baggage to lug around. Not so for the VB.NET team, Visual Basic has been a popular product for a very long time with strong IDE support that goes back 25 years. Most important for them was to give their customers a familiar experience back, VB.NET was already a major upheaval that was very controversial.
The way those two combo-boxes at the top of the edit window work was cast in granite bedrock stone. They never filtered anything before. If the VB.NET programmer wrote an event handler for an event then he expects to always find it back in the combobox. It works the other way around as well, the VB.NET IDE hides a lot of information. Like not showing the auto-generated source files in the Solution Explorer window.
Don't make too many assumptions about how it should work, you're likely to guess wrong. And have few options to do something about it.
If 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