Home > Mobile >  Using Command Links in Winforms .NET Core
Using Command Links in Winforms .NET Core

Time:12-29

I would like to add command links into my C# Windows Forms app for Windows 10 21H2, but I haven't found any tutorials online.

I believe that this is a photo of a command link, but I'm not too sure. Can someone tell me how to add these to my app?

command link

I couldn't find any controls like this in the VS toolbox.

CodePudding user response:

Try this Custom Control, derived from Button, where the BS_COMMANDLINK Style is added in the CreateParams property override.
This turns the standard Button Control in a CommandLink Button.
The Text Property value of the Control is shown to the right of the arrow, while the custom LinkNote Property provides the description that is shown right below the text, with a smaller Font.
When you set the LinkNote value, the BCM_SETNOTE message is sent to the native Button. This sets the associated Note, at design-time or run-time.

The custom ControlDesigner is used to remove properties that are not needed / used when this style is set, or properties, as FlatStyle (which needs to be set to FlatStyle.System), that cannot be changed.
Unfortunately, this Designer works only in .NET Framework, to make it work in .NET 5 , you need to install the Microsoft.WinForms.Designer.SDK package. Open up the Package Manager Console and paste:

NuGet\Install-Package Microsoft.WinForms.Designer.SDK -Version 1.1.0-prerelease-preview3.22076.5

This package is hard to find otherwise.
If you don't care about the Designer's features, just remove it

using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
#if NET5_0_OR_GREATER
    using Microsoft.DotNet.DesignTools.Designers;
#else
    using System.Windows.Forms.Design;
#endif

[ToolboxItem(true)]
[Designer(typeof(CommandLinkDesigner))]
public class CommandLink : Button {
    private const int BCM_SETNOTE = 0x1609;
    private string m_LinkNote = string.Empty;

    public CommandLink() {
        SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UseTextForAccessibility, true);
        FlatStyle = FlatStyle.System;
    }

    [DefaultValue(FlatStyle.System)]
    public new FlatStyle FlatStyle {
        get { return base.FlatStyle; }
        set { base.FlatStyle = FlatStyle.System; }
    }

    public string LinkNote {
        get => m_LinkNote;
        set {
            if (value != m_LinkNote) {
                m_LinkNote = value;
                SendMessage(this.Handle, BCM_SETNOTE, 0, m_LinkNote);
            }
        }
    }

    protected override CreateParams CreateParams {
        get {
            var cp = base.CreateParams;
            cp.Style |= (0 | 0x0E); // Set BS_COMMANDLINK 
            return cp;
        }
    }

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern bool SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);

    public class CommandLinkDesigner : ControlDesigner {
        private readonly string[] RemovedProperties = new[] {
            "AllowDrop", "AutoEllipsis", "AutoSize", "AutoSizeMode",
            "BackColor", "BackgroundImage", "BackgroundImageLayout",
            "Cursor", "FlatAppearance", "FlatStyle",
            "ForeColor", "Image", "ImageAlign", "ImageImdex", "ImageKey",
            "ImageList", "TextAlign", "TextImageRelation"
        };

        public CommandLinkDesigner() { }

        protected override void PreFilterProperties(IDictionary properties)
        {
            foreach (string prop in RemovedProperties) {
                properties.Remove(prop);
            }
            base.PreFilterProperties(properties);
        }
    }
}
  • Related