Solved

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

Posted on 2011-02-14
6
346 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
Comment Utility
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
Comment Utility
properties window
0
 
LVL 4

Expert Comment

by:shrikantss
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now