lundi 6 juin 2022

How to randomly change a form using relfection from a specified list of forms?

I want to write a code that pops up a random form from a specified list of forms when a button is pressed in winforms.

The forms in the specified list include Form1, Form2, and Form3. It must not be moved to a form other than the specified forms.

The code I'm using now works using switch() and if(). However, when increasing or decreasing the number of Forms, it is cumbersome to modify all kinds of places.

So I thought, how can I solve this using reflection?

I know how to change the form.

private void ChangeForm()
{
  var form = new Form1();
  form.ShowDialog();
}

The part I'm stuck on is that I'm not sure what to do when I use the new keyword to specify the form I want to change.

Any help would be appreciated.





Aucun commentaire:

Enregistrer un commentaire