How can I validate custom dialog input in a .NET I

2019-08-12 17:57发布

问题:

I have a VS 2010 Setup project. In the setup project I have a custom dialog, and a custom action. Both work, I can even debug my custom action and it receives correctly the input the user provides in the custom dialog.

I would like to validate user input and only allow user go to next step if the input is valid. I can show a messagebox with MessageBox.Show, but how can I prevent to go to the next step until the user corrects the input?

public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);
    string myInput = Context.Parameters["MY_INPUT"]; // Value from custom dialog
    if (myInput ..... )
    {
        // Not a valid input, we do not want to proceed to the next step 
        MessageBox.Show("Not a valid input, please correct it");

        // What to do here? 
        // How can I tell the Installer do not accept this input?
    }
    else
    {
        // Valid input...
    }
 }

Thx for answers

回答1:

The simplest method of achieving this is set a property from your custom action and use that property to condition the NewDialog event that is called when the user press the "Next" button, so the event does not get executed if the condition is not true.

A .NET Installer class custom action cannot set the property directly from its code, but you could use your C# code to write another type of custom action that can get/set properties, as in this example: http://www.advancedinstaller.com/user-guide/qa-c-sharp-ca.html

EDIT: You can do that by editing the MSI generated from VS with Orca, however, this is quite painful I would say as you need to edit multiple tables manually, like Binary, CustomActions, ControlEvent. Here is something to get you started with that:http://support.microsoft.com/kb/255905
Another method would be to switch creating the setup package with a more advanced setup authoring tool. If you want to go with a free and powerful one I recommend WiX (http://wix.sourceforge.net/), it will take you some time to get started with it but its way faster than editing in Orca. The commercial alternative, which will allow you to edit your project even faster and much easier is Advanced Installer (http://www.advancedinstaller.com), but you will need an Enterprise license for what you require to accomplish.