用公共类。
namespace Police_Service
{
public class userinfo
{
public static string userid="";
public static string username="";
public static string userpass="" ;
public static string quanxian="" ;
public static string logindate="" ;
public static bool blogin=false ;
public static string sername="" ;
}
}
直接赋值和引用。
userinfo.username="" //赋值
userinfo.username//引用
namespace Police_Service
{
public class userinfo
{
public static string userid="";
public static string username="";
public static string userpass="" ;
public static string quanxian="" ;
public static string logindate="" ;
public static bool blogin=false ;
public static string sername="" ;
}
}
直接赋值和引用。
userinfo.username="" //赋值
userinfo.username//引用
Form2 f2=new Form2(this);
f2.show();
Form2中
private Form1 f1=null;
构造函数
Form2(Form1 temp)
{
......
this.f1=temp;
}
然后,在你要调用的时侯,就可以用f1.相应属性或方法!
例如:
public class frmAccountSetup : System.Windows.Forms.Form
{
//----------公有变量----------
public bool newAccount;
public string accountName;
……
} public class frmSetup : System.Windows.Forms.Form
{
…… private void btnAddUser_Click(object sender, System.EventArgs e)
{
frmAccountSetup accountSetupObj = new frmAccountSetup();
accountSetupObj.newAccount = true; //添加用户
……
accountSetupObj.ShowDialog(this);
……
}
……
}以上是直接定义public成员变量,并直接进行操作。也可以定义一堆private变量,提供一堆public方法对那些变量进行操作……
public struct ShareData
{
//定义你的共享数据
}form1的类中
public ShareData myData;在form2的类中
public form1 myForm1;form1新建form2时,
form2.myForm1=this;那么,窗体form1可以用“this.myData.”,而窗体form2可以用“this.myForm1.myData.”。
欢迎在编程爱好者上发帖、回帖
http://pgfans.126.com
使用SetDataObject方法发送数据到剪贴板
button_click(object s,EventArgs e)
{
Clipboard.SetDataObject(textBox1.text);
}
在用GetDataObject方法取
button2_click()
{
IDataObject iData = Clipboard.GetDataObject();
textbox2.text=(string)iData.GetData(DataFormats.Text);
}
先建一个项目名称为"传值"代码如下,有两个窗体.即可运行
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 传值
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnShowForm;
/// <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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnShowForm = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// btnShowForm
//
this.btnShowForm.Location = new System.Drawing.Point(160, 80);
this.btnShowForm.Name = "btnShowForm";
this.btnShowForm.TabIndex = 1;
this.btnShowForm.Text = "ShowForm";
this.btnShowForm.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.AddRange(new System.Windows.Forms.Control[] {
this.btnShowForm,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Form2 f=new Form2();
f.thisForm=this;
f.Show();
} public string sTextValue
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text=value;
}
}
}
}
第二个:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace 传值
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnGetValue;
private System.Windows.Forms.Button btnSetValue;
//添加Form1的类型变量
private Form1 m_Form=null;
//添加属性
public Form1 thisForm
{
get
{
return m_Form;
}
set
{
m_Form=value;
}
}
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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.btnGetValue = new System.Windows.Forms.Button();
this.btnSetValue = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(216, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// btnGetValue
//
this.btnGetValue.Location = new System.Drawing.Point(72, 64);
this.btnGetValue.Name = "btnGetValue";
this.btnGetValue.TabIndex = 1;
this.btnGetValue.Text = "GetValue";
this.btnGetValue.Click += new System.EventHandler(this.button1_Click);
//
// btnSetValue
//
this.btnSetValue.Location = new System.Drawing.Point(152, 64);
this.btnSetValue.Name = "btnSetValue";
this.btnSetValue.TabIndex = 2;
this.btnSetValue.Text = "SetValue";
this.btnSetValue.Click += new System.EventHandler(this.btnSetValue_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(240, 141);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnSetValue,
this.btnGetValue,
this.textBox1});
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
this.textBox1.Text=m_Form.sTextValue;
} private void btnSetValue_Click(object sender, System.EventArgs e)
{
m_Form.sTextValue=this.textBox1.Text;
}
}
}