Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have three forms,Form1 Contains one textbox and button.When i enters some integer in form1 and clicks the OK button,Form2 should gets open.Similarlly Form2 contains TextBox and Button,in the same way wen i enters some integer in form2 and clicks OK button,Form3 should gets open. and Form3 should displays the summation of two integer values(Integers entered in Form1 and Form2)......

Moreover i should not pass the values between the forms,which means i have to reduce the declaration variables..... i tried to access the textbox values in Form3. but i cant..... so plz suggest me how to do it?
Thanks in advance.........
Posted

Create a public property and access your form's text box value via that property.
 
Share this answer
 
Comments
Ramya 2010 26-Jul-10 2:54am    
@Abihnav-
i declared the property in both the forms..... and in form3 load event i put a following code.
private void Form3_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.one + Form2.two;

}
while executing it shows the following error........

Error 1 An object reference is required for the non-static field, method, or property 'test1.Form1.one.get'
Error 2 An object reference is required for the non-static field, method, or property 'test1.Form2.two.get'
You need to create a static public property, then you can access it by its formname, otherwise you have to create a object of each form for accessing non-static public property.
 
Share this answer
 
In this case, i prefer singleton class, store entered data into singleton class object and ask form 3 to access it and display it!
 
Share this answer
 
in form 1 use below

C#
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Form2 f2 = new Form2();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            f2.fo = textBox1.Text;
            f2.Show();
        }
    }
}




and in form 2




C#
namespace WindowsFormsApplication4
{
    public partial class Form2 : Form
    {
        public string fo
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }

        }
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {


        }
 
Share this answer
 
Try this

--- in form1's button click event
me.txtbox.text = form2.txtbox.text

--- in form2's button click event
me.txtbox.text = form3.txtbox.text

You may need not write "Me.", also try without using "Me.".
 
Share this answer
 
i worked on it just now and i got it... hope this helps YOU...

in Form1 Button click event

str = textbox1.Text;//str is the static variable of Form1



and same in Form2 Button click event
str = textbox2.Text;//str is the static variable of Form2



and Now in Form3 Buttong click event
textbox1.Text=Form1.str + Form2.str


Hey Please dont mind if i have misunderstood your question and sorry if i have given very silly reply
 
Share this answer
 
If you are just wanting parent to child communication then you can use the constructor to pass values. Do NOT use this method to pass from child to parent or sibling to sibling (use events instead).

For value types this works well, for reference types you have to be careful as a change to the object in the child will affect the parent(s).
C#
// Form1.cs

using System;
using System.Windows.Forms;

namespace CPWinForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int value;
            if (int.TryParse(textBox1.Text, out value))
            {
                Form2 form2 = new Form2(value);
                form2.Show();
            }
        }
    }
}

C#
// Form2.cs

using System;
using System.Windows.Forms;

namespace CPWinForms
{
    public partial class Form2 : Form
    {
        private int value1;

        public Form2()
            : this(0)
        { }
        public Form2(int value1)
        {
            InitializeComponent();
            this.value1 = value1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int value;
            if (int.TryParse(textBox1.Text, out value))
            {
                Form3 form3 = new Form3(value, value1);
                form3.Show();
            }
        }
    }
}

C#
// Form3.cs

using System.Windows.Forms;

namespace CPWinForms
{
    public partial class Form3 : Form
    {
        public Form3()
            : this(0, 0)
        { }
        public Form3(int value1, int value2)
        {
            InitializeComponent();
            label1.Text = (value1 + value2).ToString();
        }
    }
}
 
Share this answer
 
Comments
koool.kabeer 27-Jul-10 5:48am    
dear DaveyM69,
He doesnt want to pass the value between forms
If you want to pass data between forms, always use a delegate. And, buy a basic C# book to help you understand your errors in the code you posted, it's clear you've never taken a course or read a book on OO.
 
Share this answer
 
Comments
Francis Donald Dowuona 8-Jun-15 6:45am    
How to clear Textbox of one form by clicking a button on another form in C#?

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