سؤال

I just started learning C#, and I was wondering if it was possible to
change the 'name' of a Void (If that's the same as a Sub in VB.NET)? I know some basic things about VB.NET,
and this is the code for the button click in VB.NET:

Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click  

End Sub

When I double click a button to add code in C# all I get is this:

private void Button1_Click(object sender, EventArgs e)
{

}

How can I change the name of the Void to something else and make it still work with the button? And also, how can I change the part in C# where in VB.NET I can simply change the Handles Button1.Click to something else like Button1.MouseEnter?

I'm sorry for bad explanation, English is not my native language. I'd appreciate any help available.

added: If this question is a possible duplicate I apologize, I used Google and the search option here to find an answer, unfortunately I couldn't find one.

Edit: No, I'm using Visual C#.

هل كانت مفيدة؟

المحلول 2

You are missing part of the code.

When you instantiate your button, you associate it with a method to handle its events.

Button1.Click += new EventHandler(this.Button1_Click);

If you are looking to change the name of your method (Button1_Click) you will need to change both the method:

private void MyNewButtonEventHandler(object sender, EventArgs e)

And the handler:

Button1.Click += MyNewButtonEventHandler;

The handler code is usually in the designer.cs file.

نصائح أخرى

are you using asp.net?

You could add the EventHandler, for sample:

Button1.Click += Button1_Click;

In ASP.NET you could specify on the control tag, for sample:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"></asp:Button>

(it works for both, C# and Vb.Net)

In Windows Forms Application, the event handler is added in design file (take a look there and understand how everything works... it is not just magic as draging and drop).

Buttons and Event Handlers

When you press a button on the screen it generates a click event. You need to write a method to respond to that click. Any method that we use to respond to an event (generally speaking) is called an event handler. You must tell your program what buttons go with what event handlers. We call this registering the event handler.

So for buttons and there event handlers there is no return value so it will be void (means event handlers never return any value.) in any case.

Also you can not change the core syntax (the rules developed and imposed by C#) of C# or VB.NET so you have to follow this protocol. If you don't like it. Bro move ahead and choose some other technological platform.

As for as changing the event is concerned you can choose some other event handlers for some controls. You can name your event method what you want.

I might not be clear about what you're asking, but events like "Click" or "MouseEnter" do not return anything, so the return type is void.


It seems to me like you want the events to return a value so you can do something with the value.

I'd recommend keeping your logic in a separate method/class, not in the events themselves. Then you can call the same logic from multiple events, and the code will be easier to maintain:

private string DoSomething()
{
    ...
    ...

    return "someValue";
}

private void Button1_Click(object sender, EventArgs e)
{
    DoSomething();  // execute the code
}

private void Button1_MouseEnter(object sender, EventArgs e)
{
    DoSomething();  // execute the code here too
}

private void SomeOtherMethodThatNeedsTheReturnValue()
{
    var returnValue = DoSomething();  // execute the code, use the return value

    // do something else with the returnValue
}

paqogomez's answer is correct but I feel like it's not the simplest approach to the problem. The easiest way to rename a method (or just about anything in your code, to be honest) is to right click on the name of the method and select Refactor > Rename... (or whatever the equivalent is in your language) and Visual Studio will do all the work for you by renaming both the method and the handler. You can also just press F2 to rename whatever symbol is selected in your code (or where the editor's caret is currently located).

Honestly you shouldn't really be messing around manually in the automatically generated designer file unless there's a problem you need to fix because your changes might get ignored when the designer changes the code again or you could possibly break the designer.

If you want to manually declare your own event handlers in code you should do it in the form or window's constructor after the InitializeComponent method. But really there's no reason not to use the designer's automatic event handler generation most of the time.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top