按键清除文本框(Clear Textbox on key press)

编程入门 行业动态 更新时间:2024-10-22 05:10:03
按键清除文本框(Clear Textbox on key press)

有没有办法像在Excel中那样清除按键上的文本框。

我尝试了以下代码,但它在单击文本框时清除了文本。 我希望它在按下某个键时清除它。

Private Sub Text10_GotFocus() Text10.Value = "" End Sub

Is there any way to clear the textbox on keypress like in excel.

I tried the following code but it clears the text when clicking on the textbox. I want it to clear it when a certain key is pressed.

Private Sub Text10_GotFocus() Text10.Value = "" End Sub

最满意答案

只要该控件获得焦点,您就可以选择控件的整个文本内容。 然后您的按键将替换所选文本。

如果希望每个表单上的每个文本框都出现这种情况,可以将“行为输入字段”设置为“选择整个字段”。 (在Access 2007中,从Office按钮 - >访问选项 - >高级查找该设置,然后查看该对话框的编辑标题下。对于Access 2003,请参阅此页 。)

该设置不仅应用于表单控件,还应用于数据表视图中的表和查询。 如果这不是您想要的,您可以在表单的模块中使用VBA来选择仅特定控件的文本:

Private Sub MyTextBox_GotFocus() Me.MyTextBox.SelStart = 0 Me.MyTextBox.SelLength = Len(Me.MyTextBox) End Sub

如果要对多个控件执行此操作,可以创建一般过程:

Private Sub SelectWholeField() Me.ActiveControl.SelStart = 0 Me.ActiveControl.SelLength = Len(Me.ActiveControl) End Sub

然后从单个控件的焦点事件中调用该过程,如下所示:

Private Sub MyTextBox_GotFocus() SelectWholeField End Sub

You could select the control's entire text content whenever that control gets focus. Then your keypress would replace the selected text.

If you want that to happen for every text box on every form, you can set "Behavior entering field" setting to "Select entire field". (In Access 2007, find that setting from Office Button -> Access Options -> Advanced, then look under the Editing heading of that dialog. For Access 2003, see this page.)

Not only will that setting be applied to form controls, but also to tables and queries in datasheet view. If that's not what you want, you can use VBA in your form's module to select the text for only specific controls:

Private Sub MyTextBox_GotFocus() Me.MyTextBox.SelStart = 0 Me.MyTextBox.SelLength = Len(Me.MyTextBox) End Sub

If you want to do that for multiple controls, you could create a general procedure:

Private Sub SelectWholeField() Me.ActiveControl.SelStart = 0 Me.ActiveControl.SelLength = Len(Me.ActiveControl) End Sub

Then call that procedure from the got focus event of an individual control like this:

Private Sub MyTextBox_GotFocus() SelectWholeField End Sub

更多推荐

清除,Sub,textbox,clear,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-08-05 06:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   按键   Clear   press   key

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!