【问题标题】:How to check for focus of a textbox c# windows 8 app如何检查文本框 c# windows 8 应用程序的焦点
【发布时间】:2013-06-09 20:30:17
【问题描述】:

我正在制作一个单位转换器应用程序,我希望如果你在 INCHES 文本框中输入一个值,那么其他文本框会发生变化(英里、英尺等,每个单位都是不同的文本框)。我正在为每个文本框使用文本更改事件。当你在 let 中输入一个值时,问题就出现了 s 说 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 事件而导致文本框无限循环。谢谢!!!

【问题讨论】:

    标签: c# textbox focus


    【解决方案1】:

    您可以尝试将验证移动到按钮单击,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.");
            }
        }
    

    这只是一个例子,当然如果你想你可以添加其他单位

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      相关资源
      最近更新 更多