价值未达到预期

发布于 2024-08-10 20:43:39 字数 850 浏览 4 评论 0原文

我正在构建一个使用文件来配置某些字体的应用程序。它是这样的:

Font = Verdana
Size = 12
Style = Bold

我的代码是这样的:

openDialog.ShowDialog();
string file = openDialog.FileName;
StreamReader reader = new StreamReader(file);
while (reader.Peek() <= 0)
{
    string line = reader.ReadLine();
    string[] data = Split(new[] { '=' });
    // property is in data[0]
    // value is in data[1]
    TextFont = data[1];
    TextSize = data[3];
    TextSt = data[5];
}
reader.Close();
reader.Dispose();

并像这样使用它:

textBox1.Font = new System.Drawing.Font(TextFont, 12F, FontStyle.Bold);

但是当我执行它时,我得到了这个错误:

参数异常

价值未达到预期

然后我有两个问题:

  • 我该如何解决这个问题?
  • 如何在 Font 方法中使用浮点数来代替 TextSize 的字符串来实现它?

谢谢。

I'm building a application that uses a file to configure some fonts. It's like this:

Font = Verdana
Size = 12
Style = Bold

And my code is like this:

openDialog.ShowDialog();
string file = openDialog.FileName;
StreamReader reader = new StreamReader(file);
while (reader.Peek() <= 0)
{
    string line = reader.ReadLine();
    string[] data = Split(new[] { '=' });
    // property is in data[0]
    // value is in data[1]
    TextFont = data[1];
    TextSize = data[3];
    TextSt = data[5];
}
reader.Close();
reader.Dispose();

And using it like this:

textBox1.Font = new System.Drawing.Font(TextFont, 12F, FontStyle.Bold);

But when I execute it I got this error:

ArgumentException

Value does not fall within the expected

Then I have two questions:

  • How can I solve this problem?
  • How can I use instead of a string for TextSize use a float to implement it in the Font method?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-08-17 20:43:39

您可能还遇到数据转换问题:Split() 方法返回一个字符串数组,但 TextSize 是浮点数,而 TextStyle 是枚举 (FontStyle)。虽然我们人类可以很容易地看出数字 12 和字符串“12”至少是相关的,但编译器却要挑剔得多。

您可以尝试对 TextSize 进行此操作:

float fSize;
if (float.TryParse(data[3], out fSize))
    TextSize = fSize;

处理 TextStyle 可能会有点棘手,因为您必须将字符串值与不同的枚举值进行比较。例如,要检测“粗体”样式,您可以编写:

if (String.Compare("Bold", data[5]) == 0)  // true if equal
    TextStyle = FontStyle.Bold;

干杯!
谦虚的程序员
,,,^..^,,,

You may also have data conversion issues: the Split() method returns an array of strings, but TextSize is a float, while TextStyle is an enumeration (FontStyle). While we as human beings can easily tell that the number 12 and the string "12" are at least related, compilers are a lot pickier.

You might try this for the TextSize:

float fSize;
if (float.TryParse(data[3], out fSize))
    TextSize = fSize;

Handling the TextStyle might be a little trickier, because you'll have to compare the string value against the different enumerated values. For example, to detect the "Bold" style, you would write:

if (String.Compare("Bold", data[5]) == 0)  // true if equal
    TextStyle = FontStyle.Bold;

Cheers!
Humble Programmer
,,,^..^,,,

风启觞 2024-08-17 20:43:39

您正在读取一行,然后尝试从中获取三个值。看看评论:

// property is in data[0]
// value is in data[1]

然后你正在使用 data[1]、data[3] 和 data[5]...

你可能想要类似的东西:

openDialog.ShowDialog();
string file = openDialog.FileName;
string[] lines = File.ReadAllLines(file);
foreach (string line in line)
{
    string[] data = line.Split('=');
    string property = data[0].Trim();
    string value = data[1].Trim();
    switch (property)
    {
        case "Font": TextFont = value; break;
        case "Size": TextSize = value; break;
        case "Style": TextSt = value; break;
        default:
          // Whatever you want to do here for properties you don't recognise
          break;
    }
}

You're reading a single line, but then trying to take three values from it. Look at the comment:

// property is in data[0]
// value is in data[1]

You're then using data[1], data[3] and data[5]...

You probably want something like:

openDialog.ShowDialog();
string file = openDialog.FileName;
string[] lines = File.ReadAllLines(file);
foreach (string line in line)
{
    string[] data = line.Split('=');
    string property = data[0].Trim();
    string value = data[1].Trim();
    switch (property)
    {
        case "Font": TextFont = value; break;
        case "Size": TextSize = value; break;
        case "Style": TextSt = value; break;
        default:
          // Whatever you want to do here for properties you don't recognise
          break;
    }
}
樱&纷飞 2024-08-17 20:43:39

Jon Skeet 已经回答了你的第一个问题,所以对于你的第二个问题(如何将字体大小解析为浮点数):

float.Parse(s, CultureInfo.InvariantCulture);

其中 s 是包含字体大小的字符串。

Jon Skeet already answered your first question, so for your second one (how to parse the font size as a float):

float.Parse(s, CultureInfo.InvariantCulture);

where s is the string containing the font size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文