Reputation: 2043
I converted a piece of code from VB to C#. The UI in VB has a button ButNewOrder. On click of the button , the below method gets executed in VB code
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
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. Double click on the button generates the following stub which means that there is nothing hooked up on the click event of the button.
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?
Upvotes: 0
Views: 84
Reputation: 7425
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.
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
Upvotes: 0
Reputation: 6367
Here's the C# version of VB's AddHandler
statement:
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. You'll get best results by putting it near where your form starts up.
Upvotes: 1