Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have form1 , i put in it textBox1 and form2 , i put in it button1
i want when i click on the button1 in form2 the textbox1 in form1 change its text
Posted

 
Share this answer
 
Comments
walterhevedeich 24-Aug-11 2:56am    
Beat me to it. Have a 5.
through properties..
create the instance for the form you want to use
 
Share this answer
 
v2
You can make use of public properties for this. Create a public string property for the textbox text. Get the current instance of form 1 in form two and change it.

In form1:

C#
Public String TextBoxText
{
get;
set;
}

In form 2:

C#
Form form = Application.OpenForms.OfType<Form>().Where(x => x is Form1).First();

if (form != null) {
    form.TextBoxText = "Value";
}
 
Share this answer
 
v2
Hey this is too much easy. I am giving you a complete solution.

Here i have used two forms. The code of form1 is given value,

C#
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
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}



The design file of the Form1 is ,

XML
namespace FormTest
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.TxtBoxFrm1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // TxtBoxFrm1
            //
            this.TxtBoxFrm1.Location = new System.Drawing.Point(69, 53);
            this.TxtBoxFrm1.Name = "TxtBoxFrm1";
            this.TxtBoxFrm1.Size = new System.Drawing.Size(100, 20);
            this.TxtBoxFrm1.TabIndex = 0;

            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.TxtBoxFrm1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        public System.Windows.Forms.TextBox TxtBoxFrm1;
    }
}



Please look on that we need to make the control of Form1 is public if we want to access it from other Form. In our Case this is

C#
public System.Windows.Forms.TextBox TxtBoxFrm1;


Now source code of Form2 is,

C#
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
    {
        private Form1 currentForm1=null;

        public Form2(Form1 submittedForm1)
        {
            InitializeComponent();

            currentForm1 = submittedForm1;

            currentForm1.Show();

        }

        private void BtnFrm2_Click(object sender, EventArgs e)
        {
            string testStr = TxtBoxFrm2.Text;

            currentForm1.TxtBoxFrm1.Text = testStr;
        }
    }
}




And Design Code is,

XML
namespace FormTest
{
    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.TxtBoxFrm2 = new System.Windows.Forms.TextBox();
            this.BtnFrm2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // TxtBoxFrm2
            //
            this.TxtBoxFrm2.Location = new System.Drawing.Point(52, 30);
            this.TxtBoxFrm2.Name = "TxtBoxFrm2";
            this.TxtBoxFrm2.Size = new System.Drawing.Size(179, 20);
            this.TxtBoxFrm2.TabIndex = 0;
            //
            // BtnFrm2
            //
            this.BtnFrm2.Location = new System.Drawing.Point(73, 137);
            this.BtnFrm2.Name = "BtnFrm2";
            this.BtnFrm2.Size = new System.Drawing.Size(131, 23);
            this.BtnFrm2.TabIndex = 1;
            this.BtnFrm2.Text = "Change Text";
            this.BtnFrm2.UseVisualStyleBackColor = true;
            this.BtnFrm2.Click += new System.EventHandler(this.BtnFrm2_Click);
            //
            // Form2
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.BtnFrm2);
            this.Controls.Add(this.TxtBoxFrm2);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox TxtBoxFrm2;
        private System.Windows.Forms.Button BtnFrm2;
    }
}


And main Program source code is,

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace FormTest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2(new Form1()));
        }
    }
}


That's all my source code. What you need to do is that

1. Create a WinForm Application
2. Add Form1. In its .cs file copy my code and Paste,in its Designer.cs file copy my design code for From1 and paste there.
3. Same thing do in Form2 and Main Program.

Run this. You will see there are two form running on the same time. Write something on the TextBox of Form2 and Press Button then you will see same text will appear in the textbox of From1
 
Share this answer
 
Comments
Md. Rashim Uddin 24-Aug-11 7:18am    
Please try on that. I have done it to help you. It is working man.

Thanks,
Rashim
Md. Rashim Uddin 25-Aug-11 5:09am    
Is it Solve your Problem.Please lemme know if not or if you need any assistance regarding this
Md. Rashim Uddin 8-Sep-11 2:12am    
Did u try with that??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900