Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Okay, i'm writing my final year project after spending most of my time programming in consoles.My dilemna is i personally want to code in c++ even though at present with the abundance of online help in c#, c# would be a better option.I need a way to rewrite this block of c# code in c++

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PassBetweenForms
{
    public partial class frmMain : Form
    {
        // default constructor
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            // nothing to do
        }

        private void btnSetName_Click(object sender, EventArgs e)
        {
            frmID f = new frmID();

            // Add an event handler to update this form
            // when the ID form is updated (when IdentityUpdated fires).
            f.IdentityUpdated += new frmID.IdentityUpdateHandler(IdForm_ButtonClicked);

            f.Show();

        }

        // handles the event from frmId
        private void IdForm_ButtonClicked(object sender, IdentityUpdateEventArgs e)
        {
            // update the forms values from the event args
            txtFirstName.Text = e.FirstName;
            txtMiddleName.Text = e.MiddleName;
            txtLastName.Text = e.LastName;
        }


        private void btnSetAddress_Click(object sender, EventArgs e)
        {
            frmAddress f = new frmAddress();

            // Add an event handler to update this form
            // when the address form is updated (when AddressUpdated fires).
            f.AddressUpdated += new frmAddress.AddressUpdateHandler(AddressForm_ButtonClicked);

            f.Show();
        }


        // handles the event from frmAddress
        private void AddressForm_ButtonClicked(object sender, AddressUpdateEventArgs e)
        {
            // update the forms values from the event args
            txtStreet.Text = e.Street;
            txtCity.Text = e.City;
            txtState.Text = e.State;
            txtZipCode.Text = e.ZipCode;
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}







C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PassBetweenForms
{
    public partial class frmID : Form
    {

        // add a delegate
        public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);

        // add an event of the delegate type
        public event IdentityUpdateHandler IdentityUpdated;


        // default constructor
        public frmID()
        {
            InitializeComponent();
        }

        // close the form without raising the event
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        // raise the event
        private void btnOkay_Click(object sender, EventArgs e)
        {
            // this button click event handler will raise the
            // event which can then intercepted by any listeners

            // read the textboxes and set the member variables
            string sNewFirst = txtFirstName.Text;
            string sNewMiddle = txtMiddleName.Text;
            string sNewLast = txtLastName.Text;

            // instance the event args and pass it each value
            IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(sNewFirst,
                sNewMiddle, sNewLast);

            // raise the event with the updated arguments
            IdentityUpdated(this, args);

            this.Dispose();
        }
    }


    public class IdentityUpdateEventArgs : System.EventArgs
    {
        // add local member variable to hold text
        private string mFirstName;
        private string mMiddleName;
        private string mLastName;

        // class constructor
        public IdentityUpdateEventArgs(string sFirstName, string sMiddleName, string sLastName)
        {
            this.mFirstName = sFirstName;
            this.mMiddleName = sMiddleName;
            this.mLastName = sLastName;
        }

        // Properties - Accessible by the listener

        public string FirstName
        {
            get
            {
                return mFirstName;
            }
        }

        public string MiddleName
        {
            get
            {
                return mMiddleName;
            }
        }


        public string LastName
        {
            get
            {
                return mLastName;
            }
        }
    }
}
Posted
Updated 20-Jul-15 4:52am
v2
Comments
[no name] 20-Jul-15 10:09am    
One way of rewriting this is to use a keyboard. Seriously, we are not a code translation service. If you know C++/CLI then why are you unable to change the C# code to C++/CLI? What have you tried to do for yourself? What is the problem with what you have tried?
Sergey Alexandrovich Kryukov 20-Jul-15 10:50am    
Probably Reflector could do it automatically (I use ILSpy which only supports IL, C# and VB.NET at the moment.)
—SA
Philippe Mori 20-Jul-15 13:38pm    
This is documented on MSDN. In C++/CLI, you need to be explicit in particular to take the address of a member function and you need to provide both this and the address.

As far as I know Reflector can do this provide the appropriate plug in is installed. I have done it many time in the past.

In pratice, however it does not make much sense to do that in you case. The main reason to use C++/CLI, is to make a bridge between managed and unmanaged code. The only time one would do an UI in C++/CLI would be if the user interface is minimalist and the rest of the code is written in C++. Or maybe if the designer is not used.

1 solution

It really should be no big deal if you use MFC for the GUI and its CString class for the strings. The only problem will be the callback and its handlers.

I prefer it like in the old way like in this sample code. Some excerpt:
C++
//declare a function type
typedef int (*CallbackType)(float); 
//it is useable as a variable
CallbackType callback = &DoWork;//assign it to a function (with matching signature)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900