[英]Subscribing Events in C# Code
I converted a piece of code from VB to C#. 我将一段代码从VB转换为C#。 The UI in VB has a button ButNewOrder .
VB中的UI有一个按钮ButNewOrder 。 On click of the button , the below method gets executed in VB code
单击按钮后,以下方法将在VB代码中执行
Public Sub mnuFileNewJob_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ButNewOrder.Click
Dim ErrorFlag As ErrorFlagType = InitErrorFlag()
Try
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
StatusText = "Loading New Job."
LoadNewSoftJob(Me)
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
Catch ex As Exception
ErrorFlag.NumErrors += 1
ReDim Preserve ErrorFlag.ErrorDef(ErrorFlag.NumErrors - 1)
With ErrorFlag.ErrorDef(ErrorFlag.NumErrors - 1)
.Description = "Error Loading New Job: " + ex.Message
.Number = ErrorFlag.NumErrors - 1
End With
End Try
If ErrorFlag.NumErrors > 0 Then
Dim ErrFrm As New FrmErrList
ErrFrm.ErrorFlag = ErrorFlag
ErrFrm.Show()
End If
End Sub
The above code when I convert to C#, I get this 上面的代码在我转换为C#时得到了
public void mnuFileNewJob_Click(System.Object eventSender, System.EventArgs eventArgs)
{
Mold_Power_Suite.Model.FrontEndStructures.ErrorFlagType ErrorFlag = FrontEndStructures.InitErrorFlag();
try
{
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
ModSoftFrontEndGlobalVariables.StatusText = "Loading New Job.";
frmMain main = new frmMain();
MainMod.LoadNewSoftJob(this);// I think I need to replace this with the form name
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
}
catch (Exception ex)
{
ErrorFlag.NumErrors += 1;
Array.Resize(ref ErrorFlag.ErrorDef, ErrorFlag.NumErrors);
var _with1 = ErrorFlag.ErrorDef[ErrorFlag.NumErrors - 1];
_with1.Description = "Error Loading New Job: " + ex.Message;
_with1.Number =Convert.ToInt16( ErrorFlag.NumErrors - 1);
}
if (ErrorFlag.NumErrors > 0)
{
FrmErrList ErrFrm = new FrmErrList();
ErrFrm.ErrorFlag = ErrorFlag;
ErrFrm.Show();
}
}
Clicking on the Button in C# application is not resulting in anything. 在C#应用程序中单击“按钮”不会产生任何结果。 Double click on the button generates the following stub which means that there is nothing hooked up on the click event of the button.
双击按钮会生成以下存根,这意味着按钮的click事件没有任何关联。
private void ButNewOrder_Click(object sender, EventArgs e)
{
}
I want to know how to let my button execute the same function as that of VB code? 我想知道如何让我的按钮执行与VB代码相同的功能?
Here's the C# version of VB's AddHandler
statement: 这是VB的
AddHandler
语句的C#版本:
ButNewOrder.Click += new System.EventHandler(this.mnuFileNewJob_Click);
This line of code is traditionally added to your InitializeComponent()
method by the form designer, but technically you can put it just about anywhere. 传统上,这行代码是由表单设计者添加到
InitializeComponent()
方法中的,但是从技术上讲,您可以将其放置在任何地方。 You'll get best results by putting it near where your form starts up. 将其放在表单开始的位置附近,将获得最佳结果。
You can either navigate to Properties->Events (events is represented as a bolt) in Visual Studio when your button is selected and select, in the click event dropdown, your method. 选择按钮后,可以在Visual Studio中导航至“属性”->“事件”(事件以螺栓表示),然后在“单击事件”下拉列表中选择方法。
Or add this in the Constructor: 或在构造函数中添加此代码:
ButNewOrder += mnuFileNewJob_Click
Alternatively, you can navigate to the designer code of your window (into InitializeComponent()
), and replace ButNewOrder += ButNewOrder_Click
with ButNewOrder += mnuFileNewJob_Click
或者,你可以浏览到你的窗口(到的设计器代码
InitializeComponent()
并更换ButNewOrder += ButNewOrder_Click
与ButNewOrder += mnuFileNewJob_Click
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.