自定义数字键盘并将特定数字发送到活动文本框
我制作了一个带有数字的自定义小键盘。它是一个框架和其中的按钮。我把这个组件放到了表单上。现在我不知道如何捕获表单上的活动控件,然后将特定于按钮的数字发送到该控件。
有人可以帮我吗?
I made a custom numpad with numbers on it. It's a frame and buttons in it. I put this component onto the form. Now I don't know how to capture the active control on the form and then send number specific to the button to this control.
Can someone help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IMO 最简单的方法是将按钮标题的数字存储在按钮的 Tag 属性中。然后,您可以对所有按钮使用相同的事件处理程序:
另一种选择是读取事件中的标题,但是您必须进行从字符串到整数的转换,如果您不小心分配了没有数字标题的按钮,这将中断到事件处理程序:
请注意,这样您就不必关心您的组件是否是 ActiveControl;您只需将事件处理程序分配给组件的每个按钮,而无需执行任何其他操作,然后如果触发该事件,您就知道活动控件是您的组件以及单击了其中的哪一个按钮。
编辑:根据下面的评论,这个问题比看起来的要多一些。
如果您尝试将号码发送到 TEdit(您的“文本框”),则必须执行其他一些操作。首先,不要使用 TButton 或 TBitBtns,因为它们会获得焦点并将焦点从您的编辑控件上夺走。请改用 TSpeedButton。将您想要的每个标题的标题设置为您希望其放入 TEdit 中的编号。
其次,由于您想将内容放入 TEdit 中,因此无需费心使用标签。将所有 TSpeedButton 的事件处理程序设置为此一个(假设您的 TEdit 是 Edit1):
这会将 TEdit Edit1 中的任何选定文本替换为 SpeedButton 的标题,或将其添加到当前插入符号(编辑光标)位置编辑。
如果您希望将其放入多个 TEdit 中的任何一个中,也可以进行一些细微的更改:
如果这仍然不是您想要完成的任务,请编辑您的原始问题并向其添加更多信息,所以我们有机会帮助您得到答案。
The easiest way IMO is to also store the number that is the caption of the button in the button's Tag property. You can then use the same event handler for all buttons:
The other alternative is to read the caption in the event, but then you have to do a conversion from string to Integer, which will break if you accidentally assign a button without a numeric caption to the event handler:
Note that this way you don't have to care about whether your component is the ActiveControl or not; you just assign the event handler to every one of your component's buttons and nothing else, and then if the event is triggered you know that the active control is your component and which one of it's buttons was clicked.
EDIT: Based on the comment below, there's a little more to the question than it appears.
If you're trying to send the number to a TEdit (your "textbox"), you have to do a couple of other things. First, don't use TButtons or TBitBtns, as they get focus and will take the focus away from your Edit control. Use TSpeedButton instead. Set the Caption of each one you want to the number you want it to put in the TEdit.
Second, since you want to put the content in the TEdit, you don't need to bother with the tag. Set the event handler for all the TSpeedButtons to this one (which assumes your TEdit is Edit1):
This replaces any selected text in the TEdit Edit1 with the caption of the SpeedButton, or adds it at the current caret (edit cursor) position in the TEdit.
If you're looking to put it in any one of a number of TEdits, a slight change will handle that as well:
If this is still not what you're looking to accomplish, please edit your original question and add more information to it, so we have a chance of helping you get your answer.
您没有提及您正在使用的 Delphi 版本,但如果它是 Delphi 2010 或更高版本,您可能需要使用 TTouchKeyboard 组件在面板上可用,并将 Layout 属性设置为 NumPad。
非常方便,因为它为您处理所有细节。
You don't mention the Delphi version you're using, but if it is Delphi 2010 or greater, you may want to use the TTouchKeyboard component available on the palette and set the Layout property to NumPad.
Very handy, because it handles all the details for you.
尝试使用表单的ActiveControl属性。或者TScreen类的ActiveControl属性和OnActiveControlChange事件。
Try to use ActiveControl propety of the form. Or ActiveControl property and OnActiveControlChange event of the TScreen class.