Ok, I'm trying to pass some variables from inside Form1, to a new form: LoginDetails.
Essentially, what this code does is check if a file exists. If it does, it loads the values from within the file. If the file loads correctly, (it should contail login details to a server) but then the details are incorrect, the form LoginDetails is spawned and the (incorrect) values from the file are passed so the user can just edit the values rather than re-entering them all. Also, if the file doesn't exist, LoginDetails is spawned blank, so the user can enter the server's login details.

bool success = false;
            int i = 1;
                bool serverSet = false;
                //If the file containing the login details exists, load the details.
                //Also if this is the first time we've run the loop. After this if we kept running it would stick in an infinite loop.
                if (System.IO.File.Exists(serverImportLocation) && i == 1)
                    server = IO.Import(serverImportLocation);
                    serverSet = true;
                //Otherwise, we need to get the details from the user, and create the file to store the details
                    LoginDetails dialog = new LoginDetails();
                    //If we're past the first pass, show the currently loaded data
                    if (i > 1)
                        dialog.Username = server.UserName;
                        dialog.Password = server.Password;
                        dialog.Port = server.Port.ToString();
                        dialog.ServerIP = server.HostName;
                    //Show the dialog
                    DialogResult result = dialog.ShowDialog(this);

                    if (result == DialogResult.OK)
                        int port;
                        int.TryParse(dialog.Port, out port);
                        server = new Server(dialog.ServerIP, port, dialog.Username, dialog.Password);
                        serverSet = true;
                        IO.Export(serverImportLocation, server);
                        MessageBox.Show("Oops! Check what you've entered!");
                //By this point, a set of server details has been loaded
                //The connection to the server needs to be tested, and we will do this by logging on to the server, querying the server for our login details, and parsing the response.
                //As this program will only function correctly is the login is serveradmin, this will also check the login is serveradmin ~ This could be changed in a later version.

                if (serverSet)
                    request = BuildRequest.WhoAMI();
                    response = Telnet.SendRequest(server, request);
                    string user = "";
                    string error = ParseResponse.WhoAmI(response, ref user);

                    //NOTE: serveradmin doesn't neccessarily need to be the user. Not all features of the program work if you dont have full serveradmin priviledges, as such this checks you are BOTH logged in successfully, AND serveradmin

                    if (error == "Data Extracted Successfully!" && user == "serveradmin")
                        success = true;
            } while (!success);


public partial class LoginDetails : Form
        public string ServerIP;
        public string Port;
        public string Username;
        public string Password;

        public LoginDetails()
            //If this has been called due to a bad login - we'll have been sent values for the text boxes
            //If one is set, all should be. As such we'll only test for one of the values :)
            if (ServerIP != null)
                txtIP.Text = ServerIP;
                txtPassword.Text = Password;
                txtPort.Text = Port;
                txtUserName.Text = Username;

        private void btnAddServer_Click(object sender, EventArgs e)
            this.ServerIP = txtIP.Text.Trim();
            this.Port = txtPort.Text.Trim();
            this.Username = txtUserName.Text.Trim();
            this.Password = txtPassword.Text.Trim();
            this.DialogResult = DialogResult.OK;

        private void LoginDetails_Load(object sender, EventArgs e)


As it is, this solution isn't working. How do I go about passing the variables from Form1 to LoginDetails.

Thanks for any help in advance.

6 Years
Discussion Span
Last Post by Medalgod

When the LoginDetails constructor executes ServerIP etc have defualt valeus.
Put the code that copies the values to the textboxes in the form's shown event.
That way it happens when the dialog is displayed not created.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.