首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

代码段中使用同名变量,这么做难道不会丢失句柄吗?解决方法

2012-01-07 
代码段中使用同名变量,这么做难道不会丢失句柄吗??看下面代码:在构造器中,代码先后使用了btn变量来创建按

代码段中使用同名变量,这么做难道不会丢失句柄吗??
看下面代码:    
在构造器中,代码先后使用了btn变量来创建按钮,它这么做难道不会丢失对btn的句柄吗?    
书中的解释是:同时,因为我对两个按钮使用了两个不同的Click事件处理程序,所以也不需要将按钮对象作为字段存储。我使用同一个btn变量创建两个按钮。    
 
using     System;    
using     System.Drawing;    
using     System.Windows.Forms;    
 
class     TwoButtonsDock:     Form    
{    
                 
                  public     static     void     Main()    
                  {    
                                      Application.Run(new     TwoButtonsDock());    
                  }    
                  public     TwoButtonsDock()    
                  {    
                                      Text     =     "Two     Buttons     with     Dock ";    
                                      ResizeRedraw     =     true;    
 
                                      Button     btn     =     new     Button();    
                                      btn.Parent     =     this;    
                                      btn.Text             =     "&Larger ";    
                                      btn.Height     =     2     *     Font.Height;    
                                      btn.Dock             =     DockStyle.Fill;    
                                      btn.Click     +=     new     EventHandler(ButtonLargerOnClick);    
                                         
                                      btn     =     new     Button();    
                                      btn.Parent     =     this;    


                                      btn.Text             =     "&Smaller ";    
                                      btn.Height     =     2     *     Font.Height;    
                                      btn.Dock             =     DockStyle.Bottom;    
                                      btn.Click     +=     new     EventHandler(ButtonSmallerOnClick);    
                  }    
                  void     ButtonLargerOnClick(object     obj,     EventArgs     ea)    
                  {    
 
                                  Left     -=     (int)(0.05     *     Width);    
                                  Top     -=     (int)(0.05     *     Height);    
                                  Width     +=     (int)(0.10     *     Width);    
                                  Height     +=     (int)(0.10     *     Height);    
                  }    
                  void     ButtonSmallerOnClick(object     obj,     EventArgs     ea)    
                  {    
 
                                  Left     +=     (int)(Width     /     22f);    
                                  Top     +=     (int)(Height     /     22f);    
                                  Width     -=     (int)(Width     /     11f);    
                                  Height     -=     (int)(Height     /     11f);    
                  }    
}    
 
 
 
在看看另一段代码:    


看这段代码构造器里的for循环中,它也用同样的chkbox变量来创建CheckBox控件的对象,但它使用了同样的CheckedChanged事件,这又是怎么回事??    
 
using     System;    
using     System.Drawing;    
using     System.Windows.Forms;    
 
class     CheckBoxWithLabel:     Form    
{    
                  Label     label;    
 
                  public     static     void     Main()    
                  {    
                                      Application.Run(new     CheckBoxWithLabel());    
                  }    
                  public     CheckBoxWithLabel()    
                  {    
                                      Text     =     "CheckBox     Demo     with     Label ";    
 
                                      int                         cyText             =     Font.Height;    
                                      int                         cxText             =     cyText     /     2;    
                                      string[]     astrText     =     { "Bold ",     "Italic ",     "Underline ",     "Strikeout "};    
 
                                      label     =     new     Label();    
                                      label.Parent             =     this;    
                                      label.Text                     =     Text     +     ":     Sample     Text ";    
                                      label.AutoSize     =     true;    
 
                                      for     (int     i     =     0;     i     <     4;     i++)    


                                      {    
                                                          CheckBox     chkbox     =     new     CheckBox();    
                                                          chkbox.Parent     =     this;    
                                                          chkbox.Text     =     astrText[i];    
                                                          chkbox.Location     =     new     Point(2     *     cxText,        
                                                                                                                                                                                          (4     +     3     *     i)     *     cyText     /     2);    
                                                          chkbox.Size     =     new     Size(12     *     cxText,     cyText);    
                                                          chkbox.CheckedChanged     +=        
                                                                                                                      new     EventHandler(CheckBoxOnCheckedChanged);    
                                      }    
                  }    
                  void     CheckBoxOnCheckedChanged(object     obj,     EventArgs     ea)    


                  {    
                                      FontStyle             fs             =     0;    
                                      FontStyle[]     afs         =     {     FontStyle.Bold,                         FontStyle.Italic,        
                                                                                                                          FontStyle.Underline,     FontStyle.Strikeout     };    
 
                                      for     (int     i     =     0;     i     <     4;     i++)    
                                                          if     (((CheckBox)     Controls[i     +     1]).Checked)    
                                                                              fs         &brvbar;=     afs[i];    
 
                                      label.Font     =     new     Font(label.Font,     fs);    
                  }    
}    


[解决办法]
//看见这样的语句搂主是不是更晕了?没有变量就创建了一个按钮

private void Form1_Load(object sender, EventArgs e)
{
new Button().Parent = this;
}

变量是指向

按钮是一个实例

变量和实例并不是一一对应的关系

Button A = button1;
A.Text = "Zswang 路过 ";

变量A和变量button1都是指向同一个实例,所以A.Text改变button1也改变

btn = new Button(); // 新建一个实例
btn = new Button(); // 又新建一个实例,并不会影响前面创建的实例,只是btn的指向发生了变化

热点排行