How can i make the function keep going












0















I building a program and have an execute button. I want to make that if it is the first time you use the program you need to login. if not then you dont need to because the login credentials are alredy saved. Here is my code.. Right now it dont keep going with the rest of the execute function..



The loginSaver is an get and set method wich sets the logincontinue to true in loginform.



IB-Form:



internal void ExecBtn_Click(object sender, EventArgs e)
{
if (Username != "" || Password != "")
{
Username = Properties.Settings.Default.UserName.ToString();
Password = Properties.Settings.Default.UserPass.ToString();
}
else if (loginForm.LoginSaver == true)
{
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
}
else if (Username == "" || Password == "")
{
loginForm.Show();
return;
}



if (ServerComboBox.SelectedIndex == -1)
{
ServerComboBox.BackColor = Color.LightYellow;
MessageBox.Show("Du måste välja en kund!");

return;
}


LoginForm:



private void LoginBtn_Click(object sender, EventArgs e)
{
IB_Ärende IBForm = new IB_Ärende();
if (UserNameTxt.Text == "" || PasswordTxt.Text == "")
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
}

if (CheckBoxSave.Checked == true)
{
IsServerConnected();
if (testingConnection == true)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
//loginContinue = true;
this.Hide();
UserNameTxt.Text = "";
PasswordTxt.Text = "";

//IBForm.ExecBtn_Click(sender, e);
}
else if (testingConnection == false)
{
MessageBox.Show("Fel användarnamn eller lösenord");
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
return;
}

}
else if (CheckBoxSave.Checked == false)
{
IsServerConnected();
if (testingConnection == true)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

loginchecker = true;
loginContinue = true;
this.Hide();
//IBForm.ExecBtn_Click(sender, e);
UserNameTxt.Text = "";
PasswordTxt.Text = "";
}
else if (testingConnection == false)
{
MessageBox.Show("Fel användarnamn eller lösenord");
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
return;

}

}

else
{
MessageBox.Show("Du måste logga in för att kunna fortsätta!");
}

}


Thanks for helping...










share|improve this question























  • save the username and password externally (File, DataBase) and then the program starts you can check if the username and password already exits there

    – styx
    Jan 2 at 8:18











  • Look up the difference between Show() and ShowDialog()

    – bommelding
    Jan 2 at 8:26











  • The problem is that im using this against a database but i cant use it there.. i need to use this way.. Can Show() and showdialog() be the problem?

    – Carlo Goretti
    Jan 2 at 8:41
















0















I building a program and have an execute button. I want to make that if it is the first time you use the program you need to login. if not then you dont need to because the login credentials are alredy saved. Here is my code.. Right now it dont keep going with the rest of the execute function..



The loginSaver is an get and set method wich sets the logincontinue to true in loginform.



IB-Form:



internal void ExecBtn_Click(object sender, EventArgs e)
{
if (Username != "" || Password != "")
{
Username = Properties.Settings.Default.UserName.ToString();
Password = Properties.Settings.Default.UserPass.ToString();
}
else if (loginForm.LoginSaver == true)
{
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
}
else if (Username == "" || Password == "")
{
loginForm.Show();
return;
}



if (ServerComboBox.SelectedIndex == -1)
{
ServerComboBox.BackColor = Color.LightYellow;
MessageBox.Show("Du måste välja en kund!");

return;
}


LoginForm:



private void LoginBtn_Click(object sender, EventArgs e)
{
IB_Ärende IBForm = new IB_Ärende();
if (UserNameTxt.Text == "" || PasswordTxt.Text == "")
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
}

if (CheckBoxSave.Checked == true)
{
IsServerConnected();
if (testingConnection == true)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
//loginContinue = true;
this.Hide();
UserNameTxt.Text = "";
PasswordTxt.Text = "";

//IBForm.ExecBtn_Click(sender, e);
}
else if (testingConnection == false)
{
MessageBox.Show("Fel användarnamn eller lösenord");
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
return;
}

}
else if (CheckBoxSave.Checked == false)
{
IsServerConnected();
if (testingConnection == true)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

loginchecker = true;
loginContinue = true;
this.Hide();
//IBForm.ExecBtn_Click(sender, e);
UserNameTxt.Text = "";
PasswordTxt.Text = "";
}
else if (testingConnection == false)
{
MessageBox.Show("Fel användarnamn eller lösenord");
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
return;

}

}

