📄 jvdbcombobox.pas
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvDBComb.PAS, released on 2002-07-04.
The Initial Developers of the Original Code are: Fedor Koshevnikov, Igor Pavluk and Serge Korolev
Copyright (c) 1997, 1998 Fedor Koshevnikov, Igor Pavluk and Serge Korolev
Copyright (c) 2001,2002 SGB Software
All Rights Reserved.
Contributor(s):
Polaris Software
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvDBCombobox.pas,v 1.20 2005/02/17 10:20:20 marquardt Exp $
unit JvDBCombobox;
{$I jvcl.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
{$IFDEF VCL}
Windows, Messages,
{$ENDIF VCL}
{$IFDEF VisualCLX}
QWindows,
{$ENDIF VisualCLX}
Classes, Graphics, Controls, StdCtrls, DB, DBCtrls,
JvExStdCtrls;
type
TJvCustomDBComboBox = class(TJvExCustomComboBox)
private
FDataLink: TFieldDataLink;
FPaintControl: TPaintControl;
FBeepOnError: Boolean;
procedure DataChange(Sender: TObject);
procedure EditingChange(Sender: TObject);
function GetDataField: string;
function GetDataSource: TDataSource;
function GetField: TField;
function GetReadOnly: Boolean;
procedure SetDataField(const Value: string);
procedure SetDataSource(Value: TDataSource);
procedure SetEditReadOnly;
{$IFNDEF COMPILER6_UP}
procedure SetItems(const Value: TStrings);
{$ENDIF !COMPILER6_UP}
procedure SetReadOnly(Value: Boolean);
procedure UpdateData(Sender: TObject);
function GetComboText: string; virtual;
procedure SetComboText(const Value: string); virtual;
procedure CMGetDataLink(var Msg: TMessage); message CM_GETDATALINK;
procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;
protected
procedure DoExit; override;
procedure Change; override;
procedure Click; override;
procedure ComboWndProc(var Msg: TMessage; ComboWnd: HWND;
ComboProc: Pointer); override;
procedure CreateWnd; override;
procedure DropDown; override;
function GetPaintText: string; virtual;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure Loaded; override;
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure SetStyle(Value: TComboBoxStyle);override;
{$IFDEF COMPILER6_UP}
procedure SetItems(const Value: TStrings); override;
{$ENDIF COMPILER6_UP}
procedure WndProc(var Msg: TMessage); override;
property BeepOnError: Boolean read FBeepOnError write FBeepOnError default False;
property ComboText: string read GetComboText write SetComboText;
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly default False;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function ExecuteAction(Action: TBasicAction): Boolean; override;
function UpdateAction(Action: TBasicAction): Boolean; override;
function UseRightToLeftAlignment: Boolean; override;
property Field: TField read GetField;
property Items write SetItems;
property Text;
end;
TJvDBComboBox = class(TJvCustomDBComboBox)
private
FValues: TStringList;
FEnableValues: Boolean;
procedure SetEnableValues(Value: Boolean);
function GetValues: TStrings;
procedure SetValues(Value: TStrings);
procedure ValuesChanged(Sender: TObject);
protected
procedure SetStyle(Value: TComboBoxStyle); override;
function GetComboText: string; override;
function GetPaintText: string; override;
procedure SetComboText(const Value: string); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Align; // Polaris
property Style { must be published before Items }
default csDropDownList; // Polaris
property BeepOnError;
property Color;
property DataField;
property DataSource;
property DragMode;
property DragCursor;
property DropDownCount;
property Enabled;
property EnableValues: Boolean read FEnableValues write SetEnableValues default True;
property Font;
property Anchors;
property BiDiMode;
property Constraints;
property DragKind;
property ParentBiDiMode;
property ImeMode;
property ImeName;
property ItemHeight;
property Items;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property Sorted;
property TabOrder;
property TabStop;
property Values: TStrings read GetValues write SetValues;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDrawItem;
property OnDropDown;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMeasureItem;
property OnStartDrag;
property OnContextPopup;
property OnEndDock;
property OnStartDock;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvDBCombobox.pas,v $';
Revision: '$Revision: 1.20 $';
Date: '$Date: 2005/02/17 10:20:20 $';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
{$IFDEF COMPILER6_UP}
VDBConsts,
{$ELSE}
DBConsts,
{$ENDIF COMPILER6_UP}
SysUtils,
JvConsts, JvDBUtils;
//=== { TJvCustomDBComboBox } ================================================
constructor TJvCustomDBComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnEditingChange := EditingChange;
FPaintControl := TPaintControl.Create(Self, 'COMBOBOX');
FBeepOnError := False;
end;
destructor TJvCustomDBComboBox.Destroy;
begin
FPaintControl.Free;
FDataLink.OnDataChange := nil;
FDataLink.OnUpdateData := nil;
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
procedure TJvCustomDBComboBox.Loaded;
begin
inherited Loaded;
if csDesigning in ComponentState then
DataChange(Self);
end;
procedure TJvCustomDBComboBox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = DataSource) then
DataSource := nil;
end;
procedure TJvCustomDBComboBox.CreateWnd;
begin
inherited CreateWnd;
SetEditReadOnly;
end;
procedure TJvCustomDBComboBox.DataChange(Sender: TObject);
begin
if DroppedDown then
Exit;
if FDataLink.Field <> nil then
ComboText := FDataLink.Field.Text
else
if csDesigning in ComponentState then
ComboText := Name
else
ComboText := '';
end;
procedure TJvCustomDBComboBox.UpdateData(Sender: TObject);
begin
FDataLink.Field.Text := ComboText;
end;
procedure TJvCustomDBComboBox.SetComboText(const Value: string);
var
I: Integer;
Redraw: Boolean;
begin
if Value <> ComboText then
begin
if Style <> csDropDown then
begin
Redraw := (Style <> csSimple) and HandleAllocated;
if Redraw then
SendMessage(Handle, WM_SETREDRAW, 0, 0);
try
if Value = '' then
I := -1
else
I := Items.IndexOf(Value);
ItemIndex := I;
finally
if Redraw then
begin
SendMessage(Handle, WM_SETREDRAW, 1, 0);
Invalidate;
end;
end;
if I >= 0 then
Exit;
end;
if Style in [csDropDown, csSimple] then
Text := Value;
end;
end;
function TJvCustomDBComboBox.GetComboText: string;
var
I: Integer;
begin
if Style in [csDropDown, csSimple] then
Result := Text
else
begin
I := ItemIndex;
if I < 0 then
Result := ''
else
Result := Items[I];
end;
end;
procedure TJvCustomDBComboBox.Change;
begin
FDataLink.Edit;
inherited Change;
FDataLink.Modified;
end;
procedure TJvCustomDBComboBox.Click;
begin
FDataLink.Edit;
inherited Click;
FDataLink.Modified;
end;
procedure TJvCustomDBComboBox.DropDown;
begin
inherited DropDown;
end;
function TJvCustomDBComboBox.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TJvCustomDBComboBox.SetDataSource(Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Value <> nil then
Value.FreeNotification(Self);
end;
function TJvCustomDBComboBox.GetDataField: string;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -