调用api sendmessage 试试

解决方案 »

  1.   

    不用这么复杂,打开form2的时候留一个form2的引用,同时把自己(form1)的引用传递进去,然后两者各自对对方的引用进行相关处理就可以了
      

  2.   

    To:52rainbow(52rainbow) 
    我不太明白怎么引用,是用using吗?还是怎么样?
      

  3.   

    //in Form2 
    Form controlForm
    public Form2(Form controlForm)
    {
       this.controlForm = controlForm;
    }private void SetForm1()
    {
       controlForm.Width = ???;
    ....
    }
    //End Form2//in Form1
    Form2 f2 = new Form2(this); 
    f2.Show();
    f2.Width = ???
    ...
    //方法2:
    使用event
      

  4.   

    我在Form1和Form2里面都写有各自调整尺寸的函数,我想在Form2里调用Form1的函数,在Form1调用Form2的函数,引用的方法行不通,,还有其他解决办法吗?
      

  5.   

    晕,怎么会不行,是你的函数声明的不对,把函数改为public 的试试
      

  6.   

    To: lyl_rabbit(lyl_rabbit)
    那函数是public的,这点我注意到了,
    因为我以前是做WebForm的,最近才转过来。呵呵
      

  7.   

    建立一个类
    /// <summary>
    /// PrcMain 的摘要说明。
    /// </summary>
    public class PrcMain
    {
    public static ArrayList FormArrList = new ArrayList();
                      //静态类阻止实力化。
    private PrcMain()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    [STAThread]
    static void Main() 
    {

    Application.Run(new Form2());
    }
    }。。
    private void Form2_Load(object sender, System.EventArgs e)
    {
    PrcMain.FormArrList.Add(this);
    .................
    private void Form1_Load(object sender, System.EventArgs e)
    {
    PrcMain.FormArrList.Add(this);以后要用的时候到 PrcMain.FormArrList 循环取就行了。
      

  8.   

    public Form1:.....
    {
       public static Form1 f1;
       public static void Main()
       {
           Form1.f1 = new Form1();
           Application.Run(f1);
        }
    }
      

  9.   

    //form1:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication4
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox txtWidth;
    private System.Windows.Forms.TextBox txtHeight;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; private Form2 frm2 ;
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.txtWidth = new System.Windows.Forms.TextBox();
    this.txtHeight = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // txtWidth
    // 
    this.txtWidth.Location = new System.Drawing.Point(32, 40);
    this.txtWidth.Name = "txtWidth";
    this.txtWidth.TabIndex = 0;
    this.txtWidth.Text = "";
    // 
    // txtHeight
    // 
    this.txtHeight.Location = new System.Drawing.Point(192, 40);
    this.txtHeight.Name = "txtHeight";
    this.txtHeight.TabIndex = 1;
    this.txtHeight.Text = "";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(120, 120);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(120, 23);
    this.button1.TabIndex = 2;
    this.button1.Text = "改变Form2尺寸";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(360, 253);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.txtHeight);
    this.Controls.Add(this.txtWidth);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    this.frm2 = new Form2(this);
    this.frm2.Show();
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    this.frm2.Width = Convert.ToInt32(this.txtWidth.Text);
    this.frm2.Height = Convert.ToInt32(this.txtHeight.Text);
    }
    catch(Exception er)
    {
    MessageBox.Show(er.ToString());
    }
    }
    }
    }
    //Form2
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace WindowsApplication4
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox txtHeight;
    private System.Windows.Forms.TextBox txtWidth;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
        private Form1 frm1;
    public Form2(Form1 frm)
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    this.frm1 = frm; //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.txtHeight = new System.Windows.Forms.TextBox();
    this.txtWidth = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(178, 151);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(120, 23);
    this.button1.TabIndex = 5;
    this.button1.Text = "改变Form1尺寸";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // txtHeight
    // 
    this.txtHeight.Location = new System.Drawing.Point(250, 71);
    this.txtHeight.Name = "txtHeight";
    this.txtHeight.TabIndex = 4;
    this.txtHeight.Text = "";
    // 
    // txtWidth
    // 
    this.txtWidth.Location = new System.Drawing.Point(90, 71);
    this.txtWidth.Name = "txtWidth";
    this.txtWidth.TabIndex = 3;
    this.txtWidth.Text = "";
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(440, 245);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.txtHeight);
    this.Controls.Add(this.txtWidth);
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    this.frm1.Width = Convert.ToInt32(this.txtWidth.Text);
    this.frm1.Height = Convert.ToInt32(this.txtHeight.Text);
    }
    catch(Exception er)
    {
    MessageBox.Show(er.ToString());
    }
    }
    }
    }
      

  10.   

    就是hanbinghai说的那样了
    不过太……
    为什么要全部贴出来!!
    %%
      

  11.   

    用构造函数传值的方法好象不能调用另外一个窗体的public函数
    在Form1里面有一个public函数,我想在Form2里面执行,但是语法提示里面找不到Form1里面的那个public的函数
    上面的代码的方法行不通啊