System.FormatException未处理 [英] System.FormatException was unhandled
本文介绍了System.FormatException未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试更改RichTextBox字体,样式,大小如下。但是在运行时间期间发生异常,如果是条件,则在最后一个错误是输入字符串格式不正确。
I'm trying change RichTextBox font, style, size as below. But exception occurred during at run time in last else if condition. An error is "Input string was not in a correct format."
public void FontFormation()
{
if (FontStyle.Bold.ToString() == ddlFontStyle.Text.ToString())
{
rchtxtMainBody.Font = new System.Drawing.Font(ddlFont.Text.ToString().Trim(), Convert.ToUInt32(ddlFontSize.Text), FontStyle.Bold);
}
else if(FontStyle.Italic.ToString() == ddlFontStyle.Text.ToString())
{
rchtxtMainBody.Font = new System.Drawing.Font(ddlFont.Text.ToString().Trim(), Convert.ToUInt32(ddlFontSize.Text), FontStyle.Italic);
}
else if(FontStyle.Regular.ToString() == ddlFontStyle.Text.ToString())
{
rchtxtMainBody.Font = new System.Drawing.Font(ddlFont.Text.ToString().Trim(), Convert.ToUInt32(ddlFontSize.Text), FontStyle.Regular);
}
}
在我的代码中,前两个条件正在执行而没有错误,但最后一个条件单独显示上述错误。如果我删除我的最后一个if条件,即FontStyle.Regular.ToString()== ddlFontStyle.Text。代码执行没有任何错误。
Plz告诉我如何解决这个问题。
In my code first two conditions are executing without error but last one alone showing aforesaid error. If i remove my last else if condition i.e FontStyle.Regular.ToString()== ddlFontStyle.Text. code is getting executed without any error.
Plz tell me how to fix this.
推荐答案
困难确定不知道究竟发生了什么,以及它在抱怨哪一行,但我会看一下你下拉列表中的字体大小 - 检查没有小数点(例如8.5)或后缀(例如10pt)。
我也会重构该代码:在例程的顶部进行一次转换,并停止将Text属性转换为stings - 它们已经是字符串,因此只会使代码混乱。
Difficult to be certain without knowing exactly what else is going on, and which line it is complaining about, but I would look at the font sizes in you drop down list - check that there are no decimal points ("8.5" for example) or suffixes ("10pt" for example).
I would also refactor that code: do the conversion once, at the top of the routine, and stop converting Text properties to stings - they are strings already, so that just clutters up your code.
这篇关于System.FormatException未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文