tags:

views:

664

answers:

3

what i want to do is actually more complex than this. but the principal is this..

i want to insert another control like text box into an existing form, but after some event like a click of a button.

the new text box would be inserted into the same form( main form)

what i have got is. i created as usual windows form application, and then put a button there. then in the same project i add new form window. and put a text box there.

and in the event of button click, i put

form2.showdialog();

it works but it shows as a dialog box.

but what i want is that the text box shows up on the main form, not in the new form as a dialog box.

thank for the asap reply.

+1  A: 
private void button1_Click( object sender, EventArgs e )
{
    TextBoxt text = new TextBox( );
    // set location and other properties
    this.Controls.Add( text );
}
Ed Swangren
+3  A: 

If you need only a single text box sometimes visible, sometimes not, I suggest just to add it in the designer and toggle the TextBox.Visible property in the event.

If you need to dynamically add several controls, I suggest to use the TableLayoutPanel and add controls to it at runtime.

Finally you could just add the control to the main form with something like the following.

Control textBox = new TextBox();

// Set the location, size, and all the other properties.

this.Controls.Add(textBox);

This way you have the largest freedom to build your form, but accept for very simple cases it is non-trivial to get a reasonable layout.

Daniel Brückner
havent succeeded to apply this
r4ccoon
i am trying to do it on a separate file and class. i think i have to pass the main form variable to it.
r4ccoon
i have a class FormUI_VectorAddition, and i have method InitializeComponent(Control control,windowForm form)control is actually a panel.control.SuspendLayout();form.SuspendLayout();control.Controls.Add(this.txtBox_v1a);this.txtBox_v1a = new System.Windows.Forms.TextBox(); control.ResumeLayout(false); control.PerformLayout(); form.ResumeLayout(false);
r4ccoon
and i instantiate the object, and then, call the initalize().but still it doesnt work
r4ccoon
Is this your exact code? You are calling Controls.Add() before creating the text box with new TextBox(). So if this is your actual code, you are just adding null to the panel. Just swap the constructor call and the call to Controls.Add() to fix it.
Daniel Brückner
A: 

hei. i am able to solve it by using list (generic list)

in main form, create a private variabel List and create public method to get the variable.

in main form, create a public method to add by looping through the list.

so in the new class i have created, in one of the method i put the creation of the form. and into here i pass the listcontrol.

and then put all the control variable into the list control.

and on a click of a button, i call the class method, and then it will automatically draw the form controls that created by the class.

private List<Control> listControl;

        public windowForm()
        {
            InitializeComponent();
            listControl = new List<Control>(); 
        } 

        public List<Control> ListControl {
            get { return listControl; }
        }

        public void addControl() {
            if (this.listControl.Count() > 0) {
                foreach (Control c in listControl)
                {
                    Console.WriteLine("adding "+c.Name);
                    this.panel1.Controls.Add(c);
                }
            }
        }

        public void removeControl() {
            if (this.listControl.Count() > 0)
            {
                foreach (Control c in listControl)
                {
                    Console.WriteLine("removing " + c.Name);
                    this.panel1.Controls.Remove(c);
                }
            }
        }

and for the new class i have created, i put

this.groupbox_VectorAddition = new System.Windows.Forms.GroupBox();
            this.txtBox_v1a = new System.Windows.Forms.TextBox();
            this.txtBox_v1b = new System.Windows.Forms.TextBox();
            this.txtBox_v1c = new System.Windows.Forms.TextBox();
            this.txtBox_v2c = new System.Windows.Forms.TextBox();
            this.txtBox_v2b = new System.Windows.Forms.TextBox();
            this.txtBox_v2a = new System.Windows.Forms.TextBox();
            this.lbl_Vector1 = new System.Windows.Forms.Label();
            this.lbl_Vector2 = new System.Windows.Forms.Label();
            this.btn_countAddVector = new System.Windows.Forms.Button();
            this.btn_resetVector = new System.Windows.Forms.Button();
//put everything into the panel
            form.ListControl.Add(btn_resetVector);
            form.ListControl.Add(btn_countAddVector);
            form.ListControl.Add(lbl_Vector2);
            form.ListControl.Add(lbl_Vector1);
            form.ListControl.Add(txtBox_v2a);
            form.ListControl.Add(txtBox_v2b);
            form.ListControl.Add(txtBox_v2c);
            form.ListControl.Add(txtBox_v1c);
            form.ListControl.Add(txtBox_v1b);
            form.ListControl.Add(txtBox_v1a);

            form.ListControl.Add(groupbox_VectorAddition);
r4ccoon