2017年3月17日 星期五

[視窗介面] VC++ object array

最近在寫visual studio c++ windows 程式, 以前就是一個物件一個程式,但後來覺得相同的物件應該可以寫成陣列的方式了,這樣管理起來也比較方便,研究了一下:
1、button event:
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    Button^ thisBox = safe_cast<Button^>(sender);
    textBox2->Text = thisBox->Text;
}
2、radioButton:
比較高級的寫法
private: System::Void radioButton4_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {

     array<RadioButton ^, 2>^buttons = { { radioButton4, radioButton5, radioButton6 },{ radioButton7, radioButton8, radioButton9 } };

     int i;
     for (i = 0; i < 3; i++) {
         if (buttons[0,i]->Checked) textBox3->Text = "Group1OK" + i.ToString();
     }
     for (i = 0; i < 3; i++) {
         if (buttons[1, i]->Checked) textBox2->Text = "Group2OK" + i.ToString();
     }
}

更高級的寫法
private: System::Void radioButton4_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {

     array<RadioButton ^, 2>^buttons = { { radioButton4, radioButton5, radioButton6 },{ radioButton7, radioButton8, radioButton9 } };
     array<TextBox ^>^textboxs = { textBox3, textBox2 };

     int i,j;
     for (j = 0; j < 2; j++) {
         for (i = 0; i < 3; i++) {
              if (buttons[j,i]->Checked) textboxs[j]->Text = "Group" + j.ToString() + "OK" +i.ToString();
         }
     }
}


沒有留言:

張貼留言