[英]How to get the integer value in TextBox?
我在 Windows 應用程序中有一個文本框。 此文本框只允許整數值而不是字符串。 任何人都可以有解決方案嗎?
用這個。
int value = Convert.ToInt32(textBox1.Text);
您使用此代碼並獲取您的整數值。謝謝
轉換它。
public int GetIntValue(TextBox tb)
{
try
{
return Convert.toInt32(tb.Text);
}
catch (Exception ex)
{
//This is called if the converting failed for some reason
}
return 0; //This should only return 0 if the textbox does not contain a valid integer value
}
像這樣使用它:
int number = GetIntValue(textBox1);
希望這可以幫助!
我從C# 中找到了一個解決方案How do I make a textbox that only accept numbers
希望它會幫助你。
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.