int Height=Convert.ToInt32(float.Parse(txtHeight.Text))
The above line of code was executed the windows form shrinking to small size and controls are not visible, instead it is causing my form to close.
int Height=Convert.ToInt32(float.Parse(txtHeight.Text))
The above line of code was executed the windows form shrinking to small size and controls are not visible, instead it is causing my form to close.
考虑到float.Parse(txtHeigth.Text))
,很可能存在解析异常。
第一:使用 TryParse(..)
第二:指定Culture
always。
所以使用方法TryParse (example)
var result = 0;
if(float.TryParse(txtHeight.Text,
NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {
//Conversion succeed
}
第三:至少从提供的代码来看,看不到先将文本转换为float
,然后再转换为 的感觉int
。立即将其转换为所需的目标格式,因此int
.