Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have problem with using a method of a form in another form

i coded like this:
(i entered 123 in textbox1)
C#
public class Form1 : Form
    {
        private string user_code;

      public string UserCode
        {
            get { return user_code; }
        }

        public bool LoginUser()
        {
       user_code = null;
 
        
        if(textBox1.Text==123){
              user_code="usercode";
                 }
             * 
             */
        }
    }

and usage in form2:

C#
Form1 form1 = new Form1();
            form1.LoginUser();

            MessageBox.Show(form1.UserName);


now output is an empty string,i checked with breakpoint and i saw that when i call loginuser in form2 the value in textbox1 become empty and if condition become false
what do you think about problem??

TIA
Posted
Updated 16-Nov-11 22:05pm
v3
Comments
LanFanNinja 17-Nov-11 4:40am    
Check my solution below.

Check out my solution here(solution 2) Need data back in form1 Textbox1 while coming back from form2[^] for an easy way of doing this.
 
Share this answer
 
I think you need to go back to basics! :laugh:
You do not want to do this:
Form1 form1 = new Form1();
As the new keyword suggests, it creates a new instance of Form1 rather than accessing the existing, displayed version which has the filled-in textBox. You need to access the existing version which is curently displayed, ort you will not be able to get at it's textbox content!

It's a bit like you putting your phone into the glove box of your car, then being surprised when you look in the glove box of my car to find it isn't there!
 
Share this answer
 
Comments
arashmobileboy 17-Nov-11 4:07am    
thanks,so how should i access to current value of textbox?
OriginalGriff 17-Nov-11 4:28am    
Via the instance of Form1 that you typed into!
I can't tell you exactly what to do - I don't have your code. But, at some point you opened Form2 (presumably from Form1)...

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