mbugr
mbugr

Reputation: 352

How to control form load from another form

I want to control my form load event from another form. my problem I create some winform control in form1 runtime, but the creation will controlled by form2.

I will read some data from user in form2 and when user enter specific text I will create winform control in form1.

I make some code to do that using from1 to create winform control in runtime.

private TextBox txtBox = new TextBox();
        private Button btnAdd = new Button();
        private ListBox lstBox = new ListBox();
        private CheckBox chkBox = new CheckBox();
        private Label lblCount = new Label();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.BackColor = Color.White;
            this.ForeColor = Color.Black;
            this.Size = new System.Drawing.Size(550, 550);
            this.Text = "Test Create form in runtime ";
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterScreen;

            this.btnAdd.BackColor = Color.Gray;
            this.btnAdd.Text = "Add";
            this.btnAdd.Location = new System.Drawing.Point(90, 25);
            this.btnAdd.Size = new System.Drawing.Size(50, 25);
            this.txtBox.Text = "Text";
            this.txtBox.Location = new System.Drawing.Point(10, 25);
            this.txtBox.Size = new System.Drawing.Size(70, 20);

            this.lstBox.Items.Add("One");

            this.lstBox.Sorted = true;
            this.lstBox.Location = new System.Drawing.Point(10, 55);
            this.lstBox.Size = new System.Drawing.Size(130, 95);
            this.chkBox.Text = "Disable";
            this.chkBox.Location = new System.Drawing.Point(15, 190);
            this.chkBox.Size = new System.Drawing.Size(110, 30);
            this.lblCount.Text = lstBox.Items.Count.ToString() + " items";
            this.lblCount.Location = new System.Drawing.Point(55, 160);
            this.lblCount.Size = new System.Drawing.Size(65, 15);

            this.Controls.Add(btnAdd);
            this.Controls.Add(txtBox);
            this.Controls.Add(lstBox);
            this.Controls.Add(chkBox);
            this.Controls.Add(lblCount);

        }

How make the same thing from form2 ?

Upvotes: 1

Views: 1994

Answers (1)

Lali
Lali

Reputation: 2866

I don't know which kind of 'Control' you need. However in multiple forms environment, communication between forms is trivial. There are many ways to do communicate, like one can be as

Create public properties of type Form in your Parent form,

public Form propForm1 {get;set;}

When on menu item click you open form1, store it's object to that public property.

var form1 = New yourchildformname();
form1.MdiParent = this;
propForm1 = form1; // Add this line.
form1.Show();

Now every time when you will click an other button to open the form2, you will have propForm1 object, which you can use to set some data on that form.

EDIT: On form2, you can access controls of form1 as

private void button1_Click(object sender, EventArgs e)
{
    this.parent.propForm1.txtUserName = "Yokohama";
}

Remember the above code is on form2. Also set 'access modifier' property of txtUserName from private to public.

Upvotes: 2

Related Questions