Category: StackOverflow

Key/Value pairs in ComboBox using Delphi Firemonkey

I would like to use an enumerator to populate a Combobox with Key/Value pairs. Its important that I hide the key from the user and display the value only. On selecting I would like to capture the key associated with the selected value. The code looks something similar to this. var currentObj: ISuperObject; enum: TSuperEnumerator<IJSONAncestor>; while enum.MoveNext do begin currentObj := enum.Current.AsObject; cboUserList.Items.Add(currentObj.S['key'],currentObj.S['value']); end; The key currentObj.S['key'] should be capture on user select of the value currentObj.S['value'] which is visible to the user on the cboUserList dropdownlist. Any ideas?
Read More

Delphi visual component with Canvas

I have always written non-visual components; their creation is pretty easy and they work equally well under VCL and FMX. So far so good, but now I'm facing a problem. I used to inherit from TComponent but now I cannot anymore because my component called TRedistPreview really needs a procedure like this: procedure drawPreview(area: TCanvas); This procedure has to draw something (shapes, lines and colours) somewhere, for example in a TRectangle. I have seen online that TComponent doesn't have the ability to draw, so I should inherit from something else. I have found TWinControl (but that is VCL-only) and TCustomControl (VCL only, too). Could you please tell me what I should inherit from to get a canvas? I mean, I want to replace: TRedistPreview = class(TComponent) end; With: TRedistPreview = class(TSomeClassThatHasCanvas) end; Where I can call procedure drawPreview(area: TCanvas); and draw on a surface (like a TRectangle). I am looking for the lowest possible class in the hierarchy with a Canvas. Since this component is very useful to me under Windows and Android, I am looking for a Firemonkey implementation. From my research, I have seen that I could inherit from TRectangle, which is inside FMX.Objects, but I don't know if this is the right choice. What should I do? Also, if I needed this component to be in VCL, do I have to write another component that inherits from another class? As I've said, this is my first time with visual component writing, so I'd like someone to show me the right way!
Read More

ListView Filter not working after fetching data from datasnap server

I have an application made in Delphi Seattle, and i migrated to Delphi Tokyo. But my searches in ListView are no longer working properly. On a first use, it works correctly, i filter a record and edit it. I send the data to a datasnap server and fetch all records again to refresh the listview. But if I try to search again, it no longer finds the record, however browsing the listview I can find the record. Apparently all records that are not appearing on the screen when I reload the data are not being filtered. If I manually navigate to the record and perform the search, then it works. My Code to retrieve data from datasnap and show on listview: DadosSocio := F_ClientModule.ServerMethods1Client.GetSocio(0); Assert(TFDJSONDataSetsReader.GetListCount(DadosSocio) = 1); mem_Socios.Close; mem_Socios.AppendData(TFDJSONDataSetsReader.GetListValue(DadosSocio, 0)); mem_Socios.Open; lvSocios.ScrollTo(0); lvSocios.ScrollViewPos := 0; I use this code to clean the filter: for I := 0 to lvSocios.Controls.Count-1 do Begin if lvSocios.Controls[I].ClassType = TSearchBox then begin TSearchBox(lvSocios.Controls[I]).Text := ''; end; End; I tried to use this code to clean the filter, but it returns the error Argument out of Range: lvSocios.Items.Filter := nil;
Read More

Rad Studio 10.2.1 Memory Leaks Windows 10 store

It's well know around the Delphi community that there are memory leaks in Rad Studio 10.2.1 FMX. I did find a workaround for it: Workaround for fmx memory leaks I want to know some stuff about it. Does this only affect FMX Windows? Someone told me that if your app is on the windows store the memory leaks aren't a matter since windows store terminates them. Is this true? Here is his exact words: if you publish on Windows Store then it's not needed because all memory is automatically freed by the Store when an app is terminated.
Read More

Decompiling .SO files [on hold]

