Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the right parameters for an event without using Design Mode in Visual Studio 2010

Is there a way to know what parameters are needed by an event in Visual Studio 2010?

Let's say I have a DropDownList control and I want to bind a method to the "OnSelectedIndexChanged", I would do something like this

In the ASPX File:

<asp:DropDownList ID="lstMyList" runat="server" OnSelectedIndexChanged="lstMyList_SelectedIndexChanged"></asp:DropDownList>

In the codebehind:

protected void lstMyList_SelectedIndexChanged(object sender, EventArgs e) 
{
    ...
}

Is there a way to know what parameters the method needs? (In this case, an object for the sender and an EventArgs parameter for the event.)

I know you can easily create the method by double-clicking the right event in Design Mode, but it does a mess with your code so I prefer not to use it.

Thanks!

like image 769
Jason Avatar asked Dec 27 '10 19:12

Jason


People also ask

How do I find project properties in Visual Studio?

To access properties on the solution, right click the solution node in Solution Explorer and choose Properties.

What is Ctrl F5 in Visual Studio?

F5 & Ctrl-F5 F5 is used to start your project in debug mode and Ctrl-F5 is used to start your project without debug mode.

What does ctrl t'do in Visual Studio?

New! Save questions or answers and organize your favorite content.

How do I see variable values in Visual Studio debugging?

Hover over a variable to see its value. The most commonly used way to look at variables is the DataTip. When stopped in the debugger hover the mouse cursor over the variable you want to look at. The DataTip will appear showing you the value of that variable.


2 Answers

You can find out the parameters by "going to definition" (F12) on the appropriate event, finding out what delegate type it uses, then going to definition on that. In this case the SelectedIndexChanged event has type EventHandler which is defined as follows:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public delegate void EventHandler(
    Object sender,
    EventArgs e
)

You can also find this information by searching the web or pressing F1 and searching in the help.


I know you can easily create the method by double-clicking the right event in Design Mode, but it does a mess with your code so I prefer not to use it.

I think you should try to overcome your fear of using the designer. You are most likely wasting more time in lost productivity by not using the code generation features in Visual Studio than the potential time you might have saved by protecting yourself against the designer messing up your code.

like image 117
Mark Byers Avatar answered Oct 28 '22 00:10

Mark Byers


Hardly economical in terms of keystrokes and productivity but a possibility, if you're finding the event in code to 'Go To Definition' anyway, and imagining for a moment that you don't mind using the code editor features of VS, too, is to hit the Tab key, then add the += before hitting the Tab key another two times. This will generate the appropriate delegation and method definition, which will result in you seeing the method signature.

For instance, typing the following, tabbing appropriately (twice after typing '+='):

MyType.MyEvent += new System.EventHandler(MyType_MyEvent);

Generates a method like the following:

void MyType_MyEvent(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

Now, the productivity reduction could occur if you don't actually require both pieces of code.

like image 29
Grant Thomas Avatar answered Oct 27 '22 23:10

Grant Thomas