-1

i am using c# winform app i use ssh.net for connecting to a headless raspberry pi. i can connect to raspberry pi using a single form and send commands but i want to make a separate login to device form and if connection was successful then proceed to the next form.

i don't know how to transfer the SSH connection to the second form.

i have tried passing some int and string using a class like this :

public static string s { get; set; }

but not a connection. any idea ?

Mohsen K
  • 257
  • 4
  • 10
  • If it's a setting, you can add a `.settings` file and store the value in a setting property. If it's a value provided by user in the first form and you want to pass it to the second form, you can add a parameter to the second form's constructor and when creating an instance of the second form, pass it to the constructor. To see more options about interaction between forms, take a look at [this post](https://stackoverflow.com/q/38768737/3110834) and also its the linked posts. – Reza Aghaei Jan 28 '18 at 16:14

1 Answers1

0

I think easy way is to do it like this: In your main form create a public connection variable:

public ConnectionInfo _connectionInfo

Then create new login form by calling it constructor: var myLoginForm = new LoginForm(); // I assume your login form is called LoginForm.

In login form create also public variable

public ConnectionInfo _connectionInfo;

Then pass main form _connectionInfo into login form:

myLoginForm._connectionInfo = this._connectionInfo;

And then show the login form:

myLoginForm.Show();

In login form you need to create a new connection into _connectionInfo variable when user press "Login button":

_connectionInfo = new ConnectionInfo("sftp.foo.com",
                                        "guest",
                                        new PasswordAuthenticationMethod("guest", "pwd"),
                                        new PrivateKeyAuthenticationMethod("rsa.key"));

Now close login form after login button is pressed by calling forms close method:

this.Close();

Now in your main form you can use _connectionInfo variable to do all the crazy things with ssh connection.

Hope this will help you. Happy coding!

Panu Oksala
  • 3,245
  • 1
  • 18
  • 28
  • If you are bit more advanced in coding, you can use properties and constructor parameters. For simplicity I just used local variable in this code example. – Panu Oksala Jan 28 '18 at 18:16