Solved

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

Posted on 2011-02-14
6
347 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 85

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to call weather service by using asmx? 4 30
HttpPostedFile 1 26
VB: Convert 2 dates to specific format 24 48
MVC DDL Json Not Binding to Model 2 13
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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