从新形式中获取价值 [英] Get value from new form

查看:96
本文介绍了从新形式中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户使用一个按钮来启动一个新窗口并获得我想要使用的新值。


新值不是已更新为form1。


        private void button1_Click(object sender,EventArgs e)

        {
$


            Form2 newForm = new Form2();
$


            newForm.Show();

            portname2 = newForm.portname;



 

                         

        }



newform有一个列表框我想使用值


        public string portname; //全局变量



       private void listBox1_SelectedIndexChanged(object sender,EventArgs e)

        {

           

           portname = listBox1.SelectedItem.ToString();

           Thread.Sleep(500);

           MessageBox.Show(" new com port" + portname);

           

     

            this.Close();

        }



如何更新原始表单以便更改portname2




解决方案

更改你的Form2构造函数,以便它需要一个表格参数(即Form1)。然后将Form2创建为:


Form2 newForm = new Form2(this);


在构造函数代码中,将参数保存为成员变量,比如说" ParentForm" ;.然后在listBox1_SelectedIndexChanged()代码中,使用:


ParentForm.portname2 = listBox1.SelectedItem.ToString();



I am trying to get the user to with a button to launch a new window and get a new value that i want to use.

The new value is not being updated to form1.

        private void button1_Click(object sender, EventArgs e)
        {

            Form2 newForm = new Form2();

            newForm.Show();
            portname2 = newForm.portname;

 
                        
        }

newform has a listbox i want to use the value

        public string portname ;//global variable

       private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
           portname = listBox1.SelectedItem.ToString();
           Thread.Sleep(500);
           MessageBox.Show("new com port " + portname);
           
     
            this.Close();
        }

How do i update original form so portname2 is changed

解决方案

Change your Form2 constructor, so that it takes a Form parameter (namely Form1). Then create Form2 as:

Form2 newForm = new Form2 (this);

In the constructor code, save the parameter as a member variable, say "ParentForm". Then inside the listBox1_SelectedIndexChanged() code, use:

ParentForm.portname2 = listBox1.SelectedItem.ToString();


这篇关于从新形式中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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