I created an application for Android using Embarcadero Delphi, then lost source code. But I have an apk file with .so file inside it. I don't need source code any more, but when I wrote this application, I compiled .rc into .res (using brcc32) and Delphi embedded it into lib.so file. Is there a way to extract my *.res file? It's important, 'cause it contains some grahic files that are important to me. Thanks much in advance.
Read More

How can I include an Apple Help Book when building my app for OSX

I have developed a Firemonkey app for Windows and OSX. I have also developed an Apple Help Book containing the help for my application when it is run on OSX. (I have a different help system when running in Windows.) Apple's recommended location for the help bundle is in the app's Contents/Resources folder. I can copy the help bundle there manually but it gets deleted every time I run my app in the IDE or deploy my app. I thought about trying to use the deployment manager but this only allows files to be deployed, not bundles. The help bundle contains a large number of files spread amongst various folders and it would be tedious to set each file up individually in the deployment manager. Moreover, I would have to change the setup every time I added or deleted a page from help since each page is a separate HTML file in the bundle. So for now, I am manually pasting the help bundle into Contents/Resources. Does anyone know of a more efficient method to incorporate the help bundle?
Read More

Delphi Firemonkey OSX how to show properly popup menu on button click?

I have a couple of buttons, which all have to show various popup menus when clicked. I use the following code in the onClick event of the buttons: procedure TForm1.btnExportClick(Sender: TObject); var pt:TPointF; begin pt.X:=0; pt.Y:=btnExport.Height; pt:=btnExport.LocalToAbsolute(pt); pt:=ClientToScreen(pt); mnuExport.Popup(pt.X,pt.y); end; When I click a button and release the mouse, the onClick event is triggered, the button turns blue and the popup menu appears. When I then click another button, without releasing the mouse (so it should not trigger a onclick event yet), the first popup menu vanish, but the popup menu of the other button appear and the onClick event is triggered (I see this in debugger). When I first close the popup menu by clicking somewhere else, then the other button also work as expected, but as long a popup menu is open, the buttons onClick event is triggered even I do not release the mouse button yet. There is definitely something wrong with my code or way to do it. Note: It only happen when running it on Mac OSX, not on Windows using FireMonkey. How to do it properly?
Read More

how work atomicCmpExchange under the hood?

To compare a double value from a background thread we can do atomicCmpExchange(...). But as the implementation of this function is hidden from the delphi source code, how it's done under the hood ? on windows, but also on other platform ios/android (so on Firemonkey) I would like to know if it's safe from the main thread to do ::main thread MyDouble := xxx; and from a background thread to do ::background thread if AtomicCmpExchange(MyDouble , -1, -1) = xxx then .... ;
Read More

How to detect when FMX List View is scrolled to the bottom?

I'm working on a Firemonkey TListView to display search results. This list loads 25 items at a time, but could potentially show hundreds of items. I need to detect when the user has scrolled down to the bottom, so that I can fetch the next 25 items to show in the list. However, I cannot find the appropriate properties to determine this. There's the OnPullRefresh event, but that applies to scrolling to the top of the list and pulling it down. What I need is similar, but for the bottom of the list instead of the top. There's the OnScrollViewChange event, which seems appropriate. There's also the ScrollViewPos property, which is also useful. However, I cannot figure out what to compare that number to - specifically, the maximum possible value for ScrollViewPos. Something like ScrollViewMax is what I would need. But I cannot find anything more to accurately detect that user has scrolled to the bottom. How can I detect when a user has scrolled to the bottom of a Firemonkey TListView so that I can load more search results? EDIT If this is not possible for whatever reason, there is an alternative to add a dummy item to the end of the list with a "Load More..." button. But I'd rather it be automated. EDIT2 I forgot to mention... I have the Search Box showing in this list view, and items can have a variable height. If calculating this based on item contents is the only way, then such a calculation needs to be perfect. I don't want to know when user is "approaching" or "near" the bottom, but when user exactly hits the bottom of the list.
Read More

DI container can’t resolve FMX controls

