Preventing a form to show when registry keys are missing

G

Gustaf

My Windows app is dependant on some registry keys settings. In case the
subkey for the app doesn't exists, I want to show a messagebox, and then
quit the whole program (without showing the main form). How do you
accomplish this?

The program starts with:

[STAThread]
static void Main()
{
Application.Run(new frmMain());
}

In the frmMain() constructor I call a GetRegistryValues() method that
populates a set of class variables:

public frmMain()
{
// Get settings from Windows registry
bool b = GetRegistryValues();
if (b == false)
return;

// Draw the form
InitializeComponent();

...

GetRegistryValues() returns false if there was a problem. In that case,
I don't want to show frmMain at all. But when I run the code above, I
get an empty form of standard size, even if I return to Main() when
GetRegistryValues() has failed.

Gustaf
 
A

Andy

Create a static class in another file, and move the static void Main to
that class.

Then, code your Main like so:

static void Main() {
if ( your_keys_exist) ) {
Application.Run( new frmMain() );
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Gustaf,

I really would recommend against placing the code to check the registry
in the constructor. I would keep that outside of the constructor and then
not call the static Run method on the Application class if your keys don't
exist.

Hope this helps.
 
G

Gustaf

Andy said:
Create a static class in another file, and move the static void Main to
that class.

Then, code your Main like so:

static void Main() {
if ( your_keys_exist) ) {
Application.Run( new frmMain() );
}
}

Thank you.

I made a starter class I call Start.cs now, with static void Main(). I
also moved GetRegistryValues() to this class, because it's needed before
frmMain is loaded. But GetRegistryValues() used to fill the class
variables in frmMain, so I have to move these values to frmMain after
it's loaded. What's the cleanest way to do that? Shall I send the
variables as parameters to the frmMain() constructor? Or shall I just
*check* for the keys and strings in Start.cs and then *read* them in
frmMain()?

Gustaf
 
T

Tom Spink

Gustaf said:
My Windows app is dependant on some registry keys settings. In case the
subkey for the app doesn't exists, I want to show a messagebox, and then
quit the whole program (without showing the main form). How do you
accomplish this?
<snipeddy-doo-dah>

Hi Gustaf,

Just FYI, the reason the form is blank when the keys don't exist is because
you are exiting the constructor before you've called InitializeComponent(),
I don't know whether or not you realised this.

To solve your problem, I think you should do a bit of code-shifting.

In your entry point, instantiate your form, then call the
GetRegistryValues() method, and finally start the message pump if the
result was true. You may need to alter the scope of GetRegistryValues() to
internal (or public, if you want).

///
[STAThread]
static void Main()
{
frmMain mf = new frmMain();

if ( !mf.GetRegistryValues() )
{
MessageBox.Show( "Error" );
return;
}

Application.Run( mf );
}
///

And then, remember to make sure GetRegistryValues() is defined as internal
or public in your frmMain class.

///
internal bool GetRegistryValues ( ) { ... }
///
 
G

Gustaf

Tom said:
frmMain mf = new frmMain();

if ( !mf.GetRegistryValues() )
{
MessageBox.Show( "Error" );
return;
}

Application.Run( mf );
}

Smart. That's how it's gonna be. Thanks for helping.

Gustaf
 
A

Andy

I would have the form read them, since it is the object that needs
them. If the keys aren't present, is it possible for you to assign a
default value (and then stick that in the registry). Also, I would
handle this on the Form.Load event, not in the constructor.

HTH
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top