vb.net 改虚名?(等于私有子 NameOfSub() )
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21942208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Void name? ( Equal to Private Sub NameOfSub() )
提问by RAJVH
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:
我刚开始学习 C#,我想知道是否可以
更改 Void 的“名称”(如果这与 VB.NET 中的 Sub 相同)?我知道一些关于VB.NET的基本知识,
这是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:
当我双击一个按钮在 C# 中添加代码时,我得到的是:
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?
如何将 Void 的名称更改为其他名称并使其仍可与按钮一起使用?而且,如何更改 C# 中的部分,在 VB.NET 中,我可以简单地将 Handles Button1.Click 更改为 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#.
编辑:不,我使用的是 Visual C#。
回答by Felipe Oriani
are you using asp.net?
你在用asp.net吗?
You could add the EventHandler, for sample:
您可以添加 EventHandler,例如:
Button1.Click += Button1_Click;
In ASP.NET you could specify on the control tag, for sample:
在 ASP.NET 中,您可以在控件标记上指定,例如:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"></asp:Button>
(it works for both, C# and Vb.Net)
(它适用于 C# 和 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).
在 Windows 窗体应用程序中,事件处理程序被添加到设计文件中(看看那里并了解一切是如何工作的......它不仅仅是拖放的魔法)。
回答by paqogomez
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:
如果您要更改方法的名称 ( Button1_Click),则需要同时更改两个方法:
private void MyNewButtonEventHandler(object sender, EventArgs e)
And the handler:
和处理程序:
Button1.Click += MyNewButtonEventHandler;
The handler code is usually in the designer.cs file.
处理程序代码通常在designer.cs 文件中。
回答by Aftab Ahmed
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.
此外,您不能更改 C# 或 VB.NET 的核心语法(由 C# 开发和强加的规则),因此您必须遵循此协议。如果你不喜欢它。兄弟继续前进并选择其他一些技术平台。
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.
至于更改事件,您可以为某些控件选择其他一些事件处理程序。您可以根据需要命名事件方法。
回答by vivat pisces
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.
我可能不清楚你在问什么,但是像“Click”或“MouseEnter”这样的事件不会返回任何东西,所以返回类型是无效的。
It seemsto 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
}
回答by Nacimota
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 F2to rename whatever symbol is selected in your code (or where the editor's caret is currently located).
paqogomez 的回答是正确的,但我觉得这不是解决问题的最简单方法。重命名方法(或几乎任何代码中的任何内容,老实说)的最简单方法是右键单击方法的名称,然后选择重构 > 重命名...(或您的语言中的任何等效项)和 Visual Studio 将通过重命名方法和处理程序来为您完成所有工作。您也可以按F2重命名代码中选择的任何符号(或编辑器插入符号当前所在的位置)。
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 InitializeComponentmethod. But really there's no reason not to use the designer's automatic event handler generation most of the time.
如果您想在代码中手动声明您自己的事件处理程序,您应该在InitializeComponent方法之后的窗体或窗口的构造函数中进行。但实际上,大多数时候没有理由不使用设计器的自动事件处理程序生成。

