Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I am trying to pass textbox string from form1 to form2.

I am using the following code to do that

C#
public void btnGatewayMsgsClick(object sender, EventArgs e)
        {

            try
            {
                //new GatewayMsgIDs(txtDataLogFile.Text).Show();
                new GatewayMsgIDs(txtDataLogFile.Text).Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.RT_ERROR + ex.Message, Constants.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }


Constructor of form2

C#
public class GatewayMsgIDs
    {

 public string dataLogFile = string.Empty;

        public GatewayMsgIDs(string logfilename)
        {
            dataLogFile = logfilename;
        }
}




But when I am trying like this, it is showing empty form2(GatewayMsgIDs)
when I am tring something like below on button click event
new GatewayMsgIDs().Show();
It is showing empty form


Can anyone suggest something about this??+

Thanks
John
Posted

Change like this
C#
public GatewayMsgIDs(string logfilename)
       {
           InitializeComponent();
           dataLogFile = logfilename;
       }
 
Share this answer
 
I would add a property for that.

C#
public void btnGatewayMsgsClick(object sender, EventArgs e)
        {

            try
            {
                GatewayMsgIDs gmID = new GatewayMsgIDs();
                gmID.DataLogFile = txtDataLogFile.Text;
                gmID.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.RT_ERROR + ex.Message, Constants.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

the other form looks like this:

C#
public class GatewayMsgIDs
    {

 private string dataLogFile = string.Empty; //this should NOT be public!

        public GatewayMsgIDs()
        {
        }

        public string DataLogFile{
          set{
            //here you could first check if the value a valid value (depending on what it should contain.)
            dataLogFile = value;
          }
          get{} // if necessary
        }
}



hope this helps.
 
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