c# - Open form based on int -


I'm just starting Project Euler and have already participated in my first issue. I have to give a different look for every Euler problem, but I do not know how to open the form What I want to do in a good way, use the variable problem number to open a form. For example, if I have a problem, then number 54 is set and I press a button, it should open Form 54.

  using the system; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Linq; Using System.Text; System usage threading. Task; Using System.Windows.Forms; Namespace WindowsFormsApplication1 {Public Partial Category Form 1: Form {Public Form 1 () {InitializeComponent (); } Private Zero Button 1_Click (Object Sender, EventArgs e) {int problemNumber = int.Parse (numericUpDown1.Text); }}}  

I know how to open a particular form, for example form2.

  form2 f2 = new form2 (); F2.Show ();  

This will open Form 2, but as above, I want to open Form + Problem number.

Thank you!

One possibility is to create a static class, in which there is a fixed method with a switch case and one Returns form object. In the method, you create a new form object about the specified number:

  Public Static Class Farmstarter {public static form open form (int form number) {form form = empty; Switch (form number) {case 1: form = new form 1 (); break; Case 2: Form = New Form 2 (); break; // case 3: form = new form 3 (); break; // ... Default: throwing a new argument exception (); } Return form; }}  

You can then use the class like this:

  var f = FormStarter.OpenForm (2); F.ShowDialog (); // Form 2 has started!  

Once you have a new form, you only need to add an example in one method - OpenForm .

One possible solution without another stable class will be a dictionary and the form of this type of form, which holds the object:

  Lt; Int, form & gt; Form = new dictionary & lt; Int, form & gt; (); Forms.Add (1, New Form 1 ()); Forms.Add (2, new form 2 ()); Forms [2] .howdialog (); // Form 2 has started!  

If you want to refer to the form only as a word and if you only make an example when necessary, then change the value of the word type and Enter the related form in it:

  dictionary & lt; Int, type & gt; Appearance = new dictionary & lt; Int, type & gt; (); Forms.Add (1, Typef (Form 1)); Forms.Add (2, Typef (Form 2)); . (Forms) Activator.CreateInstance (forms [2])) ShowDialog (); // Form 2 has started!  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -