【问题标题】:Is there a way for exact positioning with string's PadLeft?有没有办法使用字符串 PadLeft 进行精确定位?
【发布时间】:2013-09-22 20:50:38
【问题描述】:

我想垂直编组冒号,但它们之间存在间隙,具体取决于字符大小。

我对所有的行都使用stringExpression1.PadRight(11)

是否有内置方法或者我需要measure 字符的宽度以使其更好地查看?

注意:必须是字符串,不能使用可视化组件,比如TextBlock

【问题讨论】:

  • 您需要测量 (Graphics.MeasureString) 或使用mono-spaced font
  • 答案是NO,你可能要使用一些fixed-width font
  • 你在哪里列出这些字符串,在一个winforms TextBox
  • 我知道你和那个蒂姆要去哪里......
  • @TimSchmelter 它是一个 WPF 。但它是图表的工具提示,图表仅接受 object 作为工具提示文本。我找不到此工具提示的模板。所以..我也不知道它使用的是哪种字体。

标签: c# wpf string padding


【解决方案1】:

您可以使用Tab“\t”。如果左侧描述(如 Systolic、Diastolic 等)是不变的,那么您可以添加适当数量的 "\t" 以提供正确的间距,如下所示。

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.AppendLine(String.Format("{0}\t\t: {1}", "Systolic", 80));
        stringBuilder.AppendLine(String.Format("{0}\t\t: {1}", "Diastolic", 170));
        stringBuilder.AppendLine(String.Format("{0}\t\t: {1}", "Pulse", 50));
        stringBuilder.AppendLine(String.Format("{0}\t: {1}", "LooooooongDesc", 50));
        this.tb.Text = stringBuilder.ToString();

谢里登编辑>>>

为了在WPF 中工作,您需要将TextBoxAcceptsTab property 设置为True

<TextBox AcceptsTab="True" ... />

【讨论】:

  • 表达式;收缩...是Localized,所以我不能做那样的事情。除此之外,我无法联系到TextBox。这是组件设计的不足。使用tabs 而不是PadRight(11) 有什么优势?
【解决方案2】:

这并没有真正帮助,但希望为 s.one 指明一条道路

用法

            List<string> items = new List<string>() {        
                          UIResources.Systolic, 
                          UIResources.Diastolic, 
                          UIResources.Pulse };

            var result = GetAlined(items);

帮助代码

    const string EMPTYLINE="                                                                                                                                ";   
    public static int GetDefaultItemWidth(string text,System.Drawing.Font font=null)
    {
        if (font == null)
            font = System.Drawing.SystemFonts.DefaultFont;

        return System.Windows.Forms.TextRenderer.MeasureText(text, font).Width;
    }

    public static List<string> GetAlined(List<string> inputString)
    {
        double max = default(double);
        var spaceWidth = GetDefaultItemWidth(" ");
        foreach (var item in inputString)
        {
            var width = GetDefaultItemWidth(item);

            max = Math.Max(max, width);
        }

        List<string> resultItems = new List<string>(inputString.Count);
        foreach (var item in inputString)
        {
            var width = GetDefaultItemWidth(item);
            if ((max - width) > spaceWidth)
            {
                var spaceCount = (int)Math.Round((max - width) / spaceWidth);
                string resultItem = item;
                resultItem=EMPTYLINE.Take(spaceCount).ToString() + resultItem;
                resultItems.Add(resultItem);
            }
            else
            {
                resultItems.Add(item);
            }
        }
        return resultItems;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-23
    • 2021-12-30
    • 2011-11-29
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    相关资源
    最近更新 更多