【问题标题】:More than 32 links in LinkLabel?LinkLabel 中的链接超过 32 个?
【发布时间】:2016-11-11 05:16:20
【问题描述】:

我目前正在用 C# 制作一个使用 LinkLabels 的应用程序。我有一个函数可以为某个数组中的每个元素添加一个新链接。但是,该数组恰好有超过 32 个链接,当这种情况发生时,我会收到一个 OverflowException:

System.OverflowException:溢出错误。 在 System.Drawing.StringFormat.SetMeasurableCharacterRanges(CharacterRange[] 范围) 在 System.Windows.Forms.LinkLabel.CreateStringFormat() 在 System.Windows.Forms.LinkLabel.EnsureRun(图形 g) 在 System.Windows.Forms.LinkLabel.OnPaint(PaintEventArgs e) 在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16 层) 在 System.Windows.Forms.Control.WmPaint(消息和 m) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.Label.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

有没有办法覆盖SetMeasurableCharacterRanges 函数。这样当字符范围超过 32 个时它不会抛出该错误? 这是我的代码示例:

int LengthCounter = 0;
llbl.Links.Clear();
string[] props = AList.ToArray();

llbl.Text = string.Join(", ", props);
foreach (var Prop in props)
{
    llbl.Links.Add(LengthCounter, Prop.Length, string.Format("{0}{1}", prefix, Sanitize(Prop)));
    LengthCounter += Prop.Length + 2;
}

【问题讨论】:

    标签: c# .net winforms linklabel overflowexception


    【解决方案1】:

    SetMeasurableCharacterRanges 是这样实现的:

    /// <summary>Specifies an array of <see cref="T:System.Drawing.CharacterRange" /> structures that represent the ranges of characters measured by a call to the <see cref="M:System.Drawing.Graphics.MeasureCharacterRanges(System.String,System.Drawing.Font,System.Drawing.RectangleF,System.Drawing.StringFormat)" /> method.</summary>
    /// <param name="ranges">An array of <see cref="T:System.Drawing.CharacterRange" /> structures that specifies the ranges of characters measured by a call to the <see cref="M:System.Drawing.Graphics.MeasureCharacterRanges(System.String,System.Drawing.Font,System.Drawing.RectangleF,System.Drawing.StringFormat)" /> method.</param>
    /// <exception cref="T:System.OverflowException">More than 32 character ranges are set.</exception>
    public void SetMeasurableCharacterRanges( CharacterRange[] ranges )
    {
        int num = SafeNativeMethods.Gdip.GdipSetStringFormatMeasurableCharacterRanges( new HandleRef( this, this.nativeFormat ), ranges.Length, ranges );
        if( num != 0 )
            throw SafeNativeMethods.Gdip.StatusException( num );
    }
    

    StringFormat 类是密封的,SetMeasurableCharacterRanges 方法不是虚拟的,所以你不能覆盖它。在内部,它对 gdiplus.dll 进行 API 调用。

    您可以尝试从LinkLabel 继承自定义 LinkLabel 并覆盖OnPaint()-方法并自己完成绘图。 (如果方法 CreateStringFormat() 不是内部方法,事情会更容易。)

    或者您只是在FlowLayoutPanel 上使用多个 LinkLabels,每个标签只有一个链接:

    for( int i = 0; i < AList.Count; i++ )
    {
        string prop = AList[i];
        LinkLabel llbl = new LinkLabel()
        {
            AutoSize = true,
            Margin = new Padding( 0 ),
            Name = "llbl" + i,
            Text = prop + ", "
        };
        llbl.Links.Add( 0, prop.Length, string.Format( "{0}{1}", prefix, Sanitize( prop ) ) );
    
        flowLayoutPanel1.Controls.Add( llbl );
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 2022-08-04
      • 1970-01-01
      • 2013-05-19
      • 2020-08-05
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多