c# - 单击选定的组合框第一个文本框。Focus() 在 C# 中工作不完整
问题描述
为什么 textbox.Focus() 工作不完整?当我单击选定的组合框时,第一个文本框必须是焦点,但它只集中了片刻就迷路了,我不知道为什么会这样。
它只是在组合框事件中有问题。当我单击“清除”按钮时,它没有问题。当第一个文本框文本更改下一个文本框焦点时,它也没有问题。
我的代码有什么问题?
private void combModel_SelectedIndexChanged(object sender, EventArgs e)
{
gFan.Visible = false;
gCom.Visible = false;
gCon.Visible = false;
pictureBox1.Visible = false;
clr();
OleDbCommand cmd;
OleDbDataReader reader;
string sql = "select * from model where model = '"+ combModel.Text +"'";
string model, part, valchk, type;
conn.Open();
cmd = new OleDbCommand(sql, conn);
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
pictureBox1.Visible = true;
while (reader.Read())
{
model = reader.GetValue(1).ToString();
type = reader.GetValue(2).ToString();
part = reader.GetValue(3).ToString();
valchk = reader.GetValue(4).ToString();
if (type == "Compressor")
{
gCom.Visible = true;
pCom.Text = part;
vCom.Text = valchk;
}
else if (type == "Condenser")
{
gCon.Visible = true;
pCon.Text = part;
vCon.Text = valchk;
}
else if (type == "Fan Motor")
{
gFan.Visible = true;
pFan.Text = part;
vFan.Text = valchk;
}
}
timeStatus.Start();
}
else
{
gFan.Visible = false;
gCom.Visible = false;
gCon.Visible = false;
btnRun.Visible = false;
}
conn.Close();
if (txtFan.Visible)
{
txtFan.Focus();
}
else if (txtComp.Visible)
{
txtComp.Focus();
}
else if (txtCond.Visible)
{
txtCond.Focus();
}
}
解决方案
很多时候(并非总是)您描述的那种不稳定的行为与在 UI 线程上处理 Windows 消息有关。在消息处理程序中调用其他 UI 控件(设置可见性,但尤其是更改焦点)可能会干扰这些消息的有序执行。
允许 SelectedIndexChanged 处理程序立即返回,而不是在 UI 线程被阻塞时尝试实际工作是一种很好的做法。执行此操作的方法是使用以下语法完成工作并包含在BeginInvoke中:
private void combModel_SelectedIndexChanged(object sender, EventArgs e)
{
BeginInvoke((MethodInvoker)delegate { ExecCombModel(); });
}
...真正的工作放在一个单独的方法中。(我将该方法命名为ExecCombModel(),但不管你怎么称呼它。)该函数将在组合框更改完全完成并以新状态绘制后立即执行。
private void ExecCombModel()
{
gFan.Visible = false;
gCom.Visible = false;
gCon.Visible = false;
pictureBox1.Visible = false;
clr();
OleDbCommand cmd;
OleDbDataReader reader;
string sql = "select * from model where model = '" + combModel.Text + "'";
string model, part, valchk, type;
conn.Open();
cmd = new OleDbCommand(sql, conn);
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
pictureBox1.Visible = true;
while (reader.Read())
{
model = reader.GetValue(1).ToString();
type = reader.GetValue(2).ToString();
part = reader.GetValue(3).ToString();
valchk = reader.GetValue(4).ToString();
if (type == "Compressor")
{
gCom.Visible = true;
pCom.Text = part;
vCom.Text = valchk;
}
else if (type == "Condenser")
{
gCon.Visible = true;
pCon.Text = part;
vCon.Text = valchk;
}
else if (type == "Fan Motor")
{
gFan.Visible = true;
pFan.Text = part;
vFan.Text = valchk;
}
}
timeStatus.Start();
}
else
{
gFan.Visible = false;
gCom.Visible = false;
gCon.Visible = false;
btnRun.Visible = false;
}
conn.Close();
if (txtFan.Visible)
{
txtFan.Focus();
}
else if (txtComp.Visible)
{
txtComp.Focus();
}
else if (txtCond.Visible)
{
txtCond.Focus();
}
}
我不能保证这会解决您的特定问题,但总的来说这是一种很好的做法,并且已经解决了我在自己的项目中处理过的许多类似问题。考虑试一试。
推荐阅读
- mysql - 主从复制不工作但状态正常
- android - 如何使用 OkHttpClient 在 Kitkat 上下载 apk 文件
- azure - MarkLogic - 我们可以在 Azure Blob 上配置计划备份吗
- ios - “未定义的符号:_OBJC_CLASS__$_LCOpenSDK_PlayWindow”出错
- android - 尝试从 URI 上传图像文件时,该位置不存在对象
- linux - Electron breakpad minidump 版本不匹配
- eclipse - 使用 Eclipse 搜索的“OR”正则表达式
- c# - 两种语言的机器人
- sql-server - 为 BCP 实用程序创建查询时出现单引号问题
- linux - 如何根据标签将单个 XML 文件拆分为多个