CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

问过多次了(不气馁),如何做到多个窗体使用一个公用的全局变量啊?

楼主ATLSurvival(花开花落两不知)2003-08-04 11:49:39 在 .NET技术 / C# 提问

一个全局的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

相关问题

  • vb中向窗体传递参数(不使用公用变量)?
  • 关于窗体变量
  • 窗体变量的释放问题
  • 窗体之间变量转换问题
  • 怎样用变量控制窗体?????!!!!!?????????????
  • 请教窗体间变量传递
  • 子窗体如何访问父窗体的变量?
  • 公用变量问题
  • 如何在一窗体中引用另一窗体中的变量?
  • 如何把变量的值从一个窗体传递到另一个窗体

关键词

  • 函数
  • 代码
  • button
  • 窗体
  • 变量
  • disposing
  • textbox
  • drawing
  • form
  • myevent

得分解答快速导航

  • 帖主:ATLSurvival

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo