问过多次了(不气馁),如何做到多个窗体使用一个公用的全局变量啊?
一个全局的int,string,Web Services的引用变量
如何使上面的三个冬冬,能在所有的窗体中都可以访问啊?
新手问题,55~~~~~~~
问题点数:0、回复次数:10Top
1 楼lbx1979(Love Arsenal)回复于 2003-08-04 11:52:26 得分 0
用静态变量应该可以Top
2 楼snof(雪狼)回复于 2003-08-04 11:57:44 得分 0
public static int i;
其它如上Top
3 楼jeng(乐去)回复于 2003-08-04 12:53:37 得分 0
搂住可能被自己给误导了
class Form1{..}
class Form2{..}
class Form3{..}
class ArrageForms{ Form1 form1;
Form2 form2;
Form3 form3;
Var var;}
Top
4 楼qianli918(千里马)回复于 2003-08-04 12:55:38 得分 0
?Top
5 楼pinghero(追风)回复于 2003-08-04 13:33:33 得分 0
使用静态变量 如果你要例子,我这里有一个不同角色登陆获得不同权限的例子,可以发给你Top
6 楼jinye(锦业)回复于 2003-08-04 13:35:44 得分 0
用静态变量Top
7 楼gujianxin(木头象)回复于 2003-08-04 13:35:53 得分 0
// 公用数据
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
static string t;
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void setValue(string sNew)
{
t = sNew;
}
public static string getValue()
{
return t;
}
}
//form1
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 16);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(96, 128);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(144, 40);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_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.button2,
this.button1});
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 fm = new Form2();
fm.MyEvent += new MyCallBack(Test);
fm.Show();
}
public static void ShowResult()
{
MessageBox.Show(Class1.getValue());
}
private void Test(string message)
{
MessageBox.Show(message);
}
}
//Form2 (or else)
public delegate void MyCallBack(string s );
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public event MyCallBack MyEvent;
/// <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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(224, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 96);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(168, 40);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(48, 168);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(112, 24);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_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.button2,
this.button1,
this.textBox1});
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
Class1.setValue(this.textBox1.Text);
this.Visible = false;
Form1.ShowResult();
this.Close();
}
private void button2_Click(object sender, System.EventArgs e)
{
if(MyEvent != null)
MyEvent(textBox1.Text);
}
}
Top
8 楼vikey(我心飞翔)回复于 2003-08-04 13:39:28 得分 0
同意楼上各位Top
9 楼qianblue(浅蓝)回复于 2003-09-10 17:10:46 得分 0
用公共类
public class SysInfo
{
public static string solution ="";
public static string project ="";
public static string id ="";
public static string title ="";
public static string schemefile ="";
public static bool brefresh=false;
public static ArrayList schemelist =new ArrayList();
public static int listindex=0;
public static bool bReadOnly=false;
public SysInfo()
{}
}
Top
10 楼fgc5201314(成成(努力中...))回复于 2003-09-10 18:03:54 得分 0
呵呵,你以前是用VB的吗?思想还没有转过来啊!Top




