본문 바로가기

.NET/C# Form

C# 버튼 메서드 Click 이벤트를 강제로 실행하는 PerformClick 사용예

반응형









namespace buttonTest
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  Int32 myVar=0;

  private void button1_Click(object sender, EventArgs e)
  {
   // If myVar is an even number, click Button2.
   if (myVar % 2 == 0)
   {
    MessageBox.Show("1번버튼 효과 없음");
    button2.PerformClick();
    // Display the status of Button2's Click event.
    
   }
   else
   {
    // Display the status of Button2's Click event.
    MessageBox.Show("1번버튼 눌러짐  홀수");
   }
   
  }

  private void textBox1_TextChanged(object sender, EventArgs e)
  {
   myVar = Convert.ToInt32(textBox1.Text);
  }

  private void button2_Click(object sender, EventArgs e)
  {
   MessageBox.Show("2번 버튼 짝수 ");
  }
 }