说明:主窗体Form1上一个Button1,子窗体childForm上有一个ComboBox.
我想Click Button1的时候根据注册表获得本机上的串口个数(串口个数是不一定的),然后传递给子窗体的ComboBox.Items写了如下代码:
主窗体:
        private string str;
        public string Str
        {
            get { return str; }
            set { this.str = value; }
        }        private void button1_Click(object sender, EventArgs e)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
            string[] comArray = comKey.GetValueNames();
            for (int i = 0; i < comArray.Length; i++)
            {
                this.str = comKey.GetValue(comArray[i]).ToString(); //这里不知道该怎么写.我这种写发只能传最后一个字符串给子窗体.
            }
            childForm cf = new childForm();
            cf.ShowDialog(this);
        }
子窗体:        //子窗体的Form_Load事件
        private void childForm_Load(object sender, EventArgs e)
        {
            Form1 fm = (Form1)this.Owner;
            this.comboBox1.Items.Add(fm.Str);
        }这里的运行结果只有一个Item:COM2,实际上有2个(COM1和COM2).
不知道该怎么办.

解决方案 »

  1.   

    for (int i = 0; i < comArray.Length; i++)
                {
                    this.str = comKey.GetValue(comArray[i]).ToString(); //这里不知道该怎么写.我这种写发只能传最后一个字符串给子窗体.
                } 关键在这里 你只能将comArray最后一个赋给str为何不弄一个 string[] 的属性呢 然后在子窗体foreach 添加到comboBox1
      

  2.   

     for (int i = 0; i < comArray.Length; i++) 
     { 
         this.str = comKey.GetValue(comArray[i]).ToString(); //我的目的是把所有的字符串传递给子窗体.???
     } 
      

  3.   

    不是,简单的思路应该是
     childForm cf = new childForm(); 
    //在这里调用childForm 中ADDITEM(str)
     cf.ShowDialog(this); 
    你在childform中写个公开的委托方法delegate void addstr(string str)public void  ADDITEM(string str)
    {
    if(comboBox1.invokerequered)
    {
    ....跨线程委托调用方法
    }
    else
    {
    comboBox1.Items.Add(fm.Str); 
    }
    }
      

  4.   

      private string[] str;
            public string[] Str
            {
                get { return str; }
                set { this.str = value; }
            }        private void button1_Click(object sender, EventArgs e)
            {
                RegistryKey hklm = Registry.LocalMachine;
                RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
                str = comKey.GetValueNames();
                childForm cf = new childForm();
                cf.ShowDialog(this);
            }
    子窗体:        //子窗体的Form_Load事件
            private void childForm_Load(object sender, EventArgs e)
            {
                Form1 fm = (Form1)this.Owner;
                foreach(string s in fm.Str){
    this.comboBox1.Items.Add(s);
                }
              
            } 
      

  5.   

      private string[] str;
            public string[] Str
            {
                get { return str; }
                set { this.str = value; }
            }        private void button1_Click(object sender, EventArgs e)
            {
                RegistryKey hklm = Registry.LocalMachine;
                RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
                str = comKey.GetValueNames();
                childForm cf = new childForm();
                cf.ShowDialog(this);
            }
    子窗体:        //子窗体的Form_Load事件
            private void childForm_Load(object sender, EventArgs e)
            {
                Form1 fm = (Form1)this.Owner;
                foreach(string s in fm.Str){
    this.comboBox1.Items.Add(s);
                }
              
            }
      

  6.   

    也可以用绑定的方式comboBox1.DataSoure = ...;
      

  7.   

    子窗体加一个成员变量:从主窗体来的数据类型 变量名;给子窗口添个带参数的构造函数public ChildForm(要传数据的类型 参数名)
    {
      变量名=参数名;
    }这样数据就传过来了,随便什么类型的数据都可以。
      

  8.   

    for (int i = 0; i < comArray.Length; i++) 

        this.str = comKey.GetValue(comArray[i]).ToString(); //我的目的是把所有的字符串传递给子窗体.??? 

    ----->>
    for(int i=0;i<comArray.Length;i++)
    {
    this.str+=comkey.GetValue(comArray[i]).ToString();
    }
      

  9.   

     this.str 只是唯一的变量,在循环中,新的值会覆盖旧的
      

  10.   

    把子窗体要赋值的控件Modifiers属性设为public
    Form4 a=new Form4();
    a.textBox1.Text="123";
    就这个意思
      

  11.   

    这样得到的是注册表的名称.我要的是后面的数据.comKey.GetValue(comArray[i]).ToString(); 
    这个方法必不可少,不然无法得到数据.10楼的可以得到数据,不过就是全部写在一起了,没分开
      

  12.   

    构造public partial class Form2 : Form
        {
            private Form1 _Form1 = null;//在Form2中添加Form1这个属性 把Form1当成Form2的属性用 控件也成
            public Form2(Form1 _Form1)//重载构造 把Form1 或者 控件传过来
            {
                this._Form1 = _Form1;
                InitializeComponent();
                this.Show();  
            }
            
            public Form2()
            {
                InitializeComponent();
                this.Show();
            }
             
            这种传值也可以
            //public Form1 F1
            //{
            //    set { this._Form1 = value; }
            //}
            private void button1_Click(object sender, EventArgs e)
            {
                //用下面的格式就可以在Form2中使用Form1中的值 控件
                this._Form1.Visible = true;
                //this._Form1.Show();
                
            }
        }
      

  13.   

    for (int i = 0; i < comArray.Length; i++)
                {
                    this.str += comKey.GetValue(comArray[i]).ToString(); //这里不知道该怎么写.我这种写发只能传最后一个字符串给子窗体.
                } 
      

  14.   

    功夫不负有心人,搞了一天终于自己搞定了!!!散分.
    父窗体:
            private ArrayList comList;
            public ArrayList ComList
            {
                get { return comList; }
                set { this.comList = value; }
            }        private void button1_Click(object sender, EventArgs e)
            {            
                RegistryKey hklm = Registry.LocalMachine;
                RegistryKey comKey = hklm.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
                string[] comArray = comKey.GetValueNames();
                comList = new ArrayList(comArray.Length);
                for (int i = 0; i < comArray.Length; i++)
                {
                    this.comList.Add(comKey.GetValue(comArray[i]).ToString());
                }
                childForm cf = new childForm();
                cf.ShowDialog(this);
            } 
    子窗体:
            private void childForm_Load(object sender, EventArgs e)
            {            
                Form1 fm = (Form1)this.Owner;
                for (int i = 0; i < fm.ComList.Count; i++)
                {
                    this.comboBox1.Items.Add(fm.ComList[i]);
                }
            }