Recent

Author Topic: [SOLVED] TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'  (Read 1559 times)

patyit

  • New Member
  • *
  • Posts: 27
Hi !

Below is a code issue that works perfectly on FPC 3.0.5 but indicates an error at runtime on FPC 3.2.0. Runtime Error: 'RunError (219)', Type Cast error.
I looked through the change list between FPC 3.0 and 3.2, but couldn’t figure out what had changed.

If anyone could tell me what was wrong, I would be very grateful !

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn;
  2.   var Editor: TWinControl);
  3. begin
  4.   if Column.Visible and not Column.ReadOnly then begin
  5.     if Column.Field = QStatNstat then begin
  6.       with Editor as TCustomComboBox do begin
  7.         Style := csDropDown;
  8.         Items.CommaText := FDataMod.GetParam('status');
  9.         DropDownCount := 15;
  10.         AutoComplete  := True;
  11.         TComboBox(Editor).OnEditingDone := @DBCBDone;  // <-- Type Cast error between TCustomComboBox and TComboBox
  12.       end;
  13.     end;
  14.   end;
  15. end;
  16.  
  17. procedure TForm1.DBCBDone(Sender: TObject);
  18. begin
  19.   if QStat.State in dsEditModes then
  20.     QStat.Post;
  21.   EBar.Clear;
  22.   EBar.SetFocus;
  23. end;
  24.  
« Last Edit: August 07, 2020, 03:46:41 pm by patyit »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'
« Reply #1 on: August 07, 2020, 01:59:30 pm »
I tested these code below on Lazarus 2.0.10 GTK2 Linux FPC 3.2.0, did not get any compile-time error:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls, DBGrids;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     ComboBox1: TComboBox;
  16.     DBGrid1: TDBGrid;
  17.     procedure DBGrid1SelectEditor(Sender: TObject; Column: TColumn;
  18.       var Editor: TWinControl);
  19.   private
  20.     procedure DBCBDone(Sender: TObject);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.DBCBDone(Sender: TObject);
  33. begin
  34.   //
  35. end;
  36.  
  37. procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn;
  38.   var Editor: TWinControl);
  39. begin
  40.   TComboBox(Editor).OnEditingDone := @DBCBDone;
  41. end;
  42.  
  43. end.

Try to compile the simplified code above, do you still get the error?
FYI, you need to put a DBGrid and a ComboBox on the form.
« Last Edit: August 07, 2020, 02:07:11 pm by Handoko »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'
« Reply #2 on: August 07, 2020, 02:08:02 pm »
Why do you retype twice?
Code: Pascal  [Select][+][-]
  1.       with Editor as TCustomComboBox do begin
  2.         ...
  3.         Editor.OnEditingDone := @DBCBDone;  // should be enough
  4.       end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

patyit

  • New Member
  • *
  • Posts: 27
Re: TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'
« Reply #3 on: August 07, 2020, 02:16:27 pm »
Handoko :

The problem is not compiling time, running time is the problem.
I am using Lazarus 2.0.11 GTK2 Linux FPC 3.2.1, FPCUP Fixex 64Bit version.

Blaazen:

The Editor does not have the OnEditingDone property because it is TCustumComboBox.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'
« Reply #4 on: August 07, 2020, 02:21:35 pm »
Sorry, I didn't read your post carefully. I thought type casting error is a compile time issue.

Can you provide a demo so we can test it? Runtime error is harder to fix, it will be easier if we can have the runable source code to test. If you're not willing to publicize the source code you can write a simplified demo that showing the problem.

Create a new folder, copy and paste all the necessary files except: the binary (exe file), *.bak, lib and backup folders. Compress the folder and send the zip here.

wp

  • Hero Member
  • *****
  • Posts: 11856
Re: TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'
« Reply #5 on: August 07, 2020, 02:34:33 pm »
The OnSelectEditor event in a grid is normally used to tell the grid that the default editor, a TEdit-like editor, should be replaced by the component passed to the "Editor" parameter -- that's why it is a "var" argument.

You do not assign anything to "Editor", i.e. Editor remains what was selected by the grid. And this certainly is not a TCombobox.

You seem to need a picklist editor. Therefore you should assign Editor to the result of "EditorByStyle(cbsPickList)" which selects a built-in combobox (TPicklistCellEditor = class(TCustomCombobox -- see grids.pas):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn;
  2.   var Editor: TWinControl);
  3. begin
  4.   if Column.Visible and not Column.ReadOnly then begin
  5.     if Column.Field = QStatNstat then begin
  6.       Editor := EditorByStyle(cbsPickList);
  7.       with Editor as TPickListCellEditor do begin
  8.         Style := csDropDown;
  9.         Items.CommaText := FDataMod.GetParam('status');
  10.         DropDownCount := 15;
  11.         AutoComplete  := True;
  12.         TPickListCellEditor(Editor).OnEditingDone := @DBCBDone;  
  13.       end;
  14.     end;
  15.   end;
  16. end;

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'
« Reply #6 on: August 07, 2020, 02:57:11 pm »
@ The Editor does not have the OnEditingDone property because it is TCustumComboBox.
Sorry, it is even simplier:
Code: Pascal  [Select][+][-]
  1.       with Editor as TCustomComboBox do begin
  2.           ...
  3.         OnEditingDone := @DBCBDone;  // should be enough
  4.       end;

But your code yet miss two important things: assign Editor itself (parameter by reference) and bound rectangle.
Code should be like this:
Code: Pascal  [Select][+][-]
  1. Editor := DBCB;
  2.   with Editor as TCustomComboBox do begin
  3.     BoundsRect:=DBGrid1.CellRect(aCol, aRow);
  4.     Style := csDropDown;
  5.     Items.CommaText := FDataMod.GetParam('status');
  6.     DropDownCount := 15;
  7.     AutoComplete  := True;
  8.     OnEditingDone := @DBCBDone;
  9.     Visible:=True;  //*
  10.   end;  

*You should also manage visibility. Usually, grid-cell editor is invisible and become visible only on demand.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

patyit

  • New Member
  • *
  • Posts: 27
Re: TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'
« Reply #7 on: August 07, 2020, 03:46:11 pm »
Thanks to everyone for the help, the problem is solved !

@wp pointed out the TPickListCellEditor should be used instead of TCustumComboBox.
I have attached a small test project that demonstrates the correct code (at least which works correctly for this case).

Yours sincerely.  :D
« Last Edit: August 07, 2020, 03:47:55 pm by patyit »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: [SOLVED] TCustomComboBox to TComboBox Invalid type cast 'RunError(219)'
« Reply #8 on: August 07, 2020, 04:06:10 pm »
Thank you for providing the problem-solved demo, so others can learn from the code.

 

TinyPortal © 2005-2018