Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical space in command links in TaskDialog since v1.1

I have noticed a large, vertical space in my task dialog (the space between the command links' titles and instruction texts) which looks really bad. It started to appear right after I upgraded WindowsAPICodePack to version 1.1.

Here's the code:

TaskDialog td = new TaskDialog();
var b1 = new TaskDialogCommandLink("b1", "foo", "bar");
var b2 = new TaskDialogCommandLink("b2", "one", "two");
td.Controls.Add(b1);
td.Controls.Add(b2);
td.Caption = "Caption";
td.InstructionText = "InstructionText";
td.Text = "Text";
td.Show();

Here's the result:

Ugly task dialog with vertical space in command links

Before, "bar" would appear right below "foo", but now it looks as if there's an empty line between the two. Is this a problem on my end (and would anyone know what it might be) or are you guys also experiencing this?

like image 251
Christoffer Reijer Avatar asked Jul 18 '13 06:07

Christoffer Reijer


2 Answers

I've gotten the same bug in the 1.1 release. It seems to be due to the TaskDialogCommandLink class's toString method string.Format with Environment.NewLine, which doesn't map cleanly when passed to the TaskDialog itself.

public override string ToString()
{
  return string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}",
    Text ?? string.Empty,
    (!string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(instruction)) ?
        Environment.NewLine : string.Empty,
    instruction ?? string.Empty);
}

I use an implementation subclass anyway to make the arguments easier, and over-rode the method to pass a string containing a simple '\n', although I don't need to internationalize my application and so can do things a little more simply.

public override string ToString()
{
    string str;

    bool noLabel = string.IsNullOrEmpty(this.Text);
    bool noInstruction = string.IsNullOrEmpty(this.Instruction);

    if (noLabel & noInstruction)
    {
        str = string.Empty;
    }
    else if (!noLabel & noInstruction)
    {
        str = this.Text;
    }
    else if (noLabel & !noInstruction)
    {
        str = base.Instruction;
    }
    else
    {
        str = this.Text + "\n" + this.Instruction;
    }
    return str;
}
like image 166
DougM Avatar answered Nov 14 '22 21:11

DougM


I noticed the same spacing issues with the API Code Pack v1.1 on Windows 8. DougM is correct, his ToString() override will solve the problem.

Here is an updated version, simply drop this class into your project and instead of using TaskDialogCommandLink, use TaskDialogCommandLinkEx instead.

using Microsoft.WindowsAPICodePack.Dialogs;

internal class TaskDialogCommandLinkEx : TaskDialogCommandLink
{
    public override string ToString()
    {
        string str;

        var noLabel = string.IsNullOrEmpty(Text);
        var noInstruction = string.IsNullOrEmpty(Instruction);

        if (noLabel & noInstruction)
        {
            str = string.Empty;
        }
        else if (!noLabel & noInstruction)
        {
            str = Text;
        }
        else if (noLabel & !noInstruction)
        {
            str = Instruction;
        }
        else
        {
            str = Text + "\n" + Instruction;
        }
        return str;
    }
}
like image 20
qJake Avatar answered Nov 14 '22 23:11

qJake