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:
hello,
I am creating a windows application in vs 2005 using c#, my query is that i am taking 2 forms form1 and form2,
i have a richtextbox in form1 and i have changed its modifier property to public also i have a button which will display form2 the code for that is
C#
private void button1_Click(object sender, EventArgs e)
        {
            Form2 obj = new Form2();
            obj.Show();
        }


now i have a textbox in form2 whos modifier i have changged to public and a button...

what i want is that user will enter some text on richtextbox say for eg:"abc" and will hit the button to display form2 on form2 textbox the user will again enter the same text abc now if abc is present in richtextbox of form1 a messagebox must be displayed saying equal and if not equal then error

The code which i am using is given below and i have wrote this code on form2 button

C#
private void button1_Click(object sender, EventArgs e)
        {
            Form1 obj = new Form1();
            bool b = textBox1.Text.Equals(obj.richTextBox1.Text);
            if ( b== true)
            {
                MessageBox.Show("Equal");
            }
            else
            {
                MessageBox.Show("Not equal");
            }
        }

Please help me out...
Thanks & Regards
Radix :-\
Posted

radix3 wrote:
private void button1_Click(object sender, EventArgs e) { Form2 obj = new Form2(); obj.Show(); }


Because obj is not a member variable, you have thrown away your reference to this form, meaning you cannot access it's member variables. You've also not set the parent of this form, which has implications for your UI, and how form2 can access form1.


radix3 wrote:
private void button1_Click(object sender, EventArgs e) { Form1 obj = new Form1(); bool b = textBox1.Text.Equals(obj.richTextBox1.Text);


You're obviously self taught. That's fine, so am I, but I learned by buying a lot of books and reading them. You obviously don't know the first thing about how object oriented code works, and you desperately need to learn.

obj in this code is a NEW form1, it has NOTHING to do with the Form1 instance that is already visible. In the first instance, as I say, you need to learn some programming basics. As to your specific issue, I'd set up a delegate between the two forms for communication. I'd also NEVER make a control on a form public, I may make a property that returns the text on the richtextbox ( but does not allow setting it unless necessary ). This is, again, something you will understand when you know some basics about OO.
 
Share this answer
 
hi radix3,
for me it is not a good practice to do it like what you wanted. but just to answer you question. please see below:

you need to declare 1 static variable from form1, in my case i called it as sForm1;

first form
C#
private void button1_Click(object sender, EventArgs e)
        {
            sForm1 = textBox1.Text;
            Form1 sm = new Form1();
            sm.Show();
        }




2nd form
C#
private void button1_Click(object sender, EventArgs e)
       {
           string sValueForm1 = textBox1.Text;
           if (frmMain.sForm1.Contains(sValueForm1))
           {
               MessageBox.Show("equal");
           }
           //or
           if (frmMain.sForm1 == textBox1.Text)
           {
               MessageBox.Show("equal");
           }
       }
 
Share this answer
 
v2

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