Set edit control text value from custom action wit

2019-08-05 05:22发布

问题:

How can I change text value of an "edit" <Control /> from a C# custom action?

I can populate a "combobox" control but I can't find a way to change an "edit" control value.

回答1:

If you are dealing with a file browser dialog or folder browser dialog (Will work for your case as well), publish the changed property after executing the custom action. Best if you reset before calling the custom action. See following example

  <Control Id="editLocation"  Type="Edit" X="45" Y="174" Height="18" Width="220" Property="YOUR_PROPERTY" Text="[YOUR_PROPERTY]"/>
  <Control Id="btnEditLocation" Type="PushButton" X="270" Y="175" Width="56" Height="17" Text="Browse" Property="YOUR_PROPERTY">
  <Publish Event="Reset" Value="1">1</Publish>             
  <Publish Event="DoAction" Value="YOUR_CUSTOM_ACTION"><![CDATA[1]]></Publish>
  <Publish Property="YOUR_PROPERTY" Value="[YOUR_PROPERTY]"><![CDATA[1]]></Publish>
  </Control>


回答2:

As well as linking the properties as suggested by Nilaksha Perera, my approach is to move the Reset action to the C# custom action. That way, we can choose to call it only when the custom action is about to complete successfully. This has the advantage of not clearing the value of the edit control unless a replacement value has been specified.

An example custom control:

public static ActionResult FileBrowser(Session session)
{
    try
    {
        // Call your file browser here.

        session[VALUE] = "New value";

        session.DoAction("Reset");
        return ActionResult.Success;
    }
    catch (Exception ex)
    {
        session.Log($"Unable to launch the file browser: {ex.Message}");
        return ActionResult.Failure;
    }
}


回答3:

The Control element has a Property attribute. This attribute holds the name of the property, which defines the value of the control, in your case, Edit control. When you change the value of this linked property, the control starts displaying this new value.

However, the dialog should "refresh" in order for the changes to become visible. As long as Windows Installer UI is quite limited and doesn't expose a big variety of events, you should work it around somehow.

For instance, let's say that you need to change the value in the edit control when the dialog it is placed on is just loaded. You can achieve this in the following way: on a previous dialog, assign a DoAction event on the Next button click, and run your custom action by this event. Hence, when the next dialog is loaded the edit control will display the desired value.

Side note: it might turn out that you don't need a C# custom action - the SetProperty might be enough. If that's the case, use it wherever possible.