1,
form2 fm = new form2();
fm.show();form1.fm1 = new form1();
fm1.xx = "xxx";//xx必须是public类型变量
form2 fm = new form2();
fm.show();form1.fm1 = new form1();
fm1.xx = "xxx";//xx必须是public类型变量
解决方案 »
- c# app.config 中其他节点使用appSettings中添加的值
- C#查找webbrowser中的对话框
- C#怎样读取数据库中的图片?(看清说明再回答!)
- Crystal Report 11与高速打印机的配合问题,在线等!
- 知道一个窗口的名字,即字符串类型(string strFormName),如何实例化这个窗口(打开一个窗口)?
- 求助:点击一个button,触发页面的元素操作
- 一个exe应用程序在运行时如何生成另一个exe应用程序
- 关于打印的问题(顶者有分)
- 对NET的迷惑,请高人指教
- c# 基于TcpClient和TcpListener通讯模式下的错误识别
- 关于数据备份的问题,希望各们高手多多帮忙!!
- 在c#中怎么使用SENDMESSAGE获得窗口的标题?
Form1 主要代码:
------------------------------------------------------------------------
public int a=0;
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
Form2 fm=new Form2 ();
fm.Show ();
MessageBox .Show (a.ToString ());
} }
}
----------------------------------------------------------------------Form2主要代码:
--------------------------------------------------------
private void Form2_Load(object sender, System.EventArgs e)
{
Form1 fm =new Form1 ();
fm.a=10;
}
-------------------------------------------------------------
结果MessageBox显示FORM1中的a还是0;
form2 fm = new form2();
fm.fm = this;
fm.show();form2public form1 fm = new form1();
fm.a = "sdfsfa";
Form2 b并不包含对fm的定义!
那么就
b.fm = this;
如果FORM1是启动窗口
那么FORM1可以更改FORM2里面的变量;
而FORM2就不可以更改FORM1里面的变量;
FORM1.CS
//-------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication8
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{ public int a=10;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(152, 56);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
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)
{
Form2 fm=new Form2 ();
fm.Show ();
} private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show (a.ToString ());
}
}
}
//--------------------
FORM2.CS
//--------------------using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace WindowsApplication8
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
Form1 fm=new Form1 ();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// 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()
{
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
fm.a=10000;
}
}
}
//-------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication8
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{ public int a=10;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(152, 56);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
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)
{
Form2 fm=new Form2 ();
fm.fm1 = this;
fm.Show ();
}FORM2.CS
//--------------------using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace WindowsApplication8
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
Form1 fm=new Form1 ();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1 fm1 = new Form1(); public Form2()
{
//
// 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()
{
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
fm1.a=10000;
}
}
}
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show (fm1.a.ToString ());
}
}
}
{
MessageBox.Show (a.ToString ());
}
}
}
这段是form1.cs的
{
Form1 fm=new Form1 ();
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1 fm1 = new Form1(); //form2.cs在这个地方定义公共变量
BeginForm b = new BeginForm();
Application.Run(b);
加在Application.Run(new MainForm())的前面
你也可以加上一个if语句,可以判断是不是要启动主窗体
建议你用这样的顺序,不然会比较麻烦的2.要想更改一个窗口的变量,可以在这个窗口里面加一个自定义属性(把这个变量当一个属性),然后在另一个窗口里面set就可以了3.现在N多断口扫描器
3.我上面没有说清楚,我说的测试端口是否打开是指如何用C#实现,而不是借助其他软件
请问有什么办法吗?
就是我要做一个程序(如APP1)
程序(APP1)运行的时候就打开一个特定的端口监听
而另外一个程序(APP2)运行的时候,就要扫描一次局域网里所有的计算机
已确定有那些计算机在运行APP1
谢谢!