2016-11-11 49 views
1

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

System.OverflowException:溢出错误。 在System.Drawing.StringFormat.SetMeasurableCharacterRanges(CharacterRange []范围) 在System.Windows.Forms.LinkLabel.CreateStringFormat() 在System.Windows.Forms.LinkLabel.EnsureRun(图形克) 在System.Windows.Forms的(PaintEventArgs e) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16 layer) at System.Windows.Forms.Control.WmPaint(Message & m) at System.Windows.Forms。 Control.WndProc(Message & m) at System.Windows.Forms.Label.WndProc(Message & m) at 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; 
} 

回答

2

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() - 方法,做由你自己绘制complety。 (事情会更容易些,如果方法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); 
}