2017-02-16 1 views
0

私のプログラムでは、私は学生の学年を入力するtexboxesを持っています。しかし、私は010や020のような形式で数字を数字にしないように制限したいと思います。また、ユーザーが1桁を変更して別のテキストボックスに変更すると、自動的にこの数字(1)が1,0に変更されます。数字形式を受け入れないようにtexboxに制限する( "010")

私はこれを試しましたが、2番目の状態になるとエラーになります。

private void txt3Bimestre_Validated(object sender, EventArgs e) 
{ 
    if (txt3Bimestre.Text[0].ToString().Equals("1") || 
     txt3Bimestre.Text[0].ToString().Equals("2") || 
     txt3Bimestre.Text[0].ToString().Equals("3") || 
     txt3Bimestre.Text[0].ToString().Equals("4") || 
     txt3Bimestre.Text[0].ToString().Equals("5") || 
     txt3Bimestre.Text[0].ToString().Equals("6") || 
     txt3Bimestre.Text[0].ToString().Equals("7") || 
     txt3Bimestre.Text[0].ToString().Equals("8") || 
     txt3Bimestre.Text[0].ToString().Equals("9")) 
    { 

     if (txt3Bimestre.Text[1].ToString().Equals(",") || txt3Bimestre.Text.Substring(0, 2) == "10") 
     { 
     } 
    } 
    else 
    { 
     MessageBox.Show("Formato Inválido", "Alertaa", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     txt3Bimestre.Clear(); 
     txt3Bimestre.Focus(); 
    } 
} 
+2

あなたが行ったことを示す –

+5

あなたはサイトも初めてで、[ask]と[tour] – Plutonix

答えて

0

ここで問題となるのは、ユーザーがテキストボックスに必要なデータを入力しないようにすることです。

私は、テキストボックスを残したり、別のイベントを実行したりした後、データが正しいフォーマットであるかどうかをテストしたり、ユーザーに "間違い"。例えば

double value; 

bool ok = double.TryParse(txt3Bimestre.Text, out value)) 

if (ok) 
{ 
    txt3Bimestre.Text = value.ToString("0.00"); 
} 
else 
{ 
    MessageBox.Show("Formato Inválido", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    txt3Bimestre.Clear(); 
    txt3Bimestre.Focus(); 
} 

あなたが問題を持っている場合、または。小数点以下桁区切りの文化に関連する相違点については、CultureInfo.InvariantCultureを参照してください。

希望します。

関連する問題