AddClearBtnToEdit - Add 'clear text' btn over an edit ctrl

Post your working scripts, libraries and tools for AHK v1.1 and older
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

AddClearBtnToEdit - Add 'clear text' btn over an edit ctrl

18 Mar 2015, 23:14

v1.21 (2015-3-21)

Example:

Code: Select all

; AddClearBtnToEdit_Sample.ahk

#NoEnv
#SingleInstance Force
SetBatchLines -1

Gui, +Resize
Gui, Add, Edit, hwndHEDIT w200
	AddClearBtnToEdit(HEDIT)

Gui, Add, Edit, y+30 w135 hwndHEDIT2  r4, Click Me --->
	AddClearBtnToEdit(HEDIT2, { c      : "White", cBG    : "008000"
	                          , c_hot  : "008000", cBG_hot: "White"
	                          , tooltip: "Clear text", onClick: "ClickedFunc" } )

Gui, Add, Edit, x+30 w135 hwndHEDIT3  r4
	AddClearBtnToEdit(HEDIT3, { c    : "Black", cBG    : "White", border: True
	                          , c_hot: "White", cBG_hot: "Black"
	                          , w: 30, left: -20, top: 10 } )

Gui, Add, Edit, xm w300 h200 hwndHEDIT4
	AddClearBtnToEdit(HEDIT4, { c:     "Red",   cBG: "White"
	                          , c_hot: "White", cBG_hot: "Red", w: 50, h: 50, round_hot: "w"})
Gui, Show
Return

/*
GuiSize:
	AutoXYWH("w", HEDIT) ; http://ahkscript.org/boards/viewtopic.php?f=6&t=1079&start=20#p20635

	; Note: Only edit control has width changing needs to call "AutoMove"
	AddClearBtnToEdit.AutoMove(HEDIT)
Return
*/

GuiClose:
ExitApp

ClickedFunc(ParentHEDIT) {
	MsgBox, % ParentHEDIT " clicked."
}
Screenshot:
Image

:arrow: Source: gist

special thanks the help from lblb, and posts from just me, Serenity.

Changelog
Last edited by tmplinshi on 21 Mar 2015, 07:40, edited 7 times in total.
Guest

Re: AddClearBtnToEdit - Add 'clear button' over an edit cont

18 Mar 2015, 23:38

Nice tmplinshi :)
Just did a quick test, all worked fine until I used anchor :/

See screenshot:
http://img538.imageshack.us/img538/28/bwkbqW.png
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AddClearBtnToEdit - Add 'clear button' over an edit cont

20 Mar 2015, 17:08

1.10 (2015-3-21)
- Added 'OnClick' option
- Added .AutoMove() to move button when Edit's width changed
Hello Guest, try this version. I've updated the example code.
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: AddClearBtnToEdit - Add 'clear button' over an edit cont

20 Mar 2015, 17:30

Hi tmplinshi,

This is nice! the OnClick option is a great idea. It made me think that maybe you don't have to limit it to an X... Maybe you could add a parameter where the text (i.e. X) could be changed to anything else? or add an icon? Although, I understand the purpose of this is really simply to add a "clear" button, which is very sufficient in itself.

Also, could you add something so that an X appears when the text of the Edit is set through Guicontrol? For example, if I use GuiControl to set the text to "Hello" in one of the edits in your sample code, the X doesn't appear.

Nice and simple, great stuff!
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AddClearBtnToEdit - Add 'clear button' over an edit cont

20 Mar 2015, 18:32

@lblb
Sorry, use icon as 'clear button' will not add in this script, I only want to use text control in this script, to keep it simple... But in fact the code now looks complicated :|.
If use just me's ImageButton, this script will be much easier, with a much beautiful button.
Also, could you add something so that an X appears when the text of the Edit is set through Guicontrol? For example, if I use GuiControl to set the text to "Hello" in one of the edits in your sample code, the X doesn't appear.
I've noticed that, but currently I don't know how to handle it.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AddClearBtnToEdit - Add 'clear text' btn over an edit ct

21 Mar 2015, 05:29

Changes in 1.20 (2015-3-21)
  • Added .ShowHideBtn() to show/hide the "Clear Btn".
    A single-line edit's changing can now be detected when you using GuiContrl or ControlSetText.
    But if it is a multi-line edit control, you have to call .ShowHideBtn(HEDIT), after using GuiContrl or ControlSetText.
Guest

Re: AddClearBtnToEdit - Add 'clear text' btn over an edit ct

21 Mar 2015, 06:51

Much better, but...
Create a resizable GUI and edit control with some initialized text, resize the GUI around, the clear text button is not there, or disappears.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AddClearBtnToEdit - Add 'clear text' btn over an edit ct

21 Mar 2015, 07:22

Guest wrote:Much better, but...
Create a resizable GUI and edit control with some initialized text, resize the GUI around, the clear text button is not there, or disappears.
Have you added AddClearBtnToEdit.AutoMove(HEDIT) in the GuiSize label? And note that only Edit control has width changing needs the AutoMove.
If that doesn't work, please provide an example.

ok, confirmed.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AddClearBtnToEdit - Add 'clear text' btn over an edit ct

21 Mar 2015, 07:39

@Guest
Fixed in v1.21. Thanks!
lx930129
Posts: 10
Joined: 13 Sep 2016, 03:41

Re: AddClearBtnToEdit - Add 'clear text' btn over an edit ctrl

12 May 2020, 09:25

Code: Select all

Error at line 71 in #include file "C:\Users\11483\Desktop\AddClearBtnToEdit\AddClearBtnToEdit.ahk".

Line Text: Init
Error: Duplicate declaration.

The program will exit.
tmplinshi,你好,为什么这个工具在我电脑上出现这个错误呢,我看脚本中似乎并不存在重复的函数声明。 :(
安装的ahk版本和系统为:
AutoHotkey v1.1.32.00 和win10 x64
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: AddClearBtnToEdit - Add 'clear text' btn over an edit ctrl

12 May 2020, 10:44

@lx930129, you could try changing line 69 to this:

Code: Select all

	static BtnInit := AddClearBtnToEdit.Init()
You don't have to use BtnInit, just don't use Init. :D

Regards,
burque505
lx930129
Posts: 10
Joined: 13 Sep 2016, 03:41

Re: AddClearBtnToEdit - Add 'clear text' btn over an edit ctrl

12 May 2020, 20:25

burque505 wrote:
12 May 2020, 10:44
@lx930129, you could try changing line 69 to this:

Code: Select all

	static BtnInit := AddClearBtnToEdit.Init()
You don't have to use BtnInit, just don't use Init. :D

Regards,
burque505
Great, it dose solve the problem! Thank you burque505!But I still don't know why this happened cause I think there is not duplicate declaration in the class definiton.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 236 guests