如何将textbox字符串从form1传递给form2 [英] How do pass textbox string from form1 to form2

查看:86
本文介绍了如何将textbox字符串从form1传递给form2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试将textbox字符串从form1传递给form2。



我使用以下代码来执行此操作



Hi,
I am trying to pass textbox string from form1 to form2.

I am using the following code to do that

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);

            }
        }





form2的构造函数





Constructor of form2

public class GatewayMsgIDs
    {

 public string dataLogFile = string.Empty;

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









但是当我这样做时,它显示空的form2(GatewayMsgIDs)

当我在按钮点击事件上点击下面的东西时

新的GatewayMsgIDs()。Show() ;

显示空表格





任何人都可以提出一些建议吗?? +



谢谢

John





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

推荐答案

像这样改变

Change like this
public GatewayMsgIDs(string logfilename)
       {
           InitializeComponent();
           dataLogFile = logfilename;
       }


我会为此添加一个属性。



I would add a property for that.

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:

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这有帮助。




hope this helps.


这篇关于如何将textbox字符串从form1传递给form2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