else
{
MessageBox.Show("Du måste logga in för att kunna fortsätta!");
}

}


Thanks for helping...










share|improve this question























  • save the username and password externally (File, DataBase) and then the program starts you can check if the username and password already exits there

    – styx
    Jan 2 at 8:18











  • Look up the difference between Show() and ShowDialog()

    – bommelding
    Jan 2 at 8:26











  • The problem is that im using this against a database but i cant use it there.. i need to use this way.. Can Show() and showdialog() be the problem?

    – Carlo Goretti
    Jan 2 at 8:41














0












0








0








I building a program and have an execute button. I want to make that if it is the first time you use the program you need to login. if not then you dont need to because the login credentials are alredy saved. Here is my code.. Right now it dont keep going with the rest of the execute function..



The loginSaver is an get and set method wich sets the logincontinue to true in loginform.



IB-Form:



internal void ExecBtn_Click(object sender, EventArgs e)
{
if (Username != "" || Password != "")
{
Username = Properties.Settings.Default.UserName.ToString();
Password = Properties.Settings.Default.UserPass.ToString();
}
else if (loginForm.LoginSaver == true)
{
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
}
else if (Username == "" || Password == "")
{
loginForm.Show();
return;
}



if (ServerComboBox.SelectedIndex == -1)
{
ServerComboBox.BackColor = Color.LightYellow;
MessageBox.Show("Du måste välja en kund!");

return;
}


LoginForm:



private void LoginBtn_Click(object sender, EventArgs e)
{
IB_Ärende IBForm = new IB_Ärende();
if (UserNameTxt.Text == "" || PasswordTxt.Text == "")
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
}

if (CheckBoxSave.Checked == true)
{
IsServerConnected();
if (testingConnection == true)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
//loginContinue = true;
this.Hide();
UserNameTxt.Text = "";
PasswordTxt.Text = "";

//IBForm.ExecBtn_Click(sender, e);
}
else if (testingConnection == false)
{
MessageBox.Show("Fel användarnamn eller lösenord");
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
return;
}

}
else if (CheckBoxSave.Checked == false)
{
IsServerConnected();
if (testingConnection == true)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

loginchecker = true;
loginContinue = true;
this.Hide();
//IBForm.ExecBtn_Click(sender, e);
UserNameTxt.Text = "";
PasswordTxt.Text = "";
}
else if (testingConnection == false)
{
MessageBox.Show("Fel användarnamn eller lösenord");
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
return;

}

}

else
{
MessageBox.Show("Du måste logga in för att kunna fortsätta!");
}

}


Thanks for helping...










share|improve this question














I building a program and have an execute button. I want to make that if it is the first time you use the program you need to login. if not then you dont need to because the login credentials are alredy saved. Here is my code.. Right now it dont keep going with the rest of the execute function..



The loginSaver is an get and set method wich sets the logincontinue to true in loginform.



IB-Form:



internal void ExecBtn_Click(object sender, EventArgs e)
{
if (Username != "" || Password != "")
{
Username = Properties.Settings.Default.UserName.ToString();
Password = Properties.Settings.Default.UserPass.ToString();
}
else if (loginForm.LoginSaver == true)
{
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
}
else if (Username == "" || Password == "")
{
loginForm.Show();
return;
}



if (ServerComboBox.SelectedIndex == -1)
{
ServerComboBox.BackColor = Color.LightYellow;
MessageBox.Show("Du måste välja en kund!");

return;
}


LoginForm:



private void LoginBtn_Click(object sender, EventArgs e)
{
IB_Ärende IBForm = new IB_Ärende();
if (UserNameTxt.Text == "" || PasswordTxt.Text == "")
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
}

if (CheckBoxSave.Checked == true)
{
IsServerConnected();
if (testingConnection == true)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
//loginContinue = true;
this.Hide();
UserNameTxt.Text = "";
PasswordTxt.Text = "";

//IBForm.ExecBtn_Click(sender, e);
}
else if (testingConnection == false)
{
MessageBox.Show("Fel användarnamn eller lösenord");
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
return;
}

}
else if (CheckBoxSave.Checked == false)
{
IsServerConnected();
if (testingConnection == true)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

loginchecker = true;
loginContinue = true;
this.Hide();
//IBForm.ExecBtn_Click(sender, e);
UserNameTxt.Text = "";
PasswordTxt.Text = "";
}
else if (testingConnection == false)
{
MessageBox.Show("Fel användarnamn eller lösenord");
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
return;

}

}