I have following project: MyForm unit(just empty form): unit uMyForm; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs; type TMyForm = class(TForm) end; implementation {$R *.fmx} end. App Unit: unit App; interface uses uMyForm, Spring.Container; type TApp = class private _myForm: TMyForm; public [Inject] constructor Create(myForm: TMyForm); end; implementation uses System.SysUtils; { TApp } constructor TApp.Create(myForm: TMyForm); begin _myForm := myForm; end; end. And build code: procedure BuildProject; begin GlobalContainer.RegisterType<TApp>; GlobalContainer.RegisterType<TMyForm>; GlobalContainer.Build; _app := GlobalContainer.Resolve<TApp>; end; Run BuildProject() causes Error: "Cannot resolve type: TMyForm". I was testing same configuration on VCL platform and there everything is ok. Do you have any idea what is wrong here? Edit1: I had to change problem description because I was wrong thinking that the problem occurs on both(VCL and FMX) platforms. @RudyVelthuis 's comment showed me that problem is only on FMX platform.
Read More

How to kill TBannerAd?

Situation (Delphi Berlin 10.1): in my application the main form calls the secondary form. Then the secondary form shows Admob's banner. The problem is even when I close the secondary form and clear the memory, the banner continues to be displayed. How to hide the banner completely? Thank you in advance. Here's the code of the secondary unit: unit ad_Unit; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Advertising, FMX.Controls.Presentation, FMX.StdCtrls; type TAdForm = class(TForm) OKButton: TButton; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); procedure FormShow(Sender: TObject); private FBannerAd: TBannerAd; public end; var AdForm: TAdForm; implementation {$R *.fmx} procedure TAdForm.FormClose(Sender: TObject; var Action: TCloseAction); begin if Assigned(FBannerAd) then begin FBannerAd.Hide; FBannerAd.DisposeOf; FBannerAd := nil; end; Action := TCloseAction.caFree; end; procedure TAdForm.FormCreate(Sender: TObject); begin FBannerAd := TBannerAd.Create(Self); FBannerAd.Parent := Self; FBannerAd.AdUnitID := '<ID>'; FBannerAd.Align := TAlignLayout.Top; FBannerAd.TestMode := True; end; procedure TAdForm.FormShow(Sender: TObject); begin FBannerAd.LoadAd; end; end.
Read More

Cannot add TMS async components to win32 firemonkey program

I have a firemonkey app targeted only at win32. I want to add the tms async serial communications component. When I do so and try to build it gets upset trying to compile the tms source code. It is non-trivial to change the tms source code. Where is it picking up the library path to the source code? If I remove that path will it use the existing .dcu files and work? Is there any other suggestions?
Read More

How can I stop changing style in firemonkey in ScaleChanged message

I use Firemokey 10.2. In macOS, internally when you move your from from a normal display to a retina display it automatically changes style of the controls. I would like to stop this message which is TScaleChangedMessage. I would appreciate If you could help me how can I stop this message in my app. In other word how can I stop changing from normal styles to High-Resolution Styles
Read More

Delphi: ARC and Objective-C wrapped objects – how to release?

In Delphi/FMX there is no ARC for the Objective-C objects represented by the import wrapper classes and interfaces. When dealing with Objective-C objects you’ll have to call retain and release yourself at the correct points. Allocating a new Objective-C object will initialize its reference count to 1 and calling release will drop it to 0 thus destroying it. http://ridingdelphi.blogspot.de/2014/01/the-quest-to-migrate-ios-squarecam-app_3169.html For example, i want to create a UILabel dynamically. According to the reference above, my code should be look like this (I'm assuming that I have chosen the positions for retail and release correctly): procedure TForm1.Button1Click(Sender: TObject); var lbl: UILabel; begin lbl := TUILabel.Wrap(TUILabel.alloc.init); lbl.retain; ... lbl.release; end; It does not work. What is the right way to release wrapped Objective-C objects in Delphi/FMX? With "It doesn't works" I mean that the UILabel is not released as expected ans still allocates the memory. I found this with the help of the Xcode Allocation Instrument.
Read More