有两个窗口Form1和Form2 ,Form1中有个组件SerialPort,我的 问题是怎样从Form2中引用
Form1的SerialPort?

解决方案 »

  1.   

    new form2的时候把form1自己传进去  
    Form2 Dlg = new Form2(this);  
    Dlg.ShowDialog();  
    Form2:public Form2(Form1 f1) 

    this.form1=f1; 

      

  2.   

    在Form2中重载构造方法,在Form1中创建Form2的对象时将SerialPort的对象传给Form2.
    如果你只想改变SerialPort组件的某个属性那么不要将整个组件传给Form2要用一个委托将此属性封装后传给Form2.
      

  3.   

    1楼,经测试,不对哦,this.form1=f1;  
      

  4.   

    this .Text = this.components.Components["serialPort1"].ToString();
    这是在Form1中引用自己的组件,我想通过这种方法来在Form2中引用serialPort1:
    System.IO.Ports.SerialPort seriport = Application.OpenForms["Form1"] .components.Components[2] as System.IO.Ports.SerialPort;
    可是却发现不能引用components,这是怎么回事?
      

  5.   


    然后form1.SerialPort1用就可以了。
      

  6.   

    Form2: Form1 from1 = null;
    public Form2(Form1 f1)  
    {  
    this.form1=f1;  
    }  
      

  7.   

    给大家个提示,我是这样引用Form1中的控件的:
          AxMSFlexGridLib.AxMSFlexGrid flex = (AxMSFlexGridLib.AxMSFlexGrid)Application.OpenForms["Form1"].Controls[0].Controls["tabPage3"].Controls["axMSFlexGrid1"];
    可换成了组件,怎么就不能实现呢?怪哉!
    请大家献策,
      

  8.   

    很明显的错误,你修改一下啊:在构造函数前面加一个定义:
    Form1 from1 = null; 
    如果不行,可以考虑用委托调用相关方法。
      

  9.   

    Form1中SerialPort的Modifiers属性不要设为Private或Internal,这样Form2就可以访问了:Form1 form1;
    public Form2(Form1 FormMain)  
    {  
    this.form1=FormMain;  
    }//操作
    form1.SerialPort .....
      

  10.   

    或者你干脆把它传进去SerialPort mySerialPort;
    public Form2(ref SerialPort InputSerialPort)  //注意 ref
    {  
        mySerialPort =  InputSerialPort;
      

  11.   

    我知道大家说的那种方法,这种方法我以前试着用过,很不稳定。
    而且在.NET2.0中Application.OpenForms["Form1"]就可以实现大家所说的。
      

  12.   

    还请大家继续,保证参与者人人有分。
    大家可以试试Application.OpenForms["Form1"].Controls这种引用控件的方法,引用组件
      

  13.   

    怎样向Container中添加组件呢?有人知道吗?
      

  14.   

    实现
    Form1窗体代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        public Button Form1Button1 //主要这里提供一个属性,供外部调用,和别的控件一样
            {
                get
                {
                    return this.button1;
                }
            }        private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.Show();
            }    }
    }Form2窗体代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void Form2_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                Form1 frm = new Form1();
                this.button1.Text=frm.Form1Button1.Text;
            }
        }
    }一个访问另外个窗体的Button.Text
      

  15.   

    呵呵,问题解决,结合大家的方法,
    然后在Form1中添加
     public Container con = new Container();
     con.Add(this.components.Components[2] );
    就可以在Form2中引用了
     System.IO.Ports.SerialPort seriport = form1.con.Components[0] as System.IO.Ports.SerialPort;
    谢谢大家的参与。
    另有一个奇怪的问题就是 为什么不能直接引用Container呢?
    this.Container.Add(this.components.Components[2]);总是出错。
    答应大家的一定作到,明天再加100分,大散分拉。
      

  16.   

    17楼的好辛苦,
    其实对于控件下面就可以直接访问:
    this.button1.Text=((Button)Application.OpenForms["Form1"].Controls["button1"] ).Text;
      

  17.   

    最简单的办法是把Form1中SerialPort属性的修饰符改为 public还有是在form2中new form1的时候把SerialPort组建传参传进去
      

  18.   

    其实,不用传递,在Form2中:
    Form1 form1 = (Form1)this.Owner;           
    System.IO.Ports.SerialPort seriport;
    seriport = form1.con.Components[0] as System.IO.Ports.SerialPort;在Form1中添加 
     public Container con = new Container(); 
     con.Add(this.components.Components[2] ); 
      

  19.   

    以上所有方法都不符合面向对象的方法分两层实现,UI层只管界面,楼主加个类专门封装SP吧。这在代码规范比较好的。
      

  20.   

    先要将form2在form1中new 出来.