Results 1 to 7 of 7

Thread: how can i instantiate a form through its name[Resolved]

  1. #1

    Thread Starter
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    how can i instantiate a form through its name[Resolved]

    here...
    VB Code:
    1. 'frmname="Form1"
    2.         public void Show(string frmname)
    3.         {
    4.             System.Windows.Forms.Form f;
    5.             f.Name=frmname;
    6.             '....here, how can i identify when i instantiate that
    7.             'it is pointing to Form1
    8.             ' supposed to be it looks like this...Form1 f=new Form1();
    9.             'then Show();
    10.         }
    is this possible? or maybe i'm in the wrong way.
    for now i have no idea on it.
    Last edited by fret; May 23rd, 2005 at 12:40 AM.

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Location
    Montréal
    Posts
    160

    Re: how can i instantiate a form through its name

    You don't need to instantiate a new form if you only want to point to another form. All you have to do is to assign your other form to your new variable :
    Code:
    public void Show(System.Windows.Forms.Form formInit)
    {
    	System.Windows.Forms.Form f;
    	f = formInit;
    	f.Show();
    }
    There are no stupid questions, but a whole bunch of dumb sayings !

    Save time on database code, try DataLG !

  3. #3

    Thread Starter
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: how can i instantiate a form through its name

    thanks for the info you give sixfeet but i need to pass a string and through that string name it reflect to the name of a form.

  4. #4
    Lively Member
    Join Date
    May 2004
    Location
    malaysia
    Posts
    89

    Re: how can i instantiate a form through its name

    i dont think you can instantiate a form through its name, however you need to instantiate a form first, then you can assign a form name to it.

    anyway, you cannot show a form before you instantiate a form object.

    correct me if i am wrong

  5. #5

    Thread Starter
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: how can i instantiate a form through its name

    thanks for your info kkc, maybe your right i just wondering if there such way. hehehe.

  6. #6
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: how can i instantiate a form through its name

    Yes There is a way... it involves reflection.

    Code:
    		static void Main() 
    		{
    			//Application.Run(new Form1());
    
    			Application.Run((System.Windows.Forms.Form) CreateInstance("Form2"));
    
    		}
    
    		private static System.Object CreateInstance(System.String TypeName)
    		{
    			System.Reflection.Assembly ThisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    
    			System.Type[] MyTypes = ThisAssembly.GetTypes();
    
    			for (int i=0; i<MyTypes.Length; i++)
    			{
    				if(String.Compare(MyTypes[i].Name, TypeName) == 0)
    					return System.Activator.CreateInstance(MyTypes[i]);
    			}
    			return null; // Failed
    		}
    My C# is poor but you should get the Idea....
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  7. #7

    Thread Starter
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: how can i instantiate a form through its name

    thanks there <ABX, i have now the idea on it through the code you'd given me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width