2

I have a main form that is launched and then it can go to any of the other forms I have created. But the kicker is that I have written a class that I call that returns a string with the name of the form to go to.

Currently I don't have this working so I am going from form to form like this (statically written linking code):

this.Hide();
CloudAccess nextForm1 = new CloudAccess(); 
   //Where CloudAccess is the class of the next form.
nextForm1.ShowDialog();

What I want is something like this:

FormController pick = new FormController();
   //Where FormController is the Class I create an object of and ask what's next
string next = pick.whereToGo(); //lets say it returns "CloudAccess"
this.Hide();
next nextForm1 = new next(); //next is desired to be the contents of the string
nextForm1.ShowDialog();

The problem is that I don't know how to use the returned string to make the new object and use it. I've been looking at Invoke and Reflection topics like this one: Use string value to create new instance But I'm new to C# and I'm not sure how to apply that to this scenario.

Thoughts? Thanks!

5
  • 1
    Just have whereToGo() return a Form, not a string. Controllers should never ever have a problem creating view instances. Commented Jan 1, 2011 at 21:47
  • How would I go about having it return a form? public form whereToGo(){ return CloudAccess; } ? Commented Jan 1, 2011 at 22:04
  • @Dial - it doesn't answer the question. Operating the two-by-four to the forehead is best done in comments, in my experience. Commented Jan 1, 2011 at 22:41
  • @Hans It would be an answer because it'd allow me to get to the same result. I'm just not sure how I'd have the method a form.. Commented Jan 1, 2011 at 22:48
  • @cvo - that goes full circle: why do you have a controller in your code that doesn't know how to create views? Is the object model getting in the way of getting the job done? I have no idea why of course. Commented Jan 1, 2011 at 22:57

2 Answers 2

5

Here's the working code from what fejejosco said:

string asdf = "CloudAccess";
Type CAType = Type.GetType("namespace." + asdf);
Form nextForm2 = (Form)Activator.CreateInstance(CAType);
nextForm2.ShowDialog();

Thanks!

Sign up to request clarification or add additional context in comments.

Comments

1

Get the type of the form: Type.GetType("name.space." + formname), so if your class is name.space.CloudAccessForm, then pass in CloudAccessForm as formname, and it will give you the type. Then you can instantiate it with Activator.CreateInstance(type). Then cast it to a Form, and show it.

2 Comments

string asdf = "CloudAccess"; Type CAType = Type.GetType("NameSpace." + asdf); Activator.CreateInstance(CAType); Form nextForm2 = (Form)CAType; nextForm2.ShowDialog(); this gives me the error: "Cannot convert type 'System.Type' to 'System.Windows.Forms.Form'
Cast the instance, not the type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.