Le réseau pour les développeurs > Forums - Accueil > Visual C# General > Help I am trying to show a form with the name from inside a variable.
Poser une questionPoser une question
 

TraitéeHelp I am trying to show a form with the name from inside a variable.

  • mercredi 4 novembre 2009 19:51Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hello

    I have a program that creates a form using a name, input by the user. The form is created then the name is set to the users input.

    how can i show the form using the name from a variable. Any help would be great.

    Thank you in advance

    Dave

Réponses

  • mercredi 4 novembre 2009 23:57Rudedog2ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    My example showed how to use a string to select the startup form in that thread.
    You sound as if you used the code normally executed in the application's Main() method elsewheres.  Not good.

     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");  
                Application.Run(form);
     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");  
                Application.Run(form);
     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");  
                Application.Run(form);
     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");  
                Application.Run(form);
     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");   
                form.Show();


    That is what you most likely need in your application.
    I dunno.  Cannot tell for sure. 
    Post code of the method where/how you need to show the new form based on a string name.


    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."

Toutes les réponses

  • mercredi 4 novembre 2009 19:58BigTuna99 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Are you saying the user will be inputting the type of form to be created?  Or that you simply want to set the caption of the titlebar of the Form?

    The 1st requires reflection:

    string userInput = textbox1.Text
    Form frm = (Form)Activator.CreateInstance(Type.GetType(userInput));
    frm.Show();


    For the 2nd you just need to set the Text property of the Form:

    Form.Text = textbox1.Text

    If this answers your question, please mark the question as answered.
  • mercredi 4 novembre 2009 20:06Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hi

    I have the name of the form in a string. and i want to show that form.

     i tried your suggestion but it didn't work.
    gave me this error.

    ArgumentNullException
    Value cannot be null.
    Parameter name: type
  • mercredi 4 novembre 2009 20:12P.Brian.Mackey Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    A vague description of the issue and no source code is of little use.  Know your limits.


    Good coding involves knowing one's logical limits and expanding them as necessary.
  • mercredi 4 novembre 2009 20:19Balaji Baskar Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I didn't get your question completely. But here is what i do based on my understanding.

    A new form is created and the form's title is set based on some string value.

     

    Form frmUser = new Form();

    frmUser.Text =

    "Form opened by Balaji Baskar";

    frmUser.Show();


    Balaji Baskar [Please mark the post as answer if it answers your question]
  • mercredi 4 novembre 2009 20:20Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    OK sorry i will try to explain better.

    I have some code that creates a new form and names it based on the input from the user.

    eg. user inputs "hello" form is called "frmHello".

    This works fine the form is created.

    I then later need to show that form using the string that contains "frmHello"

    Hope this helps to clarify.
  • mercredi 4 novembre 2009 20:23BigTuna99 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    My suggestion requires the fully qualified type of the form.  Are you saying you have something like this?


    Form form1 = new Form();
    string name = "form1";

    and you then want to use the name variable to show the form?  The way I would do this would be to use a Hashtable or Dictionary.

    System.Collections.Hashtable h = new System.Collections.Hashtable();
    
    Form form1 = new Form();
    string name = "form1";
    
    h.Add(name, form1);
    
    //then to access the form using the name
    
    string userInput=  textbox1.Text;
    
    if(h[userInput]!=null)
    {
    	((Form)h[userInput).Show();
    }
    



    If this answers your question, please mark the question as answered.
  • mercredi 4 novembre 2009 20:36Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Thanks for the replies but i cant get it to work.

    its like this:

    Form form1 = new Form();
    string FormName = "Hello";

    form1.name = FormName;


    I tried the above suggestion but it never entered the if.

    i assume i was right to put the ] in where underlined.

    (Form)h[userInput ).Show();

  • mercredi 4 novembre 2009 20:47BigTuna99 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    It should work if you provide the FormName as the key to the Hashtable;


    Form form1 = new Form();
    string FormName = "Hello";

    form1.name = FormName;

    Hashtable h = new Hashtable();

    h.Add(FormName, form1);


    if(h[FormName]!=null)
    {
    ((Form)h[FormName]).Show();
    }



    If this answers your question, please mark the question as answered.
  • mercredi 4 novembre 2009 21:25Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    OK i got your example to work but when i applied it to my program it didn't work. My Fault for being unclear, it has been a long day and i apologize.


    In my program i have my input form, frmInput and another form frmOutput and another form frmMenu

    a user enters some details on the frmInput form and one of the variables strName is used to Name that users version of frmOutput.

    the values are sent to a class which has the code below.


    string strName = txt1.Text;                 //txt1.Text = "Hello"
    frmOutput frmNew = new frmOutput

    //note at this point i can show the form by using frmNew.Show() but i do not want to at this stage.

    frmNew.Name = "frm" + strName;

    then on the frmMenu form i want to show the users form using strName. so the form will be called frmHello

    Thanks for your help so far it has helped me gain a better understanding of C# as i am only a beginner.








  • mercredi 4 novembre 2009 21:26Balaji Baskar Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code

    I have the code this way, 2 buttons and 2 textboxes

    1 button click, to open new form with formname from textbox1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    	CreateNewForm(TextBox1.Text)
    End Sub
    
    Private Sub CreateNewForm(ByVal formName As String)
    	Dim frmUser As New Form()
    	frmUser.Text = formName
    	frmUser.Show()
    End Sub  
    
    

    1 button click, to find whether any form is open already (compared with form name)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    	For Each frm As Form In My.Application.OpenForms
    	    If (frm.Text = TextBox2.Text) Then
    		MessageBox.Show("Form Already Open.")
    	    End If
    	Next
    End Sub  
    
    

    here you would see that you actually check to see if any open form exist with the given form name.

    is this the one you wanted..? 


    Balaji Baskar [Please mark the post as answer if it answers your question]
  • mercredi 4 novembre 2009 21:37BigTuna99 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Could you post your code please, I don't understand why the example isn't helping you since the way you're describing your app is exactly like how I laid out the example.



    If this answers your question, please mark the question as answered.
  • mercredi 4 novembre 2009 21:39Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Sorry Balaji Baskar this isn't what i needed. its my fault for being unclear i tried to explain better above your post.


  • mercredi 4 novembre 2009 21:51ScottyDoesKnow Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    You want to refer to the form variable in code as frmHello? If this is the case, you can't, and there's absolutely no reason to as the variable name doesn't matter.
  • mercredi 4 novembre 2009 22:07Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Sorry for the long wait i had to retype the code, here it is

    GlobalClass.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;

    namespace formtest
    {
        static class GlobalClass
        {

            private static ArrayList arraylist1 = new ArrayList();
            
             public static ArrayList Global1
             {
                 get
                 {
                     return arraylist1;
                 }

                 set
                 {
                     arraylist1 = value;
                 }
             }

        }
    }

    ------------------------------------------------------------------------------------------------------

    Class1.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;

    namespace formtest
    {
           
        class Class1
        {
            private Form1 Form1;
            public ArrayList array1 = new ArrayList();

            public struct1 struct1Info;

            public struct struct1
            {
                public string strName;
                public string strNumber;
               
            }   

            public Class1(Form1 frm)
            {
                Form1 = frm;
            }


            public void AddForm(struct1 structNew1)
            {
                array1 = GlobalClass.Global1;
                
                struct1Info = structNew1;
                array1.Add(struct1Info);

                Form2 newform = new Form2();
                newform.Text = struct1Info.strNumber;

                //newform.Show();
                newform.Name = "frm" + struct1Info.strName;

               


                GlobalClass.Global1 = array1;

            }
        }
    }

    ---------------------------------------------------------------------------------
    Form1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace formtest
    {
        public partial class Form1 : Form
        {
            private Class1 Class1;
           
            private Class1.struct1 structNew1;

            public Form1()
            {
                InitializeComponent();
            }
                  

            private void Form1_Load(object sender, EventArgs e)
            {
                Class1 = new Class1(this);
            }

            private void btn1_Click(object sender, EventArgs e)
            {
                structNew1.strName = txt1.Text;
                structNew1.strNumber = txt2.Text;

                Class1.AddForm(structNew1);

                Form FormMenu = new frmMenu();
                FormMenu.Show();
                this.Hide();
            }
        }
    }
      

    -------------------------------------------------------------------------------------------------------------
    frmMenu.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;

    namespace formtest
    {
        public partial class frmMenu : Form
        {
            public frmMenu()
            {
                InitializeComponent();
            }

            private void btn1_Click(object sender, EventArgs e)
            {
                string strForm = "frm";
                ArrayList arraylist1 = new ArrayList();
                arraylist1 = GlobalClass.Global1;

                foreach (Class1.struct1 i in arraylist1)
                {
                    strForm = strForm + i.strName;

                    //this is where i want to show the users version of form2 using the value in strForm
                }
            }

            private void frmMenu_Load(object sender, EventArgs e)
            {

            }
        }
    }

    ----------------------------------------------------------------------------------------------

    Form2.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace formtest
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }

            private void btn1_Click(object sender, EventArgs e)
            {
                txt1.Text = this.Name;
            }
        }
    }



    --------------------------------------------------------------

    and thats it.

    so Form1 hast 2 text box txt1 and txt2 and a button btn1.

    frmMenu has one button btn1

    Form2 has a textbox txt1 and a button btn1

    A user enters details in Form1  they get sent to Class1 and put in an array because i want multiple users to input.



  • mercredi 4 novembre 2009 23:03Rudedog2ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Check this thread.  You want to use the FormFactory class.

    Rudy   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
  • mercredi 4 novembre 2009 23:29Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I Tried your solution and got this error

    SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.

    i am unsure exactly where to put this part

     Application.EnableVisualStyles();  
                Application.SetCompatibleTextRenderingDefault(false );  
     
                FormFactory factory = new  FormFactory();  
                Form form = factory.CreateInstance("MDIParent1" );  
                Application.Run(form);


    do i put it where my form is created or where i want to show my form.




    Ok a big thanks to all, i have managed to dodge around the problem (kinda i will see what happens) but would if possible still like a solution. I will at some stage post a better version of the problem with clearer code.






  • mercredi 4 novembre 2009 23:57Rudedog2ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    My example showed how to use a string to select the startup form in that thread.
    You sound as if you used the code normally executed in the application's Main() method elsewheres.  Not good.

     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");  
                Application.Run(form);
     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");  
                Application.Run(form);
     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");  
                Application.Run(form);
     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");  
                Application.Run(form);
     
                FormFactory factory = new FormFactory();  
                Form form = factory.CreateInstance("MDIParent1");   
                form.Show();


    That is what you most likely need in your application.
    I dunno.  Cannot tell for sure. 
    Post code of the method where/how you need to show the new form based on a string name.


    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
  • jeudi 5 novembre 2009 00:16Dave789 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I have kinda solved the issue by only creating the form when i need it, that way i can show it straight away, instead of trying to give it a name and then show it using the string variable with the forms name in.

    So its solved for now, i may have problems later, but by then i will have learned a bit more c# and will have sorted out my code (at the moment i am writing somethings i don't understand even though they seem to work). So if i need to post it it will tidy and easy to read. (I hope)