0

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?

Apoorv
  • 2,023
  • 1
  • 19
  • 42

2 Answers2

1

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.

InteXX
  • 6,135
  • 6
  • 43
  • 80
  • I dont see the same thing happenning in VB code. I compared the designers but VB designer also doesnt have definition for the click event of the button. – Apoorv Jul 08 '16 at 08:55
  • 1
    @Apoorv: VB uses the `Handles` clause, which is the equivalent. – InteXX Jul 08 '16 at 08:56
  • your code works bro but just wanted to know that why the VB designer didnt have that click event ? – Apoorv Jul 08 '16 at 08:58
  • 1
    It does; it just wires it up using the 'Handles' clause instead of the `AddHandler` statement. `ButNewOder.Click += this.mnuFileNewJob_Click;` is C#'s parity with VB's `AddHandler`. C# doesn't have parity with `Handles`. – InteXX Jul 08 '16 at 09:02
  • and this VB code does in code behind ? I have one more question. When i m clicking on the close button present on the top right, the page is not closing ? – Apoorv Jul 08 '16 at 09:04
  • 1
    See [this](http://stackoverflow.com/questions/8113334/), [this](http://stackoverflow.com/questions/3912836/) or [this](http://stackoverflow.com/questions/5318876/) for ideas about your form closure problem. – InteXX Jul 08 '16 at 09:10
  • 1
    By code behind you must mean the designer partial class. No, C# does it in the designer. VB, with its `Handles` clause, is able to do it in the main class (as indicated in the code you posted). – InteXX Jul 08 '16 at 09:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116778/discussion-between-apoorv-and-intexx). – Apoorv Jul 08 '16 at 09:18
  • 1
    @Apoorv: If my answer solved your problem, would you mark it as such. If not, let me know and I'll continue to try to help. – InteXX Jul 08 '16 at 10:09
0

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

Mafii
  • 7,227
  • 1
  • 35
  • 55
  • its a WinForms based project. Where do I find Events here? – Apoorv Jul 08 '16 at 08:49
  • @Apoorv You click onto your button once, then there is a bolt at the bottom right in the properties window. If there is no properties window, rightclick the button and press properties – Mafii Jul 08 '16 at 08:50