[SOLVED] TCustomComboBox to TComboBox Invalid type cast 'RunError(219)' lazarus / 16-Databases ---------------------------------------------------------------------- patyit · 2020-08-07 11:52 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn;  var Editor: TWinControl);begin  if Column.Visible and not Column.ReadOnly then begin    if Column.Field = QStatNstat then begin      with Editor as TCustomComboBox do begin        Style := csDropDown;        Items.CommaText := FDataMod.GetParam('status');        DropDownCount := 15;        AutoComplete  := True;        TComboBox(Editor).OnEditingDone := @DBCBDone;  // <-- Type Cast error between TCustomComboBox and TComboBox      end;    end;  end;end; procedure TForm1.DBCBDone(Sender: TObject);begin  if QStat.State in dsEditModes then    QStat.Post;  EBar.Clear;  EBar.SetFocus;end; ---------------------------------------------------------------------- Handoko · 2020-08-07 11:59 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, StdCtrls, DBGrids; type   { TForm1 }   TForm1 = class(TForm)    ComboBox1: TComboBox;    DBGrid1: TDBGrid;    procedure DBGrid1SelectEditor(Sender: TObject; Column: TColumn;      var Editor: TWinControl);  private    procedure DBCBDone(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.DBCBDone(Sender: TObject);begin  //end; procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn;  var Editor: TWinControl);begin  TComboBox(Editor).OnEditingDone := @DBCBDone;end; 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. ---------------------------------------------------------------------- Blaazen · 2020-08-07 12:08 Why do you retype twice? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       with Editor as TCustomComboBox do begin        ...        Editor.OnEditingDone := @DBCBDone;  // should be enough      end; ---------------------------------------------------------------------- patyit · 2020-08-07 12:16 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 · 2020-08-07 12:21 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 · 2020-08-07 12:34 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn;  var Editor: TWinControl);begin  if Column.Visible and not Column.ReadOnly then begin    if Column.Field = QStatNstat then begin      Editor := EditorByStyle(cbsPickList);      with Editor as TPickListCellEditor do begin        Style := csDropDown;        Items.CommaText := FDataMod.GetParam('status');        DropDownCount := 15;        AutoComplete  := True;        TPickListCellEditor(Editor).OnEditingDone := @DBCBDone;        end;  end;  end;end; ---------------------------------------------------------------------- Blaazen · 2020-08-07 12:57 @ The Editor does not have the OnEditingDone property because it is TCustumComboBox. Sorry, it is even simplier: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       with Editor as TCustomComboBox do begin          ...        OnEditingDone := @DBCBDone;  // should be enough      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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Editor := DBCB;  with Editor as TCustomComboBox do begin    BoundsRect:=DBGrid1.CellRect(aCol, aRow);    Style := csDropDown;    Items.CommaText := FDataMod.GetParam('status');    DropDownCount := 15;    AutoComplete  := True;    OnEditingDone := @DBCBDone;    Visible:=True;  //*  end;   *You should also manage visibility. Usually, grid-cell editor is invisible and become visible only on demand. ---------------------------------------------------------------------- patyit · 2020-08-07 13:46 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. ---------------------------------------------------------------------- Handoko · 2020-08-07 14:06 Thank you for providing the problem-solved demo, so others can learn from the code.