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