1

DispatcherTimer にテキストボックスから時間値を読み取らせたい: objTextBox. このコードを試してみましたが、TimeSpan は文字列と互換性がないように見えますか、何か間違ったことをしましたか?

エラー: 引数 1: 'string' から 'long' に変換できません

また; 時間は、テキスト ボックスで次のように表示する必要がありますか: 0、0、1、または 00:00:01?

ここにコード:

    private void testing()
    {
        string theText = objTextBox.Text;
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(listjob3_Tick);
        dispatcherTimer.Interval = new TimeSpan(theText);
        dispatcherTimer.Start();
    }
4

4 に答える 4

1

TimeSpanaから aに変換するには、stringを活用できますが、次のTimeSpan.Parse形式に準拠する必要があります[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

ws    is Optional white space.
-     is An optional minus sign, which indicates a negative TimeSpan. 
d     is Days, ranging from 0 to 10675199.
.     is A culture-sensitive symbol that separates days from hours. The invariant format uses a period (".") character.
hh    is Hours, ranging from 0 to 23. 
:     is The culture-sensitive time separator symbol. The invariant format uses a colon (":") character.
mm    is Minutes, ranging from 0 to 59. 
ss    is Optional seconds, ranging from 0 to 59. 
.     is A culture-sensitive symbol that separates seconds from fractions of a second. The invariant format uses a period (".") character.
ff    is Optional fractional seconds, consisting of one to seven decimal digits. 

したがって、実際には使用TimeSpan.Parseして文字列を渡すだけで日を変換できますが、分を変換する場合は、次のように入力を処理する必要があります。

var input = string.Format("00:{0}", objTextBox.Text.PadLeft(2, '0'));

var timeSpan = TimeSpan.Parse(input);適切にフォーマットされているため、発行でき、Parse成功します。別のオプションは、私が推測する分を日に変えることですが、それには浮動小数点の作業が必要であり、実際には、IMO ほど良いオプションではありません。

于 2013-04-15T18:26:10.420 に答える
1

あなたの例外はここにあると思います:

dispatcherTimer.Interval = new TimeSpan(theText);

代わりにこれを使用してください:

dispatcherTimer.Interval = new TimeSpan(Convert.ToInt64(theText));
于 2013-04-15T18:23:59.773 に答える
1

To convert string to TimeSpan, use TimeSpan.Parse(str)

于 2013-04-15T18:19:09.623 に答える
0

@Sneakybastardd:コンストラクターのオーバーロードに関するドキュメントを読みましたか? いずれも文字列引数を取らないことに注意してください。整数型が必要です。TimeSpan

ドキュメントを読んだ後、次のTimeSpanメソッドが役立つことがわかります。

  • Parse()
  • ParseExact()
  • TryParse()

フォーマットについては、「標準の TimeSpan フォーマット文字列」および「カスタム TimeSpan フォーマット文字列」を参照してください。また、さまざまなカルチャのさまざまなデフォルトの TimeSpan 形式について少し調査してください。

于 2013-04-15T18:24:49.117 に答える