美文网首页
C# 弹出窗口,TextBox控件交互,不同窗体参数传递

C# 弹出窗口,TextBox控件交互,不同窗体参数传递

作者: 吵吵人 | 来源:发表于2019-04-14 16:45 被阅读0次

主窗体是Form1,现在欲创建一个子窗体,并将子窗体上用户输入的数据传给主窗体。

创建新窗体


将该窗体命名为Parameter


设置窗体如下,实现输入数字,弹出MessageBox显示该数字的功能。弹出新建窗口的代码如下:

            Parameter frm = new Parameter();
            frm.ShowDialog();

接收参数

        private void button1_Click(object sender, EventArgs e)
        {
            string txt = textBox1.Text.ToString();
            MessageBox.Show(txt);
        }

效果


参数传递

现在不用对话框将数据显示出来,而是把用户输入的数据传给主窗口。
将主窗体中的“属性”封装成可以访问的属性

        private string strTest = "初始值10";
        public string StrTest
        {
            get { return strTest; }
            set { strTest = value; }
        } 

主窗体中弹出子窗体的代码

           Parameter f2 = new Parameter();
           f2.ShowDialog(this);

子窗体代码

    public partial class Parameter : Form
    {
        public Parameter()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = (Form1)this.Owner;
            f1.StrTest = textBox1.Text.ToString(); 
            this.Close();
        }
    }

参考:https://www.cnblogs.com/xh6300/p/6063649.html

另外,可以同过委托实现两个窗体之间的互操作

参考:https://blog.csdn.net/hkmaike/article/details/86006968

相关文章

网友评论

      本文标题:C# 弹出窗口,TextBox控件交互,不同窗体参数传递

      本文链接:https://www.haomeiwen.com/subject/jwcswqtx.html