else
{
MessageBox.Show("Du måste logga in för att kunna fortsätta!");
}

}


Thanks for helping...







c# winforms login






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 8:14









Carlo GorettiCarlo Goretti

107




107













  • save the username and password externally (File, DataBase) and then the program starts you can check if the username and password already exits there

    – styx
    Jan 2 at 8:18











  • Look up the difference between Show() and ShowDialog()

    – bommelding
    Jan 2 at 8:26











  • The problem is that im using this against a database but i cant use it there.. i need to use this way.. Can Show() and showdialog() be the problem?

    – Carlo Goretti
    Jan 2 at 8:41



















  • save the username and password externally (File, DataBase) and then the program starts you can check if the username and password already exits there

    – styx
    Jan 2 at 8:18











  • Look up the difference between Show() and ShowDialog()

    – bommelding
    Jan 2 at 8:26











  • The problem is that im using this against a database but i cant use it there.. i need to use this way.. Can Show() and showdialog() be the problem?

    – Carlo Goretti
    Jan 2 at 8:41

















save the username and password externally (File, DataBase) and then the program starts you can check if the username and password already exits there

– styx
Jan 2 at 8:18





save the username and password externally (File, DataBase) and then the program starts you can check if the username and password already exits there

– styx
Jan 2 at 8:18













Look up the difference between Show() and ShowDialog()

– bommelding
Jan 2 at 8:26





Look up the difference between Show() and ShowDialog()

– bommelding
Jan 2 at 8:26













The problem is that im using this against a database but i cant use it there.. i need to use this way.. Can Show() and showdialog() be the problem?

– Carlo Goretti
Jan 2 at 8:41





The problem is that im using this against a database but i cant use it there.. i need to use this way.. Can Show() and showdialog() be the problem?

– Carlo Goretti
Jan 2 at 8:41












1 Answer
1






active

oldest

votes


















0














1.If you want to block current function and wait for LoginForm to close use ShowDialog() instead of Show(). This way after closing login form you have access to Login/Password:



loginForm.ShowDialog();
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
if(string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
{
return;
}


2.I suggest using string.IsNullOrEmpty() for checking if login/password is provided.



3.Instead of if (CheckBoxSave.Checked == true) you can write if (CheckBoxSave.Checked)



4.If you have condition that evaluates to True/False e.g. if (CheckBoxSave.Checked == true) in else do not use another if like else if (CheckBoxSave.Checked == false) If something is not true then it has to be false use



if (CheckBoxSave.Checked) 
{
...
}
else
{
...
// checkbox is not checked
}


similar is for if (testingConnection == true)



You could also refactor your LoginBtn_Click(object sender, EventArgs e) method a bit:



private void LoginBtn_Click(object sender, EventArgs e)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
return;
}
IsServerConnected();
if (testingConnection)
{
if(CheckBoxSave.Checked)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
}
DialogResult = DialogResult.OK;
}
else
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Fel användarnamn eller lösenord");
}
}


This way loginForm.ShowDialog() whill be DailogResult.OK only if login succeeded and userName/password will always be set.






