Solved

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

Posted on 2011-02-14
6
348 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

766 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