Skip to content

Using objects with secondary forms... Whats wrong here...

tach

Asked Feb 29, 2008, 5:50 PM EST1 answer
Share
LinkedInX
Hello...


I am trying to create a pop up save box for a program of mine...

Basically, I am calling a new form from the main form I have so:


public partial class Form1 : Form
    {

       
        public Form1()
        {
            InitializeComponent();
        }


Is the standard form I'm using that visual studio automatically creates.  Now when you click a button(button1 in this case), I want to call a new form(form2), here is the code:

private void SaveVariable_Click(object sender, EventArgs e)
        {
          

            form2 saveform = new form2();
            saveform.Show();
            saveform.Visible = true;
           
           
        }
    }

// below is a new class


    public class form2 : Form
    {

       
       
       
            //constructor
            public form2()
            {
            //initialize components
               Label label2 = new System.Windows.Forms.Label();
                label2.AutoSize = true; // this.label2.AutoSize = true;
                label2.Location = new System.Drawing.Point(55, 55);
                label2.Name = "label2";
                label2.Size = new System.Drawing.Size(39, 13);
                label2.TabIndex = 10;
                label2.Text = "Label2";
                label2.Visible = true;
            }


    }

 


However, when the program runs, while the new form object is created, no label box appears anywhere. What am I missing? I've tried using   this.label2.X  as well for all of the form2 variables but it doesn't change anything....

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Sir,

    I have never created controls on the fly.

    so dont know much about that but if you see when you place

    controls on the form  a container is initialised in the YourForm.Desiner.cs

    and then there is InitializeComponents Which New`s That all you have placed on the

    form.

    so what you are missing can be seen on the designer class of your form by placing some controls on it

    Secondly if you are making a save box of which you know how and what should work on it

    then make a form allready and place all the controls on it that will save you lot of coding

    also.

    In my infant Way of programming which is my hobby. i take this approach

    Java
    create all the forms place controls on them
    now make a ControlCenter.cs class
    declare all the forms and classes in that like this
    //forms
    public static YourProgrammName.YourForm1  YourForm1_variable    ;
    public static YourProgrammName.YourForm2  YourForm2_variable ;
    public static YourProgrammName.YourForm3  YourForm3_variable ;
    //classes
    public static DealWithSomething   InSomething  = new DealWithSomething ()  ;
    public static DealWithSomething2   InSomething2  = new DealWithSomething2 ()  ;
    
    //In Programm File which every cs programm has Programm.cs
    //after these statements
    Application.EnableVisualStyles() ;
    Application.SetCompatibleTextRenderingDefault( false ) ;
    
    // initialize the form variables
    ControlCenter.YourForm1_variable = new YourForm1 ;
    ControlCenter.YourForm2_variable = new YourForm2 ;

    etc etc..

    C#
    // now the form that you want to be the main form
    do like this
    Application.Run( ControlCenter.YourForm1_variable ) ;
    
    //that`s it

    You can call these variables anywhere in your programm and they will

    show you all the controls in the forms you have put while making them

    and Rest all the programming is the same as always

    Lastly if you have to work with the controls of one form in another

    you have to make their scope/modifier public

     

    ThankYou

    RIGHT_THEN

    0