A new component derived from TListBox ...

I'm trying to write a new component from TListBox that autocompletes by
typing the item's chars ...

My problem is : if the item that I want to select is nou visible ( out of
the canvas area ) when selected, is the next from the last visible item in
the canvas. What can I do to have the item selected let's say in the middle
of the canvas - better totaly visible ( when it is the first he is on the
top of canvas, when is the last he is on the bottom ) ?

This is my code, please corect me is you find mode optimisations !

MyListBox.h

file://---------------------------------------------------------------------
------

#ifndef MyListBoxH
#define MyListBoxH
file://---------------------------------------------------------------------
------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
file://---------------------------------------------------------------------
------
class PACKAGE TMyListBox : public TCustomListBox
{
private:
//   TKeyPressEvent FOnKeyPress;
   AnsiString MyString;
   TDateTime MyDateTime;
protected:
//    __property TKeyPressEvent OnKeyPress = {read=FOnKeyPress,
write=FOnKeyPress};
   DYNAMIC void __fastcall KeyPress(char &Key);
public:
    __fastcall TMyListBox(TComponent* Owner);
__published:
    __property Style;
    __property AutoComplete;
    __property Align;
    __property Anchors;
    __property BevelEdges;
    __property BevelInner;
    __property BevelKind = {default=bkNone};
    __property BevelOuter;
    __property BiDiMode;
    __property BorderStyle;
    __property Color;
    __property Columns;
    __property Constraints;
    __property Ctl3D;
    __property DragCursor;
    __property DragKind;
    __property DragMode;
    __property Enabled;
    __property ExtendedSelect;
    __property Font;
    __property ImeMode;
    __property ImeName;
    __property IntegralHeight;
    __property ItemHeight;
    __property Items;
    __property MultiSelect;
    __property ParentBiDiMode;
    __property ParentColor;
    __property ParentCtl3D;
    __property ParentFont;
    __property ParentShowHint;
    __property PopupMenu;
    __property ScrollWidth;
    __property ShowHint;
    __property Sorted;
    __property TabOrder;
    __property TabStop;
    __property TabWidth;
    __property Visible;
    __property OnClick;
    __property OnContextPopup;
    __property OnData;
    __property OnDataFind;
    __property OnDataObject;
    __property OnDblClick;
    __property OnDragDrop;
    __property OnDragOver;
    __property OnDrawItem;
    __property OnEndDock;
    __property OnEndDrag;
    __property OnEnter;
    __property OnExit;
    __property OnKeyDown;
    __property OnKeyPress;
    __property OnKeyUp;
    __property OnMeasureItem;
    __property OnMouseDown;
    __property OnMouseMove;
    __property OnMouseUp;
    __property OnStartDock;
    __property OnStartDrag;

Quote
};

file://---------------------------------------------------------------------
------
#endif

MyLitBox.cpp

file://---------------------------------------------------------------------
------

#include <vcl.h>

#pragma hdrstop

#include "MyListBox.h"
#pragma package(smart_init)
file://---------------------------------------------------------------------
------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TMyListBox *)
{
        new TMyListBox(NULL);

Quote
}

file://---------------------------------------------------------------------
------
__fastcall TMyListBox::TMyListBox(TComponent* Owner)
        : TCustomListBox(Owner)
{
  Sorted = true;
  MultiSelect = true;
Quote
}

file://---------------------------------------------------------------------
------
namespace Mylistbox
{
        void __fastcall PACKAGE Register()
        {
                 TComponentClass classes[1] = {__classid(TMyListBox)};
                 RegisterComponents("Samples", classes, 0);
        }
Quote
}

file://---------------------------------------------------------------------
------
void __fastcall TMyListBox::KeyPress(char &Key)
{
  TDateTime MyBufDateTime = MyBufDateTime.CurrentDateTime();
  int i = -1,k = -1;

  if ( ( MyBufDateTime.Val - MyDateTime.Val ) > 0.000011574 ) MyString = "";
  MyDateTime = MyBufDateTime;

  AnsiString MyBufString = MyString;

  switch ( Key )
   { case 8 : MyString = ""; break;
     case 13 : break;
     default : { MyBufString += Key;

                 for ( int j = 0; j < Items->Count && i < 0; j++ )
                   { i =
Items->Strings[j].SubString(0,MyBufString.Length()).AnsiCompareIC(MyBufStrin
g);
                     if ( i == 0 ) k = j;
                   }

                 if ( k == -1 ) MessageBeep(MB_ICONASTERISK);
                 else { ClearSelection(); Selected[k] = true; MyString =
MyBufString; }
               }
   }

  Key = 0;

Quote
}