Use value of a string to create new form object - c#

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!

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!

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.

Related

How to generate a link to a html page with parameters WebApi

I'm trying to create a ResetPassword Page and I need to create something like that!
myApi.azure.com/ResetPassword?hash=YYYYYYYYYYYYYY
I already know how to create a link to another controller, but that way it would trigger the Action just with the click, and what I need is pass the hash as parameter inside of that URL and them, call a controller!
var link = new Uri(Url.Link("ValidationEmailUser", new { Code = emailToken }));
Something like this:
public IHttpActionResult RedirectAction()
{
var urlFormat = string.Format("https://www3.olx.com.br/account/forgotten_password/?hash={0}", emailToken);
var location = new Uri(urlFormat);
return this.Redirect(location);
}

Accessing WinForm Controls using C# dynamic code

I have WinForm called Form1 and there are Button1, MemoEdit1 and 2 TextBoxes named TextBox1 and TextBox2. At runtime user should be able write C# code in MemoEdit1 in order to manipulate the TextBox controls. F.e: at runtime user typed into MemoEdit1 simple code like: TextBox2.Text = "Hello" + TextBox1.Text;
So, when I click on Button1, I need to compile and execute the code.
Question may sound so simple as I am a newbie in compiling/executing code during runtime in C#.
Could you pls, help?
Thanks.
Take a look on this snippet
public class Evaluator
{
public void Eval(string Code)
{
Microsoft.CSharp.CSharpCodeProvider Provider = new Microsoft.CSharp.CSharpCodeProvider(); // Create an provider
System.CodeDom.Compiler.ICodeCompiler Compiler = Provider.CreateCompiler(); // Create An Compiler
System.CodeDom.Compiler.CompilerParameters Parameters = new System.CodeDom.Compiler.CompilerParameters(); // Create a parameters of the compiler
Parameters.GenerateInMemory = true; // It should generate the compiled assembly in the memory
System.CodeDom.Compiler.CompilerResults Results = Compiler.CompileAssemblyFromSource(Parameters, Code); //Compile it
///Now you just need to use reflection to call its methods
object SomeClass = Results.CompiledAssembly.CreateInstance("ClassName"); //Name of the class you want to create an instance
var Method = SomeClass.GetType().GetMethod("MethodName"); //Name of the Method you want to call
Method.Invoke(SomeClass, null); // change null for the argument it needs
}
}
if you want to just write code you will have to add the an class and a Method to wrap the user code and then you call it through the Invoke, you will probably have to reference your own assembly into this assembly

Different class name in WinForm and WebService

I have a class defined that I use for loading information of the database as BO. I use the same DLL (I have it in a different project) for both, the WinForm app and the WebService.
The problem is that when I retrieve the info executing the WS, the name of the class comes with the namespace and the WS name included, and makes me convert from one object to another... but IS THE SAME!!!
List<**Trevo.FrameWork.MiAutoWS.ParametrosBOL**> oWs = obj.Parametros();
List<**ParametrosBOL**> x = new List<ParametrosBOL>();
foreach (Trevo.FrameWork.MiAutoWS.ParametrosBOL o in oWs)
{
ParametrosBOL oNuevo = new ParametrosBOL();
oNuevo.IDEmpresa = o.IDEmpresa;
oNuevo.IDTipo = o.IDTipo;
oNuevo.Tipo = o.Tipo;
oNuevo.IDCodigo = o.IDCodigo;}
}
Is there any way to avoid this?
Thanks in advance.

how can i turn a string into an class reference in c#? [duplicate]

This question already has answers here:
Creating an object from from an ID (or name)
(5 answers)
Closed 9 years ago.
in c# i am working on an text based adventure where each tile is represented by a class. in my main class i have inserted a way of reading from the console and in a class that i have called tile loader i have an update function. this function gets a string from the input read at the start. when i call it i want the string to be converted into an class reference so that i can call the class and an load function witch is inside of each tile class. i could just go ahead and insert a lot of if's but i don't really want to do that. i know there is an easier way.
You can use Activator.CreateInstance to do this.
You will need to provide an assembly name or reference and may need to add the namespace for the class unless you want to provide the fully qualified class in the command line.
For example, assuming that the class Test is in an assembly called WindowsFormsApplication1 and the user entry is in a variable called sInputClassName, the following code will create an instance of the class in the oClass variable:
// What the user entered
var sInputClassName = "Test";
// The name of the assembly; there are other ways to get this, such as through reflection
const string CLASS_ASSEMBLY_NAME = "WindowsFormsApplication1";
// Get the requested type from the entered class name and assembly name
var oType = Type.GetType(CLASS_ASSEMBLY_NAME + "." + sInputClassName, true);
if (oType != null) {
// Once we have the class type, create an instance of it
var oClass = Activator.CreateInstance(oType, false);
if (oClass != null) {
System.Diagnostics.Debug.WriteLine("Created " + sInputClassName);
} else {
System.Diagnostics.Debug.WriteLine("Could not create " + sInputClassName);
}
} else {
System.Diagnostics.Debug.WriteLine("Could not find " + sInputClassName);
}

NullReference Exception unhandled. C# Reflection

ControlType = "System.Windows.Forms.WindowsFormsApplication1." + "PictureBox1";
System.Reflection.Assembly asm;
asm = typeof(Form).Assembly;
ControlObject = (System.Windows.Forms.Control)asm.CreateInstance(ControlType);
ControlObject.Name = ControlName;
The next code generated following exception for me:
ControlObject.Name = ControlName;
NullReferenceException was unhandle
Object reference not set to an instance of an object.
Assembly.CreateInstance is expecting a type name and you appear to be passing it the name of an instance of a type (namely, a PictureBox named PictureBox1.). Therefore, ControlObject is null and thus ControlObject.Name will throw a NullReferenceException.
It's not clear what you're trying to do, but that is why you are encountering the problem that you are. If you're trying to create a new instance of PictureBox I don't see why you don't just say new PictureBox(); this class has a public parameterless constructor. Alternatively, if you insist on reflection, you could say
controlType = PictureBox1.GetType();
controlObject = Activator.CreateInstance<Control>(controlType);
We could help more if we knew what you were trying to do instead of just throwing code that doesn't work at us and expecting us to solve world hunger.
Additionally,
ControlType = "System.Windows.Forms.WindowsFormsApplication1." + "PictureBox1";
Please rename this variable to controlType. You should use camel case for variable names.
Why do you have your application class WindowsFormsApplication1 living in the system namespace System.Windows.Forms? Don't do this.
You're probably trying to write
ControlObject = new PictureBox();
Looks like CreateInstance returns null, which means the type wasn't found in the assembly. Is PictureBox1 a type or an object?
Surly your application is not in System.Windows.Forms namespace
"System.Windows.Forms.WindowsFormsApplication1." + "PictureBox1"
Try:
ControlObject = (System.Windows.Forms.Control)asm.CreateInstance(typeof(PictureBox));
or just
ControlObject = new PictureBox();
to create a new instance of control.
Or maybe you want to find an existing PictureBox control on your form?
It means that asm.CreateInstance(ControlType); is returning null.
So, ControlType has a wrong value. It is supposed to recieve as parameter a type http://msdn.microsoft.com/en-us/library/dex1ss7c.aspx and it seems that you are sending an instance PictureBox1.
It should be ControlType = "System.Windows.Forms.PictureBox";

Categories