Smobiler上海石磨_.NET移动开发平台
标题:
TextBox.Focus()获取焦点失败
[打印本页]
作者:
EnZo
时间:
2024-10-16 11:53
标题:
TextBox.Focus()获取焦点失败
本帖最后由 EnZo 于 2024-10-16 11:54 编辑
窗体页面中有两个TextBox,在第一个TextBox使用SubmitEditing事件,对另一个TextBox进行获取焦点。最后点击按钮执行数据提交操作。再之后,所有的Focus就全部失效,重新打开后依然只有第一次有效
[attach]2645[/attach]
private void txtLockerNo_SubmitEditing(object sender, EventArgs e)
{
if (txtLockerNo.Text == "") return;
txtLockerNo.ReadOnly = true;
txtBoxID.ReadOnly = false;
txtBoxID.Focus();
}
private void btnCommit_Press(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
try
{
int ret = 0;
conn.ConnectionString = constr;
conn.Open();
string storagePlaceStr = string.Join(",", dtBox.AsEnumerable().Select(x => x.Field<string>("盒号")).ToArray());
//常规写入数据库
SqlCommand cmd = new SqlCommand("P_CSPCK_PDA_InLocker", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter fSqlParameter = new SqlParameter();
cmd.Parameters.Add(new SqlParameter("@LockerNo", SqlDbType.NVarChar, 50));
cmd.Parameters["@LockerNo"].Value = txtLockerNo.Text;
cmd.Parameters.Add(new SqlParameter("@StoragePlaceNo", SqlDbType.Text, 5000));
cmd.Parameters["@StoragePlaceNo"].Value = storagePlaceStr;
cmd.Parameters.Add(new SqlParameter("@Results", SqlDbType.NVarChar, 50));
cmd.Parameters["@Results"].Direction = ParameterDirection.Output;
cmd.Parameters.Add(new SqlParameter("@Msg", SqlDbType.NVarChar, 255));
cmd.Parameters["@Msg"].Direction = ParameterDirection.Output;
ret = cmd.ExecuteNonQuery();
conn.Close();
if (cmd.Parameters["@Results"].Value.ToString() == "000")
{
MessageBox.Show(string.Format("数据提交成功!,本次入【{0}】袋", dtBox.Rows.Count));
dtBox.Clear();
tableView1.DataSource = dtBox;
tableView1.DataBind();
txtBoxID.Text = "";
txtBoxID.ReadOnly = true;
txtLockerNo.Text = "";
txtLockerNo.ReadOnly = false;
txtLockerNo.Focus();
}
else
{
MessageBox.Show("数据提交失败", "错误");
}
}
catch (DataException ex)
{
MessageBox.Show("提交失败,请确认无线网络已连接!!" + ex.ToString());
txtBoxID.Text = "";
txtBoxID.ReadOnly = false;
conn.Close();
conn.Dispose();
}
}
作者:
Lula.Jin
时间:
2024-10-16 17:45
使用的designer 是什么版本的?
可以尝试改成调整 btnCommit_Press里的部分代码
MessageBox.Show(string.Format("数据提交成功!,本次入【{0}】袋", dtBox.Rows.Count), (obj, args) =>
{
dtBox.Clear();
tableView1.DataSource = dtBox;
tableView1.DataBind();
txtBoxID.Text = "";
txtBoxID.ReadOnly = true;
txtLockerNo.Text = "";
txtLockerNo.ReadOnly = false;
txtLockerNo.Focus();
});
作者:
EnZo
时间:
2024-10-17 09:22
试过了,还是不可以,不仅仅是提交之后的focus失效,提交之后,哪怕我手动选择LockerNo之后,扫码也不会执行boxid的focus
作者:
EnZo
时间:
2024-10-17 09:35
Lula.Jin 发表于 2024-10-16 17:45
使用的designer 是什么版本的?
可以尝试改成调整 btnCommit_Press里的部分代码
昨天更新了6.5的Designer,并且代码也改过了,还是一样的情况
作者:
Lula.Jin
时间:
2024-10-17 10:17
技术部回复:TextBox.ReadOnly是异步的,当 txtBoxID.ReadOnly = false; txtBoxID.Focus(); 一起执行时 ,readonly没有执行成功就已经执行foucus ,建议另开线程执行ReadOnly
例如
private void textBox1_SubmitEditing(object sender, EventArgs e)
{
Thread thread1 = new Thread(new ThreadStart(Thread1));
//调用Start方法执行线程
thread1.Start();
Thread.Sleep(1000);
textBox2.Focus();
}
private void Thread1()
{
textBox1.ReadOnly = true;
textBox2.ReadOnly = false;
this.Form.Client.RenderFlush();//在线程中更新ui 需要执行此方法
}
欢迎光临 Smobiler上海石磨_.NET移动开发平台 (https://www.smobiler.com/)
Powered by Discuz! X3.2