[英]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.