把控件的Modifier屬性直接改成public就可以以“form.控件名”的形式訪問了
但是你也可以定義窗體的屬性間接來傳遞數值

解决方案 »

  1.   

    在form1的aspx.cs中
    Session["textbox1"]=Text1.text;在form2的aspx.cs中
    Text2.text=(string)Session["textbox1"];
      

  2.   

    设计思路
    此方法实现二个功能:
    其一,主窗体能够实时地向从窗体传送数据,表现为当更改主窗体中的跟踪条(TrackBar)的数值,从窗体中定义的一个Label组件就显示出跟踪条的当前数值;
    其二,从窗体能够向主窗体提出数据请求,并且能够获取主窗体中各组件显示的数据。程序表现为,当单击从窗体中的【从Form1中获取数据】按钮,程序能够把主窗体中的二个TextBox组件显示的内容传递到从窗体,并且通过从窗体中的二个TextBox组件分别显示出来。
    第一个功能的实现思路是把从窗体看成是主窗体的一个实例,加入到从窗体中的组件,可以看出是从窗体中定义的一个个变量,由于从窗体中加入的组件的组件缺省定义类型是Private(私有的),所以要想访问这些组件,必须改变为Public(共有的);而第二个功能的实现思路是通过修改Form2的构造函数,构造函数实现功能是通过Form1类的实例(即为主窗体)来创建并初始化Form2类的实例(即为从窗体)。这样对于从窗体来说,主窗体则为其一个实例,从而也就可以向主窗体提出数据请求,当然要把需要访问的各组件定义类型从缺省的Private(私有的)类型修改为Public(共有的)。上述二个功能的实现方法,第二种方法比较复杂,希望各位能够结合后面的具体实现代码来理解。
    第二种窗体间的数据传递情况实现步骤
    1.首先创建一个Visual C#的项目文件,项目名称为【VC#中不同窗体数据传递方法02】。2.把Visual Studio .Net的当前窗口切换到【Form1.cs(设计)】窗口,并从【工具箱】中的【Windows窗体】选项卡中拖入下列组件到【Form1.cs(设计)】窗体中,并执行相应操作:
    · 二个TextBox组件,用以输入向Form2窗体传送的数据
    · 二个Label组件
    · 一个TrackBar组件,名称为trackBar1。
    3.把Visual Studio .Net的当前窗口切换到【Form1.cs】窗口,即:Form1.cs的代码编辑窗口。并用下列代码替换替代系统产生的InitializeComponent过程。
    private void InitializeComponent ( )
    {
    this.label1 = new System.Windows.Forms.Label ( ) ;
    this.label2 = new System.Windows.Forms.Label ( ) ;
    this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
    this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
    this.trackBar1 = new System.Windows.Forms.TrackBar ( ) ;
    (  ( System.ComponentModel.ISupportInitialize ) ( this.trackBar1 ) 
    ).BeginInit ( ) ;
    this.SuspendLayout ( ) ;
    this.label1.Location = new System.Drawing.Point ( 27 , 41 ) ;
    this.label1.Name = "label1" ;
    this.label1.TabIndex = 0 ;
    this.label1.Text = "欢迎词:" ;
    this.label2.Location = new System.Drawing.Point ( 27 , 83 ) ;
    this.label2.Name = "label2" ;
    this.label2.TabIndex = 1 ;
    this.label2.Text = "提示信息:" ;
    this.textBox1.Location = new System.Drawing.Point ( 108 , 38 ) ;
    this.textBox1.Name = "textBox1" ;
    this.textBox1.TabIndex = 2 ;
    this.textBox1.Text = "" ;
    this.textBox2.Location = new System.Drawing.Point ( 109 , 78 ) ;
    this.textBox2.Name = "textBox2" ;
    this.textBox2.TabIndex = 3 ;
    this.textBox2.Text = "" ;
    this.trackBar1.LargeChange = 1 ;
    this.trackBar1.Location = new System.Drawing.Point ( 12 , 182 ) ;
    this.trackBar1.Maximum = 100 ;
    this.trackBar1.Name = "trackBar1" ;
    this.trackBar1.Size = new System.Drawing.Size ( 272 , 42 ) ;
    this.trackBar1.TabIndex = 1 ;
    this.trackBar1.ValueChanged += new System.EventHandler ( 
    this.trackBar1_ValueChanged ) ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
    this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
    this.Controls.AddRange ( new System.Windows.Forms.Control[] {
      this.trackBar1 ,
      this.textBox2 ,
      this.textBox1 ,
      this.label2 ,
      this.label1 } ) ;
    this.MaximizeBox = false ;
    this.MinimizeBox = false ;
    this.Name = "Form1" ;
    this.Text = "Form1" ;
    this.Load += new System.EventHandler ( this.Form1_Load ) ;
    (  ( System.ComponentModel.ISupportInitialize ) ( this.trackBar1 ) 
    ).EndInit ( ) ;
    this.ResumeLayout ( false ) ;

    4.由于从窗体向主窗体提出的数据请求是二个TextBox组件的"Text"属性值,所以要修改Form1.cs文件中这二个TextBox组件的定义类型,把缺省定义为"private"类型修改为"public"类型,修改后的这二个TextBox组件在Form1.cs中的定义语句如下:
    public System.Windows.Forms.TextBox textBox1 ;
    public System.Windows.Forms.TextBox textBox2 ; 
    在上述代码后面再添加下面代码,下面代码是创建一个Form2类的实例m_Form,即从窗体:
    private Form2 m_Form ; 
    5.在Form1.cs中的Main函数后,添加下列代码,下列代码的功能是实现当修改主窗体中的跟踪条数值后,从窗体中的label3组件的显示数值能够随之而变化,这样就实现主窗体实时传递数据到从窗体了:
    private void trackBar1_ValueChanged ( object sender , System.EventArgs e )
    {
    m_Form.label3 .Text = trackBar1.Value.ToString ( ) ;
    6.在添加完上面代码,并在其后部,再添加下列代码,下列代码的功能是使用Form2类的构造函数,并通过Form1类的实例来创建并初始化Form2类的实例。在项目文件中加入Form2类,并修改Form2类的构造函数工作将在本节的第7到11步骤中完成。
    private void Form1_Load ( object sender , System.EventArgs e )
    {
    m_Form = new Form2 ( this ) ;
    //通过主窗体来创建、初始化从窗体
    m_Form.Show ( ) ;
    //显示从窗体
    }  
    7.选择菜单【项目】|【添加Windows窗体】后,弹出【添加新项-VC#中不同窗体数据传递方法01】对话框。在此对话框中的【名称(N):】文本框中输入【Form2】后,单击【打开】按钮,则在VC#中不同窗体数据传递方法01项目中添加了一个新的窗体,名称为【Form2】。 
      

  3.   

    8.把Visual Studio .Net的当前窗口切换到【Form2.cs(设计)】窗口,并从【工具箱】中的【Windows窗体】选项卡中拖入下列组件到【Form2.cs(设计)】窗体中,并执行相应操作:· 二个TextBox组件,用以显示向主窗体请求获得的数据。· 二个Label组件。· 一个Button组件,名称为button1。9.把Visual Studio .Net的当前窗口切换到【Form2.cs】窗口,即:Form2.cs的代码编辑窗口。并用下列代码替换替代系统产生的InitializeComponent过程。
    {
    this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
    this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
    this.label2 = new System.Windows.Forms.Label ( ) ;
    this.label1 = new System.Windows.Forms.Label ( ) ;
    this.button1 = new System.Windows.Forms.Button ( ) ;
    this.label3 = new System.Windows.Forms.Label ( ) ;
    this.SuspendLayout ( ) ;
    this.textBox1.Location = new System.Drawing.Point ( 95 , 42 ) ;
    this.textBox1.Name = "textBox1" ;
    this.textBox1.Size = new System.Drawing.Size ( 125 , 21 ) ;
    this.textBox1.TabIndex = 2 ;
    this.textBox1.Text = "" ;
    this.textBox2.Location = new System.Drawing.Point ( 94 , 80 ) ;
    this.textBox2.Name = "textBox2" ;
    this.textBox2.Size = new System.Drawing.Size ( 127 , 21 ) ;
    this.textBox2.TabIndex = 3 ;
    this.textBox2.Text = "" ;
    this.label2.Location = new System.Drawing.Point ( 27 , 83 ) ;
    this.label2.Name = "label2" ;
    this.label2.TabIndex = 5 ;
    this.label2.Text = "提示信息:" ;
    this.label1.Location = new System.Drawing.Point ( 38 , 45 ) ;
    this.label1.Name = "label1" ;
    this.label1.TabIndex = 4 ;
    this.label1.Text = "欢迎词:" ;
    this.button1.Location = new System.Drawing.Point ( 80 , 136 ) ;
    this.button1.Name = "button1" ;
    this.button1.Size = new System.Drawing.Size ( 135 , 53 ) ;
    this.button1.TabIndex = 6 ;
    this.button1.Text = "从Form1中获取数据" ;
    this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
    this.label3.Location = new System.Drawing.Point ( 102 , 210 ) ;
    this.label3.Name = "label3" ;
    this.label3.TabIndex = 7 ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
    this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
    this.Controls.AddRange ( new System.Windows.Forms.Control[] {
      this.label3 ,
      this.button1 ,
      this.textBox2 ,
      this.textBox1 ,
      this.label2 ,
      this.label1 } ) ;
    this.MaximizeBox = false ;
    this.MinimizeBox = false ;
    this.Name = "Form2" ;
    this.Text = "Form2" ;
    this.ResumeLayout ( false ) ;
    }  
    10.由于主窗体是把其中的跟踪条的数值通过从窗体中的label组件来显示的,所以必须把Form2.cs文件中创建label3组件时定义的"private"类型修改为"public"类型,修改后的创建label3组件的代码为:
    public System.Windows.Forms.Label label3 ;  
    由于Form2类的实例是通过Form1类的实例来初始化,所以在创建label3组件后面添加下列代码,下列代码是创建一个Form1类的实例,其作用是初始化Form2类的实例(即从窗体):
    private Form1 mF_Form ;  
    11.修改Form2类的构造函数,具体操作如下,用下列代码替换Form2.cs中缺省的构造函数:
    public Form2 ( Form1 myForm )
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent ( ) ;
    this.mF_Form  = myForm ;
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }  
    12.在Form2.cs中的Main函数后,添加下列代码,下列代码的功能是实现向主窗体提出数据请求,并获取主窗体的数据,当然这些数据的类型必须修改为"public"类型。
    private void button1_Click ( object sender , System.EventArgs e )
    {
    textBox1.Text = this.mF_Form.textBox1.Text  ;
    textBox2.Text = this.mF_Form.textBox2.Text  ;
    } 自己看看,会有收获的!
      

  4.   

    Form1类中public System.Windows.Forms.TextBox textBox1;
    private void button1_Click(object sender, System.EventArgs e)
    {
        Form2 tempForm = new Form2(this);
        tempForm.Show()
    }
    Form2类中
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace App1
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private Form1 mainForm;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form2(Form1 tempForm)
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    mainForm = tempForm;
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(96, 112);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.button1});
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion private void button1_Click(object sender, System.EventArgs e)
    {
        mainForm.textBox1.Text = "写你要改变的Form1上textBox1的文本";
    }
    }
    }
      

  5.   

    有两种方法:  
    1.添加一个标准模块(Module),在其中定义变量。如Public  t1  As  String然后在frmother窗体中给这个变量赋值,这样就可以在frminput窗体取得这个变量的值了。  
    2.先定义窗体之间的引用,如:  
             在'///frminput窗体中写:  
                 Public  frmother  as  new  frmother  
             在'///frmother窗体中写:  
                 Public  frminput  as  new  frminput  
                 frminput.frmother=me  
    然后在frminput就可以调用frmother中的过程、函数、控件  
    比如想取frmother中的TextBox1的值:frmother.TextBox1.text  
      

  6.   

    可以用公共变量传~~如果是要传好多空间的属性那就定义一个相同类型的公共类。
    比如你想让别的窗口可以调用或设置一个叫text1的文本框的属性你可以这样。
    public TextBox share_text1
    {
      get
       {
         return text1;
       }
      set
      {
        text1=value;
      }}
    然后你再别的窗口设置或使用那个叫share_text1的类的属性就行了。
    我觉得这样很方便。
      

  7.   

    public interface IPipe
    {
    IPipeManager Manager {get;set;}
    void OnNotifyChanged(object id, object value);
    }public interface IPipeManager
    {
    object Link(IPipe a,IPipe b);
    void UnLink(object key);
    void NotifyChanged(IPipe sender,object id,object value);
    }public class PipeManager:IPipeManager
    {
    private Hashtable links=new Hashtable();private class PipePair
    {
    public PipePair(IPipe a, IPipe b)
    {
    this.A=a;
    this.B=b;
    }
    public IPipe  A=null;
    public IPipe  B=null;
    }
    public object Link(IPipe a, IPipe b)
    {
    if(a==null || b==null) throw new ArgumentNullException();
    if(a.Manager!=null || b.Manager!=null) throw new ArgumentException();
    a.Manager=this;
    b.Manager=this;
    object key=new Object();
    this.links.Add(key,new PipePair(a,b));
    return key;
    }
    public void UnLink(object key)
    {
    if(this.links.ContainsKey(key))
    {
    PipePair pipePair=(PipePair)this.links[key];
    pipePair.A.Manager=null;
    pipePair.B.Manager=null;
    this.links.Remove(key);
    }
    }
    public void NotifyChanged(IPipe sender, object id, object value)
    {
    IEnumerator e=this.links.GetEnumerator();
    while(e.MoveNext())
    {
    PipePair pipePair=(PipePair)e.Current;
    if(object.ReferenceEquals(pipePair.A,sender))
    {
    pipePair.B.OnNotifyChanged(id,value);
    }
    else if(object.ReferenceEquals(pipePair.B,sender))
    {
    pipePair.A.OnNotifyChanged(id,value);
    }
    }
    }public class Form1 : System.Windows.Forms.Form,IPipe
    {
     //....
     private IPipeManager _manager=null;
     public IPipeManager Manager
     {
     get{return this._manager;}
     set{this._manager=value;}
     } public void OnNotifyChanged(object id, object value){} private void textBox1_TextChanged(object sender, System.EventArgs e)
     {
      if(this.Manager!=null)
      this.Manager.NotifyChanged(this,"Text1Changed",this.textBox1.Text);
     } PipeManager pipeManager=new PipeManager(); Form2 form2=new Form2();
     private void Form1_Load(object sender, System.EventArgs e)
     {
    this.pipeManager.Link(this,form2);
    form2.Show();
     }}public class Form2 : System.Windows.Forms.Form,IPipe
    {
     //....
     private IPipeManager _manager=null;
     public IPipeManager Manager
     {
     get{return this._manager;}
     set{this._manager=value;}
     }
     public void OnNotifyChanged(object id, object value)
     {
      if((id is String) && ((string) id) == "Text1Changed")
      {
       this.textBox1.Text=(string) value;
       }
    }
    }
    //---------------------
    这是一个粒度比较小的方法。