share|improve this answer


























  • But i only want to open the loginform if its not any login credentials saved. i also want to be able to not save the credentials and still make the function keep going..

    – Carlo Goretti
    Jan 2 at 9:06











  • So call this only if you do not have login credentials saved. I would do it like: internal void ExecBtn_Click(object sender, EventArgs e) { If(!IsLoggedIn())//This should check saved credentials { using(LoginForm loginForm = new LoginForm()){ loginForm.ShowDialog(); } if(!sLoggedIn()) return;

    – Pablo notPicasso
    Jan 2 at 9:13













  • Now the next problem is that it dont update the settings so it dont find the credentials. but if i close the program and then open it again then it can find my credentials..

    – Carlo Goretti
    Jan 2 at 9:44











  • I have tried the Settings.Default.Reload(); but it dont work anyway.. any idea @Pablo notPicasso?

    – Carlo Goretti
    Jan 2 at 10:06











  • The question is how and when do you save your credentials. From your code it seems that you save them only if CheckBoxSave is checked and testingConnection is true. You can see edit to my post and refactoring LoginBtn_Click method.

    – Pablo notPicasso
    Jan 2 at 10:14













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003148%2fhow-can-i-make-the-function-keep-going%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














1.If you want to block current function and wait for LoginForm to close use ShowDialog() instead of Show(). This way after closing login form you have access to Login/Password:



loginForm.ShowDialog();
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
if(string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
{
return;
}


2.I suggest using string.IsNullOrEmpty() for checking if login/password is provided.



3.Instead of if (CheckBoxSave.Checked == true) you can write if (CheckBoxSave.Checked)



4.If you have condition that evaluates to True/False e.g. if (CheckBoxSave.Checked == true) in else do not use another if like else if (CheckBoxSave.Checked == false) If something is not true then it has to be false use



if (CheckBoxSave.Checked) 
{
...
}
else
{
...
// checkbox is not checked
}


similar is for if (testingConnection == true)



You could also refactor your LoginBtn_Click(object sender, EventArgs e) method a bit:



private void LoginBtn_Click(object sender, EventArgs e)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
return;
}
IsServerConnected();
if (testingConnection)
{
if(CheckBoxSave.Checked)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
}
DialogResult = DialogResult.OK;
}
else
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Fel användarnamn eller lösenord");
}
}


This way loginForm.ShowDialog() whill be DailogResult.OK only if login succeeded and userName/password will always be set.






share|improve this answer


























  • But i only want to open the loginform if its not any login credentials saved. i also want to be able to not save the credentials and still make the function keep going..

    – Carlo Goretti
    Jan 2 at 9:06











  • So call this only if you do not have login credentials saved. I would do it like: internal void ExecBtn_Click(object sender, EventArgs e) { If(!IsLoggedIn())//This should check saved credentials { using(LoginForm loginForm = new LoginForm()){ loginForm.ShowDialog(); } if(!sLoggedIn()) return;

    – Pablo notPicasso
    Jan 2 at 9:13













  • Now the next problem is that it dont update the settings so it dont find the credentials. but if i close the program and then open it again then it can find my credentials..

    – Carlo Goretti
    Jan 2 at 9:44











  • I have tried the Settings.Default.Reload(); but it dont work anyway.. any idea @Pablo notPicasso?

    – Carlo Goretti
    Jan 2 at 10:06











  • The question is how and when do you save your credentials. From your code it seems that you save them only if CheckBoxSave is checked and testingConnection is true. You can see edit to my post and refactoring LoginBtn_Click method.

    – Pablo notPicasso
    Jan 2 at 10:14


















0














1.If you want to block current function and wait for LoginForm to close use ShowDialog() instead of Show(). This way after closing login form you have access to Login/Password:



loginForm.ShowDialog();
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
if(string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
{
return;
}


2.I suggest using string.IsNullOrEmpty() for checking if login/password is provided.



3.Instead of if (CheckBoxSave.Checked == true) you can write if (CheckBoxSave.Checked)



4.If you have condition that evaluates to True/False e.g. if (CheckBoxSave.Checked == true) in else do not use another if like else if (CheckBoxSave.Checked == false) If something is not true then it has to be false use



if (CheckBoxSave.Checked) 
{
...
}
else
{
...
// checkbox is not checked
}


similar is for if (testingConnection == true)



You could also refactor your LoginBtn_Click(object sender, EventArgs e) method a bit:



private void LoginBtn_Click(object sender, EventArgs e)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
return;
}
IsServerConnected();
if (testingConnection)
{
if(CheckBoxSave.Checked)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
}
DialogResult = DialogResult.OK;
}
else
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Fel användarnamn eller lösenord");
}
}


This way loginForm.ShowDialog() whill be DailogResult.OK only if login succeeded and userName/password will always be set.






share|improve this answer


























  • But i only want to open the loginform if its not any login credentials saved. i also want to be able to not save the credentials and still make the function keep going..

    – Carlo Goretti
    Jan 2 at 9:06











  • So call this only if you do not have login credentials saved. I would do it like: internal void ExecBtn_Click(object sender, EventArgs e) { If(!IsLoggedIn())//This should check saved credentials { using(LoginForm loginForm = new LoginForm()){ loginForm.ShowDialog(); } if(!sLoggedIn()) return;

    – Pablo notPicasso
    Jan 2 at 9:13













  • Now the next problem is that it dont update the settings so it dont find the credentials. but if i close the program and then open it again then it can find my credentials..

    – Carlo Goretti
    Jan 2 at 9:44











  • I have tried the Settings.Default.Reload(); but it dont work anyway.. any idea @Pablo notPicasso?

    – Carlo Goretti
    Jan 2 at 10:06











  • The question is how and when do you save your credentials. From your code it seems that you save them only if CheckBoxSave is checked and testingConnection is true. You can see edit to my post and refactoring LoginBtn_Click method.

    – Pablo notPicasso
    Jan 2 at 10:14
















0












0








0







1.If you want to block current function and wait for LoginForm to close use ShowDialog() instead of Show(). This way after closing login form you have access to Login/Password:



loginForm.ShowDialog();
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
if(string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
{
return;
}


2.I suggest using string.IsNullOrEmpty() for checking if login/password is provided.



3.Instead of if (CheckBoxSave.Checked == true) you can write if (CheckBoxSave.Checked)



4.If you have condition that evaluates to True/False e.g. if (CheckBoxSave.Checked == true) in else do not use another if like else if (CheckBoxSave.Checked == false) If something is not true then it has to be false use



if (CheckBoxSave.Checked) 
{
...
}
else
{
...
// checkbox is not checked
}


similar is for if (testingConnection == true)



You could also refactor your LoginBtn_Click(object sender, EventArgs e) method a bit:



private void LoginBtn_Click(object sender, EventArgs e)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
return;
}
IsServerConnected();
if (testingConnection)
{
if(CheckBoxSave.Checked)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
}
DialogResult = DialogResult.OK;
}
else
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Fel användarnamn eller lösenord");
}
}


This way loginForm.ShowDialog() whill be DailogResult.OK only if login succeeded and userName/password will always be set.






share|improve this answer















1.If you want to block current function and wait for LoginForm to close use ShowDialog() instead of Show(). This way after closing login form you have access to Login/Password:



loginForm.ShowDialog();
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
if(string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
{
return;
}


2.I suggest using string.IsNullOrEmpty() for checking if login/password is provided.



3.Instead of if (CheckBoxSave.Checked == true) you can write if (CheckBoxSave.Checked)



4.If you have condition that evaluates to True/False e.g. if (CheckBoxSave.Checked == true) in else do not use another if like else if (CheckBoxSave.Checked == false) If something is not true then it has to be false use



if (CheckBoxSave.Checked) 
{
...
}
else
{
...
// checkbox is not checked
}


similar is for if (testingConnection == true)



You could also refactor your LoginBtn_Click(object sender, EventArgs e) method a bit:



private void LoginBtn_Click(object sender, EventArgs e)
{
userName = UserNameTxt.Text;
passWord = PasswordTxt.Text;

if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
return;
}
IsServerConnected();
if (testingConnection)
{
if(CheckBoxSave.Checked)
{
Properties.Settings.Default.UserName = UserNameTxt.Text;
Properties.Settings.Default.UserPass = PasswordTxt.Text;
Properties.Settings.Default.Save();
}
DialogResult = DialogResult.OK;
}
else
{
UserNameTxt.BackColor = Color.LightYellow;
PasswordTxt.BackColor = Color.LightYellow;
UserNameTxt.ForeColor = Color.Red;
PasswordTxt.ForeColor = Color.Red;
MessageBox.Show("Fel användarnamn eller lösenord");
}
}


This way loginForm.ShowDialog() whill be DailogResult.OK only if login succeeded and userName/password will always be set.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 10:12

























answered Jan 2 at 8:55









Pablo notPicassoPablo notPicasso

2,30331320




2,30331320













  • But i only want to open the loginform if its not any login credentials saved. i also want to be able to not save the credentials and still make the function keep going..

    – Carlo Goretti
    Jan 2 at 9:06











  • So call this only if you do not have login credentials saved. I would do it like: internal void ExecBtn_Click(object sender, EventArgs e) { If(!IsLoggedIn())//This should check saved credentials { using(LoginForm loginForm = new LoginForm()){ loginForm.ShowDialog(); } if(!sLoggedIn()) return;

    – Pablo notPicasso
    Jan 2 at 9:13













  • Now the next problem is that it dont update the settings so it dont find the credentials. but if i close the program and then open it again then it can find my credentials..

    – Carlo Goretti
    Jan 2 at 9:44











  • I have tried the Settings.Default.Reload(); but it dont work anyway.. any idea @Pablo notPicasso?

    – Carlo Goretti
    Jan 2 at 10:06











  • The question is how and when do you save your credentials. From your code it seems that you save them only if CheckBoxSave is checked and testingConnection is true. You can see edit to my post and refactoring LoginBtn_Click method.

    – Pablo notPicasso
    Jan 2 at 10:14





















  • But i only want to open the loginform if its not any login credentials saved. i also want to be able to not save the credentials and still make the function keep going..

    – Carlo Goretti
    Jan 2 at 9:06











  • So call this only if you do not have login credentials saved. I would do it like: internal void ExecBtn_Click(object sender, EventArgs e) { If(!IsLoggedIn())//This should check saved credentials { using(LoginForm loginForm = new LoginForm()){ loginForm.ShowDialog(); } if(!sLoggedIn()) return;

    – Pablo notPicasso
    Jan 2 at 9:13













  • Now the next problem is that it dont update the settings so it dont find the credentials. but if i close the program and then open it again then it can find my credentials..

    – Carlo Goretti
    Jan 2 at 9:44











  • I have tried the Settings.Default.Reload(); but it dont work anyway.. any idea @Pablo notPicasso?

    – Carlo Goretti
    Jan 2 at 10:06











  • The question is how and when do you save your credentials. From your code it seems that you save them only if CheckBoxSave is checked and testingConnection is true. You can see edit to my post and refactoring LoginBtn_Click method.

    – Pablo notPicasso
    Jan 2 at 10:14



















But i only want to open the loginform if its not any login credentials saved. i also want to be able to not save the credentials and still make the function keep going..

– Carlo Goretti
Jan 2 at 9:06





But i only want to open the loginform if its not any login credentials saved. i also want to be able to not save the credentials and still make the function keep going..

– Carlo Goretti
Jan 2 at 9:06













So call this only if you do not have login credentials saved. I would do it like: internal void ExecBtn_Click(object sender, EventArgs e) { If(!IsLoggedIn())//This should check saved credentials { using(LoginForm loginForm = new LoginForm()){ loginForm.ShowDialog(); } if(!sLoggedIn()) return;

– Pablo notPicasso
Jan 2 at 9:13







So call this only if you do not have login credentials saved. I would do it like: internal void ExecBtn_Click(object sender, EventArgs e) { If(!IsLoggedIn())//This should check saved credentials { using(LoginForm loginForm = new LoginForm()){ loginForm.ShowDialog(); } if(!sLoggedIn()) return;

– Pablo notPicasso
Jan 2 at 9:13















Now the next problem is that it dont update the settings so it dont find the credentials. but if i close the program and then open it again then it can find my credentials..

– Carlo Goretti
Jan 2 at 9:44





Now the next problem is that it dont update the settings so it dont find the credentials. but if i close the program and then open it again then it can find my credentials..

– Carlo Goretti
Jan 2 at 9:44













I have tried the Settings.Default.Reload(); but it dont work anyway.. any idea @Pablo notPicasso?

– Carlo Goretti
Jan 2 at 10:06





I have tried the Settings.Default.Reload(); but it dont work anyway.. any idea @Pablo notPicasso?

– Carlo Goretti
Jan 2 at 10:06













The question is how and when do you save your credentials. From your code it seems that you save them only if CheckBoxSave is checked and testingConnection is true. You can see edit to my post and refactoring LoginBtn_Click method.

– Pablo notPicasso
Jan 2 at 10:14







The question is how and when do you save your credentials. From your code it seems that you save them only if CheckBoxSave is checked and testingConnection is true. You can see edit to my post and refactoring LoginBtn_Click method.

– Pablo notPicasso
Jan 2 at 10:14






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003148%2fhow-can-i-make-the-function-keep-going%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith