解析字体字符串时索引超出数组错误?
我在第 574 行不断收到“索引超出数组范围”错误,即:
label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));
...我正在解析的以下文本文件包含这个确切的信息:
Label
"hi tyler"
23, 76
Arial,12.5
...我可以成功解析所有其他信息(只是不是最后一行),并且我拥有的代码是:
MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\r\n""([^""]+)""\r\n(\d+), (\d+)");
foreach (Match match in lines)
{
string control = match.Groups[1].Value;
string text = match.Groups[2].Value;
int x = Int32.Parse(match.Groups[3].Value);
int y = Int32.Parse(match.Groups[4].Value);
String cfont = match.Groups[5].Value;
string color = match.Groups[6].Value;
Console.WriteLine("{0}, \"{1}\", {2}, {3}, {4}, {5}", control, text, x, y, cfont, color);
switch (control)
{
case "Label":
Label label = new Label();
label.Text = text;
label.AutoSize = true;
label.IsAccessible = true;
label.MouseClick += new MouseEventHandler(label_MouseClick);
label.MouseDoubleClick += new MouseEventHandler(label_MouseDoubleClick);
label.MouseDown += new MouseEventHandler(label_MouseDown);
label.MouseMove += new MouseEventHandler(label_MouseMove);
label.MouseUp += new MouseEventHandler(label_MouseUp);
label.Location = new Point(x, y);
canvas.Controls.Add(label);
String fontName = cfont;
String[] fontNameFields = fontName.Split(',');
label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));
...我认为可能有获取字体内容的正则表达式有问题...我不知道,但它就是行不通,有人可以帮忙吗?
有关此问题的历史记录,请参阅:解析字体信息并将其转换为System.Drawing.Font
i keep getting an "Index is out of bounds of array" error on line # 574 which is:
label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));
... The following text file i am parsing contains this exact information:
Label
"hi tyler"
23, 76
Arial,12.5
...I can successfully parse all the other info (just not the very last line), and the code i have is:
MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\r\n""([^""]+)""\r\n(\d+), (\d+)");
foreach (Match match in lines)
{
string control = match.Groups[1].Value;
string text = match.Groups[2].Value;
int x = Int32.Parse(match.Groups[3].Value);
int y = Int32.Parse(match.Groups[4].Value);
String cfont = match.Groups[5].Value;
string color = match.Groups[6].Value;
Console.WriteLine("{0}, \"{1}\", {2}, {3}, {4}, {5}", control, text, x, y, cfont, color);
switch (control)
{
case "Label":
Label label = new Label();
label.Text = text;
label.AutoSize = true;
label.IsAccessible = true;
label.MouseClick += new MouseEventHandler(label_MouseClick);
label.MouseDoubleClick += new MouseEventHandler(label_MouseDoubleClick);
label.MouseDown += new MouseEventHandler(label_MouseDown);
label.MouseMove += new MouseEventHandler(label_MouseMove);
label.MouseUp += new MouseEventHandler(label_MouseUp);
label.Location = new Point(x, y);
canvas.Controls.Add(label);
String fontName = cfont;
String[] fontNameFields = fontName.Split(',');
label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));
...i think there may be something wrong with the regex that gets the font stuff... i dont know, but it just won't work, can somebody please help?
For a history of this problem, see: Parsing font info and converting it to System.Drawing.Font
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Try
It 与slashmais 解决方案类似,只是在字体及其大小方面有更多限制,并且更接近您的原始解决方案,我喜欢它,因为块更明确可见。
Try
It is similar to slashmais solution, only a bit more restrictive in terms of what is a font and its size, and closer to your original, which I like because the blocks are more explicitly visible.