Solved

Visual Studio C#, making events - coming from VB questions

Posted on 2011-02-14
6
349 Views
Last Modified: 2013-11-26
I know there's a quick way to do this, but coming from  VB, say you have an object with events, you declare it WithEvents module level (I'm talking windows forms/silverlight), it gets listed in the LHS combo above the text window, you select it, then in the RHS combo, you get the events exposed by that object, you click on an event, and you get the stub, how do you do that in C#?
0
Comment
Question by:Silas2
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 34890255
open/pin the properties window --> select your object in the designer --> there is a lightning bolt icon at the top of the properties window -- these are your events for the currently selected object --> double click in the space next to the event you want and it will stub in the code for you.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34890275
properties window
0
 
LVL 4

Expert Comment

by:shrikantss
ID: 34890339
suppose you have a class
public class MyObject
{
   //public fields
   public string myStringField;
   public int myIntField;
   public MyObject myObjectField;

   //public properties
   public string MyStringProperty { get; set; }
   public int MyIntProperty { get; set; }
   public MyObject MyObjectProperty { get; set; }

   //public events
   public event EventHandler MyEvent1;
   public event EventHandler MyEvent2;
}

The .NET class that gives us access to all of this is the Type class. To get a Type object, we simply use the typeof keyword:

Type myObjectType = typeof(MyObject);


To get a list of public fields in an object, we'll use Type's GetField method:

Type myObjectType = typeof(MyObject);

System.Reflection.FieldInfo[] fieldInfo = myObjectType.GetFields();

foreach (System.Reflection.FieldInfo info in fieldInfo)
combobox.items.add((info.Name));


you will list everything including events also
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 250 total points
ID: 34890343
In the case of objects that don't show on your form's visual designer...

Type "object.EventName" (where "object" and "EventName" are applicable values), followed by "+=" and Visual Studio should offer to finish the statement for you if you press Tab, and then offer to add a stub if you press Tab once more.
Untitled1.png
Untitled2.png
Untitled3.png
0
 

Author Closing Comment

by:Silas2
ID: 34890745
Sorry, I did meant to say 'object with no UI'. Doh - I should have checked the intelli sense.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34890853
There is no equivalent of WithEvents/Handles in C# so the two dropdowns across CODE editor screen are not present.

As the others have demonstrated, you either go back to the Form and use the events window in the Properties pane (exact same process as in VB), or manually wire up the vent via code.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question