Page 1 of 1

SetMatchMode for control

Posted: 07 Aug 2022, 23:31
by hemsith14_
Hey, I use this to find if control is in focus:

Code: Select all

ControlFocus(_ThisControl) 
{
	ControlGetFocus, _Focus
	Return  _Focus = _ThisControl ? True : False
}
How can I set title match so even if the name of the control only has part of the variable in it, it will still match an #if statement?

Thanks

Re: SetMatchMode for control

Posted: 08 Aug 2022, 00:36
by boiler
You can InStr() to check if the contents of one variable are contained within another. There is no “SetMatchMode” for things other than window titles.

By the way, this line doesn’t need the ternary operator:

Code: Select all

	Return  _Focus = _ThisControl ? True : False
It returns True or False like this:

Code: Select all

	Return  _Focus = _ThisControl

The way you have it, you’re saying “If True, return True, else return False” rather than just returning the True or False result.

Re: SetMatchMode for control

Posted: 08 Aug 2022, 10:25
by hemsith14_
boiler wrote:
08 Aug 2022, 00:36
You can InStr() to check if the contents of one variable are contained within another. There is no “SetMatchMode” for things other than window titles.

By the way, this line doesn’t need the ternary operator:

Code: Select all

	Return  _Focus = _ThisControl ? True : False
It returns True or False like this:

Code: Select all

	Return  _Focus = _ThisControl

The way you have it, you’re saying “If True, return True, else return False” rather than just returning the True or False result.
How do I implement InStr into my function?

Re: SetMatchMode for control

Posted: 08 Aug 2022, 12:26
by boiler
This will return True/1 if the string that is passed to the function is contained in the name of the focused control. I would add the A for the WinTitle parameter as I've shown to make sure it's referencing the active window.

Code: Select all

ControlFocus(_ThisControl) 
{
	ControlGetFocus, _Focus, A
	Return InStr(_Focus, _ThisControl)
}

Re: SetMatchMode for control

Posted: 08 Aug 2022, 12:50
by hemsith14_
Thanks!

Is there an option to do something like: search only for the first 5 characters of string?

Re: SetMatchMode for control

Posted: 08 Aug 2022, 16:00
by boiler
If what you passed is expected to be exactly the first 5 characters, then you could do this:

Code: Select all

Return SubStr(_Focus, 1, 5) = _ThisControl