Click here to Skip to main content
16,011,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looking to transfer 2 variable`s values between 2 forms, I cannot get it right.
any help is appreciated ;)
I am getting no errors by the way, I am just looking for a way of transferring password.text and username.text to form2, any advice?

What I have tried:

public Form3(string password,string username)
        {
            InitializeComponent();
            password = this.password.Text;
            username = textBox1.Text;

        }
        public Form3() 
        { InitializeComponent();
            
        }



        private void button1_Click(object sender, EventArgs e)
        {
            string password = password.Text;
            string username = textBox1.Text;
            string repassword = passwordre.Text;
            if (password == repassword)
            {
                MessageBox.Show("Successfully Registered");

                Form1 startupwin = new Form1();
                Form3 newwindow = new Form3();
                this.Close();
                startupwin.Show();
            }
            else
            {
                MessageBox.Show("Passwords Do Not Match OR Not All Required Fields Filled");
                this.Close();
                Form3 newwindow3 = new Form3();
                newwindow3.Show();
            }
Posted
Updated 4-Oct-19 21:36pm

Why are you trying to pass username and password back to form2? Ideally, the validation should be made in form3, which sould not allow to close if password and passwordre do not match, and should return either DialogResult.OK if the username and password match together (sucessful login), or DialogResult.Cancel if they do not (wrong username, or wrong password).
Then form2 can use the DialogResult of form3 instance to know whether or not login was successful.
Here's a brief "architecture" to put you on a path:
C#
public class Form2 : Form
{
   private void LoginButton_Click(object sender, EventArgs e)
   {
      using (Form3 form = new Form3())
      {
         if (form.ShowDialog(this) == DialogResult.OK)
         {
            // Sucessful login

            // You can get the username back here. I don't think you need the
            // password, though. Passwords are only useful in Form3's context.
            string username = form.Username;
         }
         else
         {
            // Login failed
         }
      }
   }
}

public class Form3 : Form
{
   // This property will allow you to get the username, eventually.
   public string Username => UsernameTextBox.Text;

   // This event handler should be attached to the Validated event
   // of PasswordTextBox and PasswordRepeatTextBox. It disables the OK
   // button if both entries do not match. There is no point in enabling
   // to login if these values do not match in the first place.
   private void TextBox_Validated(object sender, EventArgs e)
   {
      OkButton.Enabled =
         PasswordTextBox.Text == PasswordRepeatTextBox.Text;
   }

   // This event handler must be attached to the Click event of the OK button.
   // It sets the form's DialogResult according to the validity of the
   // username/passsword pair.
   private void OkButton_Click(object sender, EventArgs e)
   {
      this.DialogResult =
         (IsValidLogin(UsernameTextBox.Text, PasswordTextBox.Text))
            ? DialogResult.OK
            : DialogResult.Cancel;
      this.Close();     
   }

   private bool IsValidLogin(string username, string password)
   {
      // TODO : you have to implement an actual username/password validation
      // strategy.
   }
}

I also have the feeling that you should begin with something simpler than a login system, since you do not seem to have understood the basics of OOP already. For example, you cannot create a new instance of form2 in form3 and expect to get a reference to the already existing form2 instance. When you create a new instance, it is distinct from all other instances of the same class that may have been created so far.
And, as a general advise, start with a pen and paper, and draw a quick diagram of the important steps which have to be followed; this will allow you to write more clear and effective code later on.
 
Share this answer
 
Comments
yoavarviv 5-Oct-19 3:40am    
thank you, will do something simpler ;)
You've got to start thinking about this stuff for yourself: this is your third question with the same code ... or "slightly changed" versions of the same code.

This isn't difficult. It's even pretty basic stuff.
Have a look here: Transferring information between two forms, Part 1: Parent to Child[^]

And then think about exactly what your code is doing ... because it's pretty evident that you have not sat down and decided what you need to do before diving into code, and that would really, really, help you. I'd start by working out the "data flow" that the user needs to see, and go from there, starting from a clean sheet of paper.

And change the names of your forms to reflect what they do, just as you did with your textboxes - it makes your code a lot more readable, and that means it's more likely to work.
 
Share this answer
 
Comments
yoavarviv 5-Oct-19 2:33am    
thanks for your help, I am just practicing, I am studying coding independently for now and I wasn't understanding some of the errors, I have ended up fixing it myself, srry if I made you angry in any way
OriginalGriff 5-Oct-19 3:25am    
"I am studying coding independently for now"
That's probably most of your problem.
When you "study independently" it's a problem because you don't get context for what you have to do - so you don't understand why it's necessary - and then when a problem happens, you don't know where to start to fix it. And ... you never know if there was a simpler way to do something that could have made your life a whole load easier. Just trying to pick it up as you go along doesn't work: you don't know if the code you found was any good, and you don't even know about the existence of easier ways!
I would very strongly suggest you take a course (best option - if you don't understand what is being taught, you can talk to a real human tutor who can rephrase until you do), or get a book (Wrox, Addison Wesley, and Microsoft Press do some very good ones, and your local library may have access to them, or there is the Yellow Book:
https://www.robmiles.com/c-yellow-book/
which is free and starts from the beginning (but doesn't go as far into the Framework as the others)
Whichever, start at the beginning and follow it through doing all the exercises.

You need to get the basics of OOPs under your belt before you even start coding Windows apps!
yoavarviv 5-Oct-19 3:39am    
Thanks for your help, I will definetly take a tutor. ;)
yoavarviv 5-Oct-19 2:45am    
can you maybe write me the code I need to add so I can understand it better, I don't know why but it seems like the code Im doing isn't working

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