0

好的,所以我有一个用于打印方法的队列。它为我需要打印的每一行存储文本和选定的字体。下面的循环应该打印出队列的内容,但看起来 peek 返回的是对象的值,而不是对象的实际引用。有没有办法让它返回一个参考?

        while (reportData.Count > 0 && checkLine(yPosition, e.MarginBounds.Bottom, reportData.Peek().selectedFont.Height))
        {
            ReportLine currentLine = reportData.Peek();

            maxCharacters = e.MarginBounds.Width / (int)currentLine.selectedFont.Size;

            if (currentLine.text.Length > maxCharacters)
            {
                e.Graphics.DrawString(currentLine.text.Substring(0, maxCharacters), currentLine.selectedFont, Brushes.Black, xPosition, yPosition);
                yPosition += currentLine.selectedFont.Height;
                currentLine.text.Remove(0, maxCharacters);
            }
            else
            {
                e.Graphics.DrawString(currentLine.text, currentLine.selectedFont, Brushes.Black, xPosition, yPosition);
                yPosition += currentLine.selectedFont.Height;
                reportData.Dequeue();
            }
        }

ReportLine 是一个结构,因此除非另有说明,否则它总是按值传递。我不想将它更改为一个类,因为它的唯一目的是保存 2 条信息。

[编辑]

这就是 ReportLine 的样子。这很简单:

public struct ReportLine
{
    public string text;
    public Font selectedFont;
}
4

1 回答 1

3

text是一个类型的字段,string您希望它被currentLine.text.Remove(0, maxCharacters);. 但Remove不修改字符串,它返回一个新字符串。

尝试:

currentLine.text = currentLine.text.Remove(0, maxCharacters); 

并制作ReportLine一个参考类型:

public class ReportLine
{
    public string text;
    public Font selectedFont;
}  
于 2012-05-21T14:01:13.937 回答