I just started coding in c# recently (I made some programs in c# a few years ago, but that was more scraping). I need to pass a few strings from one form to another.
My program does the following:
- first it opens a login screen. 4 Values are entered here and passed to another form (if it can connect),
- the second form is the main program, but it needs the values from the login screen to.
so here is my code:
program.cs:
login.cs:
main.cs:
This works perfectly, but i was just wondering if I overlooked something and if there is a "better"way to do this.
thanks in advance
My program does the following:
- first it opens a login screen. 4 Values are entered here and passed to another form (if it can connect),
- the second form is the main program, but it needs the values from the login screen to.
so here is my code:
program.cs:
Code:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Login login = new Login();
if (login.ShowDialog() == DialogResult.OK)
{
string hostname = login.hostname;
string port = login.port;
string user = login.user;
string password = login.password;
Application.Run(new Main(hostname,port,user,password));
}
else
{
Application.Exit();
}
}
login.cs:
Code:
public partial class Login : Form
{
public string hostname {get; private set;}
public string port { get; private set; }
public string user { get; private set; }
public string password { get; private set; }
public Login()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
l_version.Text = "Version: XXXX";
TextBox.hostname.Text = Properties.Settings.Default.hostname;
TextBox.port.Text = Properties.Settings.Default.port;
TextBox.user.Text = Properties.Settings.Default.user;
encryption decr = new encryption();
TextBox.password.Text = decr.decrypt(Properties.Settings.Default.password);
}
private void b_connecTextBox.Click(object sender, EventArgs e)
{
try
{
hostname = TextBox.hostname.Text;
port = TextBox.port.Text;
user = TextBox.user.Text;
password = TextBox.password.Text;
if (CheckBox.remember.Checked)
{
Properties.Settings.Default.hostname = hostname;
Properties.Settings.Default.port = port;
Properties.Settings.Default.user = user;
encryption encr = new encryption();
Properties.Settings.Default.password = encr.encrypt(password);
Properties.Settings.Default.Save();
}
this.DialogResult = DialogResult.OK;
this.Close();
}
catch
{
MessageBox.Show("Error: Could not connect.");
}
}
main.cs:
Code:
partial class Main : Form
{
string hostname;
string port;
string user;
string password;
public Main(string Hostname,string Port, string User, string Password)
{
InitializeComponent();
hostname = Hostname;
port = Port;
user = User;
password = Password;
}
private void Main_Load(object sender, EventArgs e)
{
}
}
This works perfectly, but i was just wondering if I overlooked something and if there is a "better"way to do this.
thanks in advance