Insert checkbox in properties panel as user input for custom activities

Hi!

If anyone was curious how to use a checkbox on the properties panel for a custom activity, apply the following code (replace “Close_Application” for your use case):

    // Check to close word (all documents will be closed)
    // Uncheck to close only the document in question and word app remains open
    [Category("Options")]
    [DefaultValue(false)]
    public bool Close_Application
    {
        get;
        set;
    }

And then simply use your checkbox boolean like so:
if (Close_Application)
{
// Do your stuff
}

Note that InArgument will render a textbox, not a checkbox!

image

3 Likes

Could you give me a complete sample code?
I want to learn how to binding this bool value in the designer.xaml,thanks!

See below. It either closes an active word document, or it closes the word application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Activities;
using System.ComponentModel;
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.IO;

namespace CustomWordActivities
{
public class CloseActiveDocument : CodeActivity
{
// Check to close word (all documents will be closed)
// Uncheck to close only the document in question and word app remains open
[Category(“Options”)]
[DefaultValue(false)]
public bool CloseApp
{
get;
set;
}

    protected override void Execute(CodeActivityContext context)
    {
        try
        {
            object wordAsObject;
            Word.Application word;
            Word.Document openDocument;

            wordAsObject = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

            word = (Word.Application)wordAsObject;

            openDocument = word.ActiveDocument;

            if (CloseApp)
            {
                word.Quit(false);
            }
            else
            {
                openDocument.Close();
            }

        }
        catch (COMException)
        {
            throw;
        }
    }
}

}

2 Likes

Thanks for your reply, but I mean I want to learn how to do this bool value in the designer. .
I have a problem with this picture. I have added resources (</sap:ActivityDesigner.Resources>) and I can’t achieve it. . .

I got this from an older post, but this might be what you’re looking for: https://blogs.msdn.microsoft.com/tilovell/2011/04/06/wf4-showing-an-inargumentbool-as-a-checkbox-in-the-workflow-designer-property-grid/

Now I use vs.community.2017, something seems different from this article, let’s study it.

thanks @MattTheList

1 Like

Did you make a working example of this? :slight_smile:

yes…
see my custom activity
i add HasHeader property

Thanks

2 Likes