我有。 Net
簡單text box
應用程序,用戶需要添加最多15位數的數字。 如果數字長度小於15,我希望在輸入數字前加零前導零。 例如,如果用戶插入的數目1002
我想text box
自動添加000000000001002
,所以我通過我的text box
TextChanged event
嘗試到:通過文本框填充帶前導零的數字TextChanged事件
private void radTextBoxNum_TextChanged(object sender, EventArgs e)
{
string str = "";
if (radTextBoxNum.Text.Length < 15)
{
int currentlength = radTextBoxNum.Text.Length;
int addZeroes = 15 - radTextBoxNum.Text.Length;
for (int i = 0; i < addZeroes; i++)
{
str += "0";
}
}
string val = radTextBoxNum.Text;
radTextBoxNum.Text = "";
radTextBoxNum.Text = str + val;
}
所以我的問題是,插入後一個數字我得到這個錯誤:
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
事件處理程序會更改文本框中的文本,所以它會無限觸發,直到庫存溢出,即不能在其TextChanged事件處理程序中更改TextBox的文本。 – 2014-10-29 16:16:02
所以我可以自動執行,而用戶輸入數字? – 2014-10-29 16:17:40