Through the Rompecabeza Gauntlet
Sometimes you want certain data from a user of your app before the app starts and may want to disallow the app from running if they don't provide what you want. For instance, you may want to record them pronouncing the word "Shibboleth" and compare their attempt at proper elocution of that word with what you consider to be the correct way; if they fail, don't allow them to run the app. Or...you might have other tests you want them to pass, or simply want to gather some info before proceeding and, if they are not willing to provide it, to perdition with them! Well, at least, don't let them run the app...
Anyway, in a Windows form app, you can do this pretty easily. Here's one way:
0) Add the values you want to prompt the user for in a global location, such as a Consts class:
public static class DbillPlatypusConsts
{
public static string userName;
public static string pwd;
public static string platypupRompecabeza;
. . .
1) Create a "login" (or "prompt" or "test" or whatever you want to call it) form. Add labels and textboxes to prompt the user for these values and give them a way to provide them; also, add an "OK" button and a "Cancel" button. That file can be something like this:
public partial class frmLogin : Form
{
public string UserName { get { return textBoxUsername.Text.Trim(); } }
public string Password { get { return textBoxPwd.Text.Trim(); } }
public string PlatypupRompecabeza { get { return textBoxPlatypupRompecabeza.Text.Trim(); } }
public frmLogin()
{
InitializeComponent();
}
private void buttonClose_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
private void buttonOK_Click(object sender, EventArgs e)
{
if (SanityCheck())
{
this.DialogResult = DialogResult.OK;
}
}
private bool SanityCheck()
{
bool pass = ((!(String.IsNullOrEmpty(textBoxUsername.Text.Trim()))) &&
(!(String.IsNullOrEmpty(textBoxPwd.Text.Trim()))) &&
(!(String.IsNullOrEmpty(textBoxPlatypupRompecabeza.Text.Trim()))));
if (!pass)
{
MessageBox.Show("You have not yet provided some key data; be sure to enter a username, password, and a platypup rompecabeza!");
}
return pass;
}
2) Add code to Program.cs to invoke that form and conditionally run the app/main form:
static class Program
{
[MTAThread]
static void Main()
{
frmLogin loginForm;
using (loginForm = new frmLogin())
{
if (loginForm.ShowDialog() == DialogResult.OK)
{
DbillPlatypusConsts.userName = loginForm.UserName;
DbillPlatypusConsts.pwd = loginForm.Password;
DbillPlatypusConsts.platypupRompecabeza = loginForm.PlatypupRompecabeza;
if ((DbillPlatypusConsts.userName == "Ephraim") &&
(DbillPlatypusConsts.pwd == "goSeahwaks_brattyIsAPunkAndBellychunksIsAWeasel") &&
(DbillPlatypusConsts.platypupRompecabeza == "defl8G8"))
{
Application.Run(new frmMain());
}
}
}
}
Pre-emptive Thumb-nosing
No doubt some fancy-pants, propellor-head supergeeks will poke holes in this methodology, but it works for me/meets me semi-tough standards.