Search code examples
delphifindcontrol

Why do I get an access violation when I access a TValueListEditor found with FindControl?


I have dynamically created TValueListEditor VCL component on a TForm. The code is located in nested procedure of one of the main form's methods. I have set:

ValueListEditor.KeyOptions := [keyEdit, keyAdd, keyUnique];

It looks like this:

TMainForm.Method();

Method has a nested procedure that contains code that creates the components mentioned above.

Then, I have helper function:

function GetMenuListData(XMLNode: TXMLNode; const XNMLDoc: string = '') : string;

In this helper I use this code to load an XML file and then retrieve its nodes and insert them into ValueListEditor.

XMLDoc := TXMLDocument.Create(Self);
XMLDoc.ParseOptions := [poPreserveWhiteSpace];
try
  XMLDoc.LoadFromFile(XNMLDoc);
  try
    Control := FindControl(FindWindow('TForm',PChar('(' + ExtractFileExt(Form1.Edit1.Text) + ')')));
    if Control <> nil then
    begin
      TValuelistEditor(Control).Keys[TValuelistEditor(Control).RowCount-1] := XMLDoc.DocumentElement.NodeName;
      if XMLDoc.DocumentElement.ChildNodes.First.AttributeNodes.Count > 0 then
        TValuelistEditor(Control).Values[TValuelistEditor(Control).Keys[TValuelistEditor(Control).RowCount-1]] := String(XMLDoc.DocumentElement.Attributes['id'])
      else
        TValuelistEditor(Control).Values[TValuelistEditor(Control).Keys[TValuelistEditor(Control).RowCount-1]] := '<Empty>';
    end else begin
      MessageBeep(0);
      FlashWindow(Application.Handle, True);
      ShowMessagePos('...');
    end;
  finally
    XMLDoc.Active := False; Result := 'Forced ' + Form1.RAWInputBtn.Caption + ' in ' + DateTimeToStr(Now);
  end;
except
  on E : EXMLDocError do
  begin
    Result := 'Forced ' + Form1.RAWInputBtn.Caption + ' in ' + DateTimeToStr(Now);
  end;
end;

The problem is that I get access violations every time code goes into the line:

TValuelistEditor(Control).Keys[TValuelistEditor(Control).RowCount-1] := XMLDoc.DocumentElement.NodeName;

I have tried various typecasts, values, parameters .. nothing does the trick.

What is my mistake?

I'm using Delphi XE.


Solution

  • As Ken commented your problem is, instead of finding the value list editor, you are finding your form and then typecasting it to a value list editor, hence the AV.

    First, you're passing 'TForm' as 'lpClassName' to FindWindow. Assuming 'TForm' is the class name of your form, it will of course find the form - not a child window on it. Second, you cannot use FindWindow to find a child window, see its documentation, it searches top-level windows.

    If you had tested the return of FindControl, the code raising the AV would never run:

      if (Control <> nil) and (Control is TValueListEditor) then
    


    You can use FindWindowEx to search in child windows, if you don't know the handle of your form find it first as you've done already:

    FormHandle := FindWindow('TForm',PChar('(' + ExtractFileExt(Form1.Edit1.Text) + ')'));
    if FormHandle <> 0 then
    begin
      Control := FindControl(FindWindowEx(FormHandle, 0, 'TValueListEditor', nil));
    

    or better yet, test the return of FindWindowEx first to avoid passing '0' to FindControl:

    ValueListEditorHandle := FindWindowEx(FormHandle, 0, 'TValueListEditor', nil);
    if Win32Check(ValueListEditorHandle <> 0) then
    begin
      Control := FindControl(ValueListEditorHandle);
      if Assigned(Control) then
      begin
        ...