Harish Kumar
Harish Kumar

Reputation: 2105

Making a dialog box stay in view

I am using windows forms application using C#. I have created a dialog box that requests the user for login details such as a username and password. using If-else statement, If login details match, a message box stating "success" appears and the dialog box closes automatically. this is fine. but, if I enter wrong login details, I make a message box appear in the else statement, stating that "wrong details entered". But, then when I close that message box, the dialog box disappears. I do not want this to happen. I want the dialog box to remain in view for the user to enter correct details again. is there some lines of code that I should enter to avoid closing of the dialog box?

Upvotes: 1

Views: 493

Answers (2)

user978122
user978122

Reputation: 5761

Check the FormClosing event, and check the event. Write some code to check the event and to stop the form from closing if it's coming from anything other than a button contained within the form.

Upvotes: 0

Grrbrr404
Grrbrr404

Reputation: 1805

I would suggest you to create a new form and then show it as dialog:

1) Create new form
2) Set MinimizeButton of Form to false
3) Set MaximizeButton of Form to false
4) Add components: 1x textfield, 2x button (ok button and cancel button)
5) Set DialogResult of Ok Button to: OK
6) Set DialogResult of Cancel Button to: Cancel
7) Add a public method: public string GetText() { return textbox.text };
8) Set password char for textbox

In your main form:

var dialogForm = new MyNewForm();
if (dialogForm.ShowDialog() != DialogResult.OK)
{
   Application.Exit()
}
else 
{
   var pw = dialogForm.GetText(); // string var pw stores the enterd pw now
   // validate your password here

   if (PasswordIsCorrect())
   {
      // some logic here
   } 
}

If you want to display the Dialog in a loop, replace "if (dialogForm.ShowDialog() ... " with "while ..."

Attention: Code untestet
Attention 2: Please make sure that your validation logic is safe. Experienced users can decompile your application to get the validation logic

Hope that helps? Kind regards

Upvotes: 2

Related Questions