Solved

need help with component....

Posted on 2001-09-05
8
189 Views
Last Modified: 2010-04-06
hi,

this a component i created
but there are some errors in it.
can you please look at it and
give your input on it.

TIA, ruben

the code is :

*** Start of Code ***

unit ExtMemo;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TExtMemo = class(TCustomMemo)
private
  { Private declarations }
  FManualInvoke : Boolean;
  FPopup: Boolean; (* changes *)
  FAutoComplete : Boolean;
  FMinLength : Integer;
  FOptions : TStringList;
  FOptionsListBox : TListBox;
protected
  { Protected declarations }
  function CurrentWord:String;
  procedure Change; override;
  procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  procedure KeyPress(var Key: Char); override;
  procedure SetParent(AParent: TWinControl); override;
public
  { Public declarations }
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
  property Options : TStringList read FOptions write FOptions;
published
  { Published declarations }
  property AutoComplete : Boolean read FAutoComplete write FAutoComplete;
  property AutoCompLength : Integer read FMinLength write FMinLength;
  //property's of TMemo
  property Align;
  property Alignment;
  property Anchors;
  property BiDiMode;
  property BorderStyle;
  property Color;
  property Constraints;
  property Ctl3D;
  property DragCursor;
  property DragKind;
  property DragMode;
  property Enabled;
  property Font;
  property HideSelection;
  property ImeMode;
  property ImeName;
  property Lines;
  property MaxLength;
  property OEMConvert;
  property ParentBiDiMode;
  property ParentColor;
  property ParentCtl3D;
  property ParentFont;
  property ParentShowHint;
  property PopupMenu;
  property ReadOnly;
  property ScrollBars;
  property ShowHint;
  property TabOrder;
  property TabStop;
  property Visible;
  property WantReturns;
  property WantTabs;
  property WordWrap;
  property OnChange;
  property OnClick;
  property OnContextPopup;
  property OnDblClick;
  property OnDragDrop;
  property OnDragOver;
  property OnEndDock;
  property OnEndDrag;
  property OnEnter;
  property OnExit;
  property OnKeyDown;
  property OnKeyPress;
  property OnKeyUp;
  property OnMouseDown;
  property OnMouseMove;
  property OnMouseUp;
  property OnStartDock;
  property OnStartDrag;
end;

procedure Register;

implementation

function TExtMemo.CurrentWord: String;
var
  FPos : Integer;
begin
  FPos := CaretPos.x;
  while (not(Lines[CaretPos.y][FPos] in [' ','.',','])) and (FPos > 0) do Dec(FPos);
  Inc(FPos);
  Result := Copy(Lines[CaretPos.y], FPos, CaretPos.x - FPos + 1); (* changes *)
end;

procedure TExtMemo.SetParent(AParent: TWinControl); (* changes *)
begin
  inherited SetParent(AParent);
  FOptionsListBox.Parent := AParent;
end;

procedure TExtMemo.Change; (* changes *)
var
  i     : Integer;
  {FPos  : Integer; //Position of Char    }
  FWord : String;  //Current Word
  FFirstVisible : LongInt;
  TextSize : TSize;
  FCanvas: TCanvas;
begin
  inherited;
  FOptionsListBox.Visible := False;
  FOptionsListBox.Clear;
  if (CaretPos.x > 0) and not (Lines[CaretPos.y][CaretPos.x] in [' ','.',',']) then
  begin
    FWord := CurrentWord;
    if (FAutoComplete and (Length(FWord) >= FMinLength)) or FPopup then
    begin
     for i := 0 to FOptions.Count - 1 do
       if Length(FOptions.Strings[i]) > Length(FWord) then
         if AnsiCompareText(Copy(FOptions.Strings[i], 1, Length(FWord)), FWord) = 0 then
           FOptionsListBox.Items.Add(FOptions.Strings[i]);
    end;
  end;
  if FPopup then FPopup := False;
  if FOptionsListBox.Items.Count > 0 then
  begin
    FOptionsListBox.Visible := True;
    FOptionsListBox.ItemIndex := 0;
    FFirstVisible := SendMessage(Self.Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
    FCanvas := TCanvas.Create;
    try
      FCanvas.Handle := GetDC(Self.Handle);
      TextSize.cx := 0;
      if (CaretPos.x - Length(FWord)) > 0
       then TextSize := FCanvas.TextExtent(Copy(Lines[CaretPos.y], 1, CaretPos.x - Length(FWord)))
       else TextSize.cy := FCanvas.TextHeight('X');
    finally
     FCanvas.Free;
    end;
    if (TextSize.cx + FOptionsListBox.Width) > Self.ClientWidth
     then FOptionsListBox.Left := (Self.ClientWidth - FOptionsListBox.Width) -2
     else FOptionsListBox.Left := TextSize.cx + 2;
    if ((TextSize.cy * CaretPos.y) + FOptionsListBox.Height) > Self.ClientHeight
      then FOptionsListBox.Top := (TextSize.cy * (CaretPos.y - FFirstVisible)) - FOptionsListBox.Height
      else FOptionsListBox.Top := (TextSize.cy * (CaretPos.y - FFirstVisible + 1)) + 2; {(CaretPos.y - FFirstVisible + 1) * TextSize.cy;}
  end;
end;

procedure TExtMemo.KeyDown(var Key: Word; Shift: TShiftState);
begin
  inherited;
  if FOptionsListBox.Visible and (Key in [VK_DOWN, VK_UP, VK_ESCAPE]) then
  begin
    case Key of
      VK_DOWN   : if FOptionsListBox.ItemIndex < FOptionsListBox.Items.Count then
                    FOptionsListBox.ItemIndex := FOptionsListBox.ItemIndex + 1;
      VK_UP     : if FOptionsListBox.ItemIndex > 0 then
                    FOptionsListBox.ItemIndex := FOptionsListBox.ItemIndex - 1;
      VK_ESCAPE : FOptionsListBox.Visible := False;
    end;
    Key := 0;
  end else
    if (ssCtrl in Shift) and (Key = VK_SPACE) then
    begin
      FManualInvoke := True;
      FPopup := True; (* changes *)
      Key := 0;
      Change; (* changes *)
    end;
end;

procedure TExtMemo.KeyPress(var Key: Char);
var
  FWord : String;  //Current Word
begin
  inherited;
  if FOptionsListBox.Visible and (Key = #13) then
  begin
    if (CaretPos.x > 0) and (Lines[CaretPos.y][CaretPos.x] <> ' ') then
      FWord := CurrentWord;
    SelStart := SelStart - Length(FWord);
    SelLength := Length(FWord);
    SelText := FOptionsListBox.Items[FOptionsListBox.itemindex];
    // !!! JACCO: This was intended to keep capitalisation as user typed
    //SelText := SelText + Copy(FOptionsListBox.Items[FOptionsListBox.itemindex], Length(SelText) + 1,255);
    Key := #0;
  end;
  if FManualInvoke then
  begin
    Key := #0;
    FManualInvoke := False; (* changes *)
  end;
end;

constructor TExtMemo.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMinLength := 2;
  FAutoComplete := True;
  FOptions := TStringList.Create;
  FOptionsListBox := TListBox.Create(Self); (* changes *)
//  FOptionsListBox.Parent := Self;
  FOptionsListBox.Height := 75;
  FOptionsListBox.Width := 100;
  FOptionsListBox.Visible := False;
end;

destructor TExtMemo.Destroy;
begin
  FOptions.Free;
  FOptionsListBox.Free;
  inherited Destroy;
end;

procedure Register;
begin
  RegisterComponents('SoSsA', [TExtMemo]);
end;

end.


*** End of Code ***
0
Comment
Question by:rtieland
8 Comments
 
LVL 1

Expert Comment

by:Dennis9
ID: 6458445
Hi.
Is the error in the source??? or when component is installed?

The only thing i could find was this:
   // !!! JACCO: This was intended to keep capitalisation as user typed
   //SelText := SelText + Copy(FOptionsListBox.Items[FOptionsListBox.itemindex], Length(SelText) +
1,255);

//If it did not have to be used it should be:
   // !!! JACCO: This was intended to keep capitalisation as user typed
 {SelText := SelText + Copy(FOptionsListBox.Items[FOptionsListBox.itemindex], Length(SelText) +
1,255);}

Dennis
0
 
LVL 10

Expert Comment

by:Jacco
ID: 6458462
Hi Ruben,

What's wrong with it?

Regards Jacco
0
 
LVL 1

Author Comment

by:rtieland
ID: 6458493
if i register the component in  the component pallet and then create a new application and drop it on the form it gets an error about can't read address 00000
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6459689
you should check, if your foptionlistpox is assigned generally.

i guess the error comes from

procedure TExtMemo.SetParent(AParent: TWinControl); (* changes *)
begin
 inherited SetParent(AParent);
 FOptionsListBox.Parent := AParent;
end;

change this code to

procedure TExtMemo.SetParent(AParent: TWinControl); (* changes *)
begin
 inherited SetParent(AParent);
 If Assigned(FOptionsListBox) Then
   FOptionsListBox.Parent := AParent;
end;

meikl ;-)

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:rtieland
ID: 6459797
this is the exact error
"access violation at address 000000000. read of address 000000000."
kretzschmar : i try'd your comment, doesn't help.
could you guy's try it yourself.

TIA,
ruben
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6459812
can you show your overridden constructor?
0
 
LVL 8

Accepted Solution

by:
TOndrej earned 100 total points
ID: 6460667
I think the problem is in the constructor and SetParent methods.
First, you create the listbox and pass it Self as Owner. This is not necessary if you're managing its lifetime; it's cleaner to pass nil in this case.

Another actual problem is that you don't set the Parent and then you adjust Height, Width and Visible properties. OTTOMH, (some of) these operations require a valid Parent.

From the code it's not clear what you're trying to do...
Do you want the listbox to be child of the memo?
It looks as if you wanted it to be a child of the memo's parent (a form, for example). In that case, you may want to check for csDesigning in ComponentState and avoid creating the listbox at designtime.

In any case, a better place to create/destroy the listbox would be CreateWnd/DestroyWnd (after/before inherited call, respectively). In the constructor your memo doesn't yet have a handle; in the destructor the handle has already been released.

HTH
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6911824
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20121474.html
http://www.experts-exchange.com/questions/Q.20168120.html
http://www.experts-exchange.com/questions/Q.20179033.html
http://www.experts-exchange.com/questions/Q.20229567.html
http://www.experts-exchange.com/questions/Q.20252042.html
http://www.experts-exchange.com/questions/Q.20255732.html




PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now