请教关于两窗体互相控制的问题,注:WinForm,非MDI窗体。多谢 调用api sendmessage 试试 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不用这么复杂,打开form2的时候留一个form2的引用,同时把自己(form1)的引用传递进去,然后两者各自对对方的引用进行相关处理就可以了 To:52rainbow(52rainbow) 我不太明白怎么引用,是用using吗?还是怎么样? //in Form2 Form controlFormpublic Form2(Form controlForm){ this.controlForm = controlForm;}private void SetForm1(){ controlForm.Width = ???;....}//End Form2//in Form1Form2 f2 = new Form2(this); f2.Show();f2.Width = ???...//方法2:使用event 我在Form1和Form2里面都写有各自调整尺寸的函数,我想在Form2里调用Form1的函数,在Form1调用Form2的函数,引用的方法行不通,,还有其他解决办法吗? 晕,怎么会不行,是你的函数声明的不对,把函数改为public 的试试 To: lyl_rabbit(lyl_rabbit)那函数是public的,这点我注意到了,因为我以前是做WebForm的,最近才转过来。呵呵 建立一个类 /// <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 循环取就行了。 public Form1:.....{ public static Form1 f1; public static void Main() { Form1.f1 = new Form1(); Application.Run(f1); }} //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()); } } }}//Form2using 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()); } } }} 就是hanbinghai说的那样了不过太……为什么要全部贴出来!!%% 用构造函数传值的方法好象不能调用另外一个窗体的public函数在Form1里面有一个public函数,我想在Form2里面执行,但是语法提示里面找不到Form1里面的那个public的函数上面的代码的方法行不通啊 程序中窗体数据的填充与保存,大家是如何处理的? List.foreach 问题 C# 游戏取色 C# DataGrid控件 增 删 改 查 请问在C#中如何调用数据连接属性对话框。不要用COM。已寻觅良久,未果啊! 请钩子高手帮助,我写的钩子函数为什么有问题?? 哪位大哥救我啊,一天没解决!记录更新失败! 请问怎么更改查询分析器默认的数据库 图片放大 怎样使DLL文件中的一个方法在另一个DDL文件中被使用? 在winform中如何获取和设置鼠标在Form中的位置 WMS(warehouse managerment system)仓库管理系统,quickbooks
我不太明白怎么引用,是用using吗?还是怎么样?
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
那函数是public的,这点我注意到了,
因为我以前是做WebForm的,最近才转过来。呵呵
/// <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 循环取就行了。
{
public static Form1 f1;
public static void Main()
{
Form1.f1 = new Form1();
Application.Run(f1);
}
}
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());
}
}
}
}
不过太……
为什么要全部贴出来!!
%%
在Form1里面有一个public函数,我想在Form2里面执行,但是语法提示里面找不到Form1里面的那个public的函数
上面的代码的方法行不通啊