2

我是C# 的新手,我有一个小项目。我被困在某个地方。我在这里解释了它(带有示例源代码):

我有一个表格申请。我要求用户从 2 个按钮中选择一个选项。有 2 个按钮(是和否)。我的代码是这样的:

public partial class Form1 : Form
{
   public int choice=0;
   public Form1()
   {
      if(choice == 0)
      {
         label.Text = "Please push one of these buttons :";
         // And there are buttons below this label
      }
      else if(choice == 1)
      {
         label.Text = "You just pushed YES button";
      }
      else if(choice == 2)
      {
         label.Text = "You just pushed NO button";
      }
   }

   private void buttonYes_Click(object sender, EventArgs e)
   {
       choice = 1;
       /*
          I have to use one of these here for redraw whole form
          this.Refresh();
          this.Invalidate();
       */
   }
   private void buttonNo_Click(object sender, EventArgs e)
   {
       choice = 2;
       /*
          I have to use one of these here for redraw whole form
          this.Refresh();
          this.Invalidate();
       */
   }
} 

如您所见,当用户单击“是”或“否”按钮之一时,应重新执行整个构造函数。标签应该是“你刚刚按下是/否按钮”。

但是当我使用 this.Refresh() 时,当我点击按钮时什么也没有发生。仍然标签是“请按下这些按钮之一:”。

当我使用 this.Invalidate() 时,所有按钮都消失了,标签仍然是“请按下这些按钮之一:”。

我该怎么办 ?

谢谢。

PS 我在问这个问题之前发现了这个问题。但如您所见,接受的答案对我不起作用。

4

4 回答 4

6

无效或刷新不会再次调用构造函数。创建表单时只调用一次构造函数,并且无效不会创建新表单。将更改内容的逻辑放在另一个方法中,并从构造函数和事件处理程序调用它——但请注意后人调用实例方法或从构造函数访问变量并不是做这些事情的最佳方式——而是您的目的是简单的解决方案。

public partial class Form1 : Form
{
   public int choice=0;
   public Form1()
   {
      UpdateForm();
   }
   private void UpdateForm(){
      if(choice == 0)
      {
         label.Text = "Please push one of these buttons :";
         // And there are buttons below this label
      }
      else if(choice == 1)
      {
         label.Text = "You just pushed YES button";
      }
      else if(choice == 2)
      {
         label.Text = "You just pushed NO button";
      }
   }
   private void buttonYes_Click(object sender, EventArgs e)
   {
       choice = 1;
       /*
          I have to use one of these here for redraw whole form
          this.Refresh();
          this.Invalidate();
       */
       UpdateForm();
   }
   private void buttonNo_Click(object sender, EventArgs e)
   {
       choice = 2;
       /*
          I have to use one of these here for redraw whole form
          this.Refresh();
          this.Invalidate();
       */
      UpdateForm();

   }
} 
于 2013-04-04T20:12:47.953 回答
3

如果您想保留与示例中相同的变量,这是最佳选择。如果您想要更短的版本,只需直接在Form1 Constructor.

public partial class Form1 : Form
{
   public int choice=0;

   public Form1()
   {
       buttonYes.Click += (s,e) => { 
       choice = 1; 
       ChangeText(choice);};

       buttonNo.Click += (s,e) => {
       choice = 2;
       ChangeText(choice);};
   }


  private void ChangeText(int userChoice)
  { 
    if(choice == 0)
         label.Text = "Please push one of these buttons :";

      else if(choice == 1)
         label.Text = "You just pushed YES button";

      else if(choice == 2)
       label.Text = "You just pushed NO button";
  }

} 

较短的版本

public partial class Form1 : Form
{
  public Form1()
  {
    label.Text = "Push a button";
    buttonYes.Click += (s,e) => {label.Text = "Yes is pressed";};
    buttonNo.Click += (s,e) => {label.Text = "No is pressed";};
  }
}
于 2013-04-04T20:17:20.893 回答
3

这就是你真正应该这样做的方式:

public partial class Form1 : Form
{
   public Form1()
   {
       // Isn't there supposed to be InitializeComponent() here?

       // You should assign this in the designer, rather than here.
       label.Text = "Please push one of these buttons :";
   }

   private void buttonYes_Click(object sender, EventArgs e)
   {
       label.Text = "You just pushed YES button";
   }
   private void buttonNo_Click(object sender, EventArgs e)
   {
       label.Text = "You just pushed NO button";
   }
}

由于按钮所做的只是更改标签,因此应该直接完成,而不是更改变量并刷新。

但为什么它没有按我的方式工作?

所有 Refresh 和 Invalidate 所做的就是重绘表单上已有的内容。他们不会重新创建它。

构造函数旨在在创建对象时对其进行一次初始化。不能调用它来“重新初始化”或“刷新”对象。

为避免过于详细,我建议您查找有关面向对象编程的文章/书籍,以了解有关构造函数和其他 OOP 习惯用法的更多信息。

于 2013-04-04T20:18:57.840 回答
0

创建新对象时执行主构造函数。

使用方法来做到这一点。我在岸上,这行得通

       public string TextSwitcher(int choice)
       {
          Switch(choice) // choice is an int 
          {
// 1,2,3 is not an serial no they will pass by parameter  
             case(1):
              return "Please push one of these buttons :";
             brake;
             case(2):
              return = "You just pushed YES button";
             brake;
             case(3)
              return = "You just pushed NO button";
             brake;
          }
       }

   private void buttonYes_Click(object sender, EventArgs e)
   {
       label.Text = TextSwitcher(2);
   }
   private void buttonNo_Click(object sender, EventArgs e)
   {
       label.Text = TextSwitcher(3);
   }

我希望这会对你有所帮助。并欢迎你的感谢。

祝你好运

于 2013-04-04T22:21:25.180 回答