3

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.

Katie Kilian
  • 6,815
  • 5
  • 41
  • 64
workat
  • 63
  • 2
  • 10

3 Answers3

6

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>
Nilaksha Perera
  • 715
  • 2
  • 12
  • 36
  • 2
    +1 No idea why this was downvoted. The key here is the Reset event, or edit controls that have been typed in won't update. Irrelevant whether it's using a file browser though :) – Gordon Leigh Oct 26 '17 at 09:27
  • Unfortunately this will not work if you have other properties in text boxes on your dialog as the reset event will reset all the other properties too. – Ryan Thomas Jan 29 '20 at 11:15
  • Get rid of the reset. The important line is the last Publish entry. As OP wrote, he updates property in custom action but it doesn't show in UI. If whatever the property name is you make the `<![CDATA[1]]>` entry after the custom action, it will somehow trigger the UI to update..at least did for me. Of course change `YOUR_PROPERTY` to the real name of your property in case that's not clear to anyone. – Joseph Willcoxson Oct 26 '21 at 21:30
1

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;
    }
}
Gary Pendlebury
  • 336
  • 5
  • 8
0

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.

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139