【发布时间】:2023-03-04 00:23:01
【问题描述】:
我正在使用“标签”类中的 Get String 函数来组合行以在位图上打印。该程序编译得很好,并且之前的表单正确地通过了 LabelQueue(在我尝试打印位图之前它看起来没有问题)。这个特定的初始化器/构造器的所有代码都在下面。错误的代码行是“c++”之前函数的最后三行。
如果您需要我添加更多必要的代码,请告诉我。
我收到一个 IndexOutofRange 异常,声称它超出了数组的范围。
private LabelQueue lq;
public Print(LabelQueue queue)
{
InitializeComponent();
lq = queue;
pictureBox1.Image = new Bitmap(2550, 3300);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
System.Drawing.Font textFont = new System.Drawing.Font("Times New Roman", 8);
System.Drawing.SolidBrush textBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
System.Drawing.StringFormat textFormat = new System.Drawing.StringFormat();
int x, y, c = 0;
while (c < 30)
{
// Get coordinates for where to put values.
x = ((c % 3) * 600) + 300;
// Accounts for column gap
if (c % 3 > 0)
x = x + ((c % 3) - 1) * 75;
y = ((c % 10) * 270) + 300;
string firstLine, secondLine, thirdLine;
firstLine = lq.labels[c].GetLastName() + ", " + lq.labels[c].GetFirstName() + " " + lq.labels[c].GetMiddleName();
secondLine = lq.labels[c].GetNewStreet();
thirdLine = lq.labels[c].GetNewCity() + ", " + lq.labels[c].GetNewState() + lq.labels[c].GetNewZIP() + lq.labels[c].GetNewCountry();
formGraphics.DrawString(firstLine, textFont, textBrush, x, y, textFormat); // Line turning up the error
formGraphics.DrawString(firstLine, textFont, textBrush, x, y + 10, textFormat); // Naturally, both these lines would need to be fixed too
formGraphics.DrawString(firstLine, textFont, textBrush, x, y + 20, textFormat);
c++;
}
}
【问题讨论】:
-
你画的不是位图,而是显示器。使用调试器应该很容易找到异常
-
啊。我的错。那么如何让它绘制到声明的位图呢?我承认,我不知道,用谷歌搜索答案对我没有帮助。
-
我认为我可以正常工作,就打印到位图而言。不过,仍然没有解决字符串连接问题。
-
lq.labels中有多少个标签?很可能少于 30...lq.labels是一个数组吗?使用lq.labels.Length属性检查元素的数量。如果是List,您将检查Count属性。