簡體   English   中英

如何檢查文本框C#Windows 8應用程序的焦點

[英]How to check for focus of a textbox c# windows 8 app

我正在制作一個單位換算器應用程序,我希望如果您在INCHES文本框中輸入一個值,則其他文本框會更改(英里,英尺等。每個單元都是一個不同的文本框)。 我正在為每個文本框使用文本更改事件。 當您在“ MILES”中輸入一個值,然后出現“ MILES”的文本更改事件開始工作,而其他對象的“文本更改事件”也開始工作時,就會出現問題。它永遠不會停止。 例如,我想檢查焦點。

if (INCHES got focus)
THEN
   //do the conversions and display it in the other textboxes 
   convert inches to miles,feet,etc... 
  // display it in the other textboxes 
   milestxtbox=the conversion from inches to miles.....

就像我說的,如果我不檢查焦點(那是我不知道如何在Windows存儲c#應用程序開發中做到這一點),那么每個人的文本框都將開始更改值,並且不會停止... :/。 我希望我對這個解釋足夠清楚,我總是很難解釋這些東西。 同樣,當我在文本框中編寫文本時,其他文本框將更改為轉換后的值,但是由於textchanged事件的發生,它不會是文本框無限循環的變化。 謝謝!!!

您可以嘗試將驗證移動到按鈕單擊,2個文本框用於值,另一個文本框用於度量單位,例如用於輸出的列表框,如下所示...

private void Button1_Click(object sender, EventArgs e)
{
     lstOutput.Items.Clear();//lstOutput is the listbox
     int inches = Convert.ToInt32(txtInches.Text);
     string UnitOfMeasure = txtUnitOfMeasure.Text.ToUpper();
     ConvertToYardsOrFeet(inches, UnitOfMeasure);
}

private void ConvertToYardsOrFeet(int inches, string UnitOfMeasure)
    {
        int Yard = 0;
        int Feet = 0;
        int Inches = 0;

        if (UnitOfMeasure == "Y")
        {
             Yard = inches / 36;
             Feet = inches % 36 / 12;
             Inches = inches % 12;

             lstOutput.Items.Add(Yard + " Yards" + Feet + " Feet" + Inches + " Inches.");

        }

        if (UnitOfMeasure == "F")
        {
            Feet = inches / 12;
            Inches = inches % 12;

            lstOutput.Items.Add(Feet + " Feet" + Inches + " Inches.");
        }
    }

當然,這只是一個示例,如果您希望可以添加其他單位

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM