⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customconnectform.pas

📁 Crlab公司用来连接MySQL数据库的控件
💻 PAS
字号:
unit CustomConnectForm;

interface

uses
{$IFNDEF LINUX}
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Grids, DBGrids, StdCtrls, ToolWin, ComCtrls, Buttons, Mask, MyDACVCL, 
{$ELSE}
  SysUtils, Types, Classes, QGraphics, QControls, QForms, QDialogs, QStdCtrls,
  QDBCtrls, QComCtrls, QExtCtrls, QGrids, QDBGrids, MyDacClx, QButtons, QMask,
{$ENDIF}
  MyClasses, MyAccess, MyCall;

type
  TfmCustomConnect = class(TForm)
    Panel: TPanel;
    lbUsername: TLabel;
    lbPassword: TLabel;
    lbServer: TLabel;
    Bevel1: TBevel;
    edUsername: TEdit;
    edServer: TComboBox;
    lbDatabase: TLabel;
    lbPort: TLabel;
    edPort: TEdit;
    btConnect: TBitBtn;
    btCancel: TBitBtn;
    edDatabase: TEdit;
    edPassword: TMaskEdit;
    procedure btConnectClick(Sender: TObject);
  private
    FConnectDialog: TMyConnectDialog;
    FRetries:integer;
    FRetry: boolean;

    procedure SetConnectDialog(Value: TMyConnectDialog);

  protected
    procedure DoInit; virtual;
    procedure DoConnect; virtual;

  public

  published
    property ConnectDialog: TMyConnectDialog read FConnectDialog write SetConnectDialog;

  end;

var
  fmCustomConnect: TfmCustomConnect;

implementation

{$IFDEF CLR}
{$R *.nfm}
{$ENDIF}
{$IFDEF WIN32}
{$R *.dfm}
{$ENDIF}
{$IFDEF LINUX}
{$R CustomConnectForm.xfm}
{$ENDIF}

procedure TfmCustomConnect.DoInit;
begin
  FRetry := False;
  FRetries := FConnectDialog.Retries;
  Caption := FConnectDialog.Caption;

  lbUsername.Caption := FConnectDialog.UsernameLabel;
  lbPassword.Caption := FConnectDialog.PasswordLabel;
  lbServer.Caption := FConnectDialog.ServerLabel;
  btConnect.Caption := FConnectDialog.ConnectButton;
  btCancel.Caption := FConnectDialog.CancelButton;

  FConnectDialog.GetServerList(edServer.Items);
  edUsername.Text := FConnectDialog.Connection.Username;
  edPassword.Text := FConnectDialog.Connection.Password;
  edServer.Text := FConnectDialog.Connection.Server;

  if (edUsername.Text <> '') and (edPassword.Text = '') then
    ActiveControl := edPassword;
end;

procedure TfmCustomConnect.DoConnect;
begin
  FConnectDialog.Connection.Password := edPassword.Text;
  FConnectDialog.Connection.Server := edServer.Text;
  FConnectDialog.Connection.UserName := edUsername.Text;
  (FConnectDialog.Connection as TMyConnection).Port := StrToInt(edPort.Text);
  FConnectDialog.Connection.Database := edDatabase.Text;
  try
    FConnectDialog.Connection.PerformConnect(FRetry);
    ModalResult := mrOk;
  except
    on E: EMyError do begin
      Dec(FRetries);
      FRetry := True;
      if FRetries = 0 then
        ModalResult := mrCancel;
      if E.ErrorCode = CR_CONN_HOST_ERROR then
        ActiveControl := edServer
      else
      if E.ErrorCode = ER_ACCESS_DENIED_ERROR then
        if ActiveControl <> edUsername then
          ActiveControl := edPassword;
      raise;
    end
    else
      raise;
  end;
end;

procedure TfmCustomConnect.SetConnectDialog(Value: TMyConnectDialog);
begin
  FConnectDialog:= Value;
  DoInit;
end;

procedure TfmCustomConnect.btConnectClick(Sender: TObject);
begin
  DoConnect;
end;

initialization
  if GetClass('TfmMyConnect') = nil then begin
    {$IFDEF VER6P}
    Classes.StartClassGroup(TfmMyConnect);
    {$ENDIF}
    Classes.RegisterClass(TfmCustomConnect);
    {$IFDEF VER6P}
    ActivateClassGroup(TfmMyConnect);
    {$ENDIF}
  end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -