2

プログラムを作成し、その広範なテストを行ったところ、「FormatException が処理されませんでした。入力文字列が正しい形式ではありませんでした」というエラーが表示されます。テキストボックスのいずれかを空白のままにして「完了」ボタンを押すと問題が発生しますが、許可したい数値範囲である 0 未満または 59 を超える値を入力すると問題なく動作します。ボックスが空白のときにこのエラーが表示されないようにするにはどうすればよいですか? これは「btnFinished」の背後にある私のコードです:

   private void btnFinished_Click(object sender, EventArgs e)
    {
        if (lstCyclists.SelectedIndex >= 0)
        {
            Cyclists currentCyc = (Cyclists)lstCyclists.SelectedItem;
            //Decalre the minsEntered and secsEntered variables for txtMins and textSecs
            int minsEntered = int.Parse(txtMins.Text);
            int secsEntered = int.Parse(txtSecs.Text);

            try
            {
                //If the status of a cyclist is already set to Finished, show an error
                if (currentCyc.Finished.ToString() == "Finished")
                {
                    MessageBox.Show("A time has already been entered for this cyclist");
                }
                else
                {
                    //if a minute lower than 0 or greater than 59 has been entered, show an error
                    if (minsEntered < 0 || minsEntered > 59)
                    {
                        MessageBox.Show("You can only enter a minute up to 59");
                    }
                    //if a second lower than 0 or greater than 59 has been entered, show an error
                    else if (secsEntered < 0 || secsEntered > 59)
                    {
                        MessageBox.Show("You can only enter a second up to 59");
                    }
                    else
                    {
                        //otherwise, set the status to finished and update the time
                        currentCyc.Finished = "Finished";
                        currentCyc.FinishedHours(Convert.ToInt32(txtHours.Text));
                        currentCyc.FinishedMins(Convert.ToInt32(txtMins.Text));
                        currentCyc.FinishedSecs(Convert.ToInt32(txtSecs.Text));
                        //pass the parameter to the scoreboard class to display it in lblCyclistsFinished
                        lblCyclistsFinished.Text += "\n" + finishLine.Scoreboard(currentCyc);
                        //add to the number of cyclists finished
                        Cyclists.NumFinished++;
                        lblnumFinished.Text = Cyclists.NumFinished.ToString();
                        //update the details box
                        DisplayDetails(currentCyc);
                        txtHours.Clear();
                    }
                }
            }
            catch
            //make sure all the time fields have been entered, otherwise show an error message
            {
                MessageBox.Show("Please ensure all time fields have been entered");
            }
        }
        else
            //make sure a cyclist has been selected when pressing "Finished", otherwise show an error message
        {
            MessageBox.Show("You must select a cyclist");
        }
    }
4

3 に答える 3

8

さて、これらの行を見てください:

int minsEntered = int.Parse(txtMins.Text);
int secsEntered = int.Parse(txtSecs.Text);

テキスト ボックスが空白の場合、これらは何を返すと思いますか?

int.Parse空のテキストボックスを呼び出さないでください。例えば:

int minsEntered = txtMins.Text == "" ? 0 : int.Parse(txtMins.Text);
// Ditto for seconds

もちろん、数値以外の何かを入力すると、これでも問題が発生します。int.TryParseおそらく代わりに使用する必要があります:

int minsEntered;
int.TryParse(txtMins.Text, out minsEntered);

ここでは の結果を無視しており、とにかく 0 のTryParseままになりminsEnteredますが、別のデフォルトが必要な場合は、次のようなものを使用します。

int minsEntered;
if (!int.TryParse(txtMins.Text, out minsEntered))
{
    minsEntered = 5; // Default on parsing failure
}

(または、その場合はエラー メッセージを表示できます...)

于 2012-11-09T16:54:06.957 に答える
3

テキスト ボックスのいずれかを空白のままにすると問題が発生する

それが問題です。int.Parse空の文字列で使用しています。空の文字列は有効な整数表現ではないため、解析は失敗します。

テキストボックスの値をテストし、代わりに妥当なものにデフォルト設定できます。

int minsEntered = 0;
int secsEntered = 0;

if(!string.IsNullOrWhitespace(textMins.Text))
{
  minsEntered = int.Parse(txtMins.Text);
}

if(!string.IsNullOrWhitespace(txtSecs.Text))
{
  secsEntered = int.Parse(txtSecs.Text);
}
于 2012-11-09T16:53:19.890 に答える
2

テキストが正常に解析されたかどうかを示す bool を返す int.TryParse を使用できます。

int minsEntered = 0;
if (int.TryParse(txtMins.Text, out minsEntered))
{
    // txtMins.Text is a valid integer.
}
于 2012-11-09T16:59:23.750 に答える