当textBox1.Value更改时选中复选框 [英] Checking a checkbox when textBox1.Value changes

查看:173
本文介绍了当textBox1.Value更改时选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我有一个搜索宏。用户窗体中有一个文本框,下面有一个复选框。存储10个最近的搜索,并且在运行宏时,最新的搜索出现在文本框中。默认情况下,取消选中该复选框(标题为:"另存为新搜索
字符串")。我有一段代码可以在用户更改文本框中的值时进行检查,如果值更改,则会选中复选框(意味着,将保存新字符串)。



以下是代码:

 


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 
  
 'textBoxValue is the value that appears in the textBox1 when a macro is run
 If UserForm3.TextBox1.Value <> textBoxValue Then 
  CheckBox1.Value = 1 
 Else: CheckBox1.Value = 0 
 End If 
 ... 
End Sub 

推荐答案

这是因为代码是在更新值之前执行的。它允许操纵输入。当您第二次按下按钮时,第一次更改了该值,因此勾选了该框。

That's because the code is executed before the value is updated. It allows to manipulate the input. When you press the button the second time the value has been already updated with the first change, thus the box is ticked then.

这可以解决您的问题:

If UserForm3.TextBox1.Value & chr(KeyAscii)   <> textBoxValue Then 


或者您可以使用keyup事件。

or you could use keyup event instead.


这篇关于当textBox1.Value更改时选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