Delphi Pen in Negative

I am working on a project in delphi XE5. Various pop-up menus are created at runtime using the same function. Names are given each time to pop up using the "Handle" from the Tcomponent class.

popupname := 'XYZ' + IntToStr(handle); 

On some system, I get the value "Handle" as negative, when I try to give a name with a "-" to the component, I get the error message "XYZ-5645 is not a valid component name"

Could you offer me a way out?

+6
source share
1 answer

Instead of IntToStr , which accepts a signed integer, you can treat the handle as a pointer and therefore represent the numeric value as hex:

 popupname := Format('XYZ%p', [Pointer(Handle)]); 

This makes sense because the handle on Windows, as defined in the header files, is an untyped pointer, void* .

As an added benefit, your code will now be correct on both 32 and 64-bit platforms.

Thinking outside the field, the component may not need a name at all. If so, delete this code, leave it unmanned and thus pose your problem.

+10
source

Source: https://habr.com/ru/post/1012197/


All Articles