再帰関数と2つのイベント(Going_in
およびComing_out
)があります。
Going_in
関数がそれ自体を呼び出すたびにイベントを使用してプログレスComing_out
バーをインクリメントし、関数が再帰から戻るたびにプログレスバーをデクリメントするために使用します。
今、私は1つのイベントに結合する必要がGoing_in
あります。Coming_out
どうやってやるの?
前もって感謝します!
これがコードの一部です。
Form1.cs
.....
void ProgressBar_increment(object sender, EventArgs e)
{
progressBar1.Value++;
}
void ProgressBar_decrement(object sender, EventArgs e)
{
progressBar1.Value--;
}
public void button2_Click(object sender, EventArgs e)
{
initialize();
label3.Visible = false;
int wait_time = telltime();
int number = reading();
Facto mth;
mth = new Facto(label3, wait_time, progressBar1);
mth.Going_in += new EventHandler(ProgressBar_increment);
mth.Coming_out += new EventHandler(ProgressBar_decrement);
int result = mth.Factorial(number);
string display = result.ToString();
label3.Visible = true;
label3.Text = display;
}
Facto.cs
public event EventHandler Going_in;
public event EventHandler Coming_out;
........
public int Factorial(int number_to_calculate)
{
int Result;
if (Going_in != null)
{
Going_in(this, new EventArgs());
}
System.Threading.Thread.Sleep(wait_time);
if (number_to_calculate == 0)
{
if (Coming_out != null)
{
Coming_out(this, new EventArgs());
}
return 1;
}
else
{
Result = (number_to_calculate * Factorial(number_to_calculate - 1));
if (label_for_output != null)
{
label_for_output.Visible = true;
label_for_output.Text = Result.ToString();
label_for_output.Update();
}
else
Console.WriteLine(Result);
}
if (Coming_out != null)
{
Coming_out(this, new EventArgs());
}
System.Threading.Thread.Sleep(wait_time);
return Result;
}