2

私はC# の初心者で、小さなプロジェクトを持っています。私はどこかで立ち往生しています。ここで説明しました(サンプルソースコード付き):

フォームアプリケーションがあります。ユーザーに 2 つのボタンからオプションを選択してもらいます。2 つのボタン (YES と NO) があります。私のコードは次のようになります:

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();
       */
   }
} 

ご覧のとおり、ユーザーが YES または NO ボタンのいずれかをクリックすると、コンストラクター関数全体が再実行されます。ラベルは「You just push YES / NO button」である必要があります。

しかし、 this.Refresh() を使用すると、ボタンをクリックしても何も起こりません。まだラベルは「これらのボタンのいずれかを押してください:」です。

this.Invalidate() を使用すると、すべてのボタンが消え、ラベルは「これらのボタンのいずれかを押してください:」のままです。

私は何をすべきか ?

ありがとう。

PS 私はこれを尋ねる前にこの質問を見つけました。しかし、ご覧のとおり、受け入れられた答えは私にとってはうまくいきません。

4

4 に答える 4

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 に答える