unit Unit5;

interface

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

type
  TFrmSetVar = class(TForm)
    lblChangeValue: TLabel;
    Label1: TLabel;
    txtNewValue: TEdit;
    lblVarNAme: TLabel;
    Button1: TButton;
    Button2: TButton;
    lblVarIndex: TLabel;
    lbl_type: TLabel;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    function IsANum(str: String) : Boolean;
    procedure txtNewValueKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmSetVar: TFrmSetVar;

implementation

uses Unit1;

{$R *.dfm}

function TFrmSetVar.IsANum(str: String) : Boolean;
var
a: Integer;
begin
  if Length(str) > 0 then begin
    Result := true;
    for a:=1 to Length(str) do begin
      if (
      (Copy(str, a, 1) <> '0') and (Copy(str, a, 1) <> '1') and
      (Copy(str, a, 1) <> '2') and (Copy(str, a, 1) <> '3') and
      (Copy(str, a, 1) <> '4') and (Copy(str, a, 1) <> '5') and
      (Copy(str, a, 1) <> '6') and (Copy(str, a, 1) <> '7') and
      (Copy(str, a, 1) <> '8') and (Copy(str, a, 1) <> '9') and
      (Copy(str, a, 1) <> '.')
      ) then Result := false;
    end;
  end else begin
    Result := false;
  end;
end;


procedure TFrmSetVar.Button2Click(Sender: TObject);
begin
  self.Close;
end;

procedure TFrmSetVar.Button1Click(Sender: TObject);
var
suffix, source: String;
begin

  // segna il tipo di variabile
  // g, l, u
  suffix := LowerCase(lbl_type.Caption);

  source := lblVarName.Caption;

  if (UpperCase(txtNewValue.Text) = 'TRUE') or (UpperCase(txtNewValue.Text)='FALSE') then begin
    // dobbiamo mandare un booleano
    if UpperCase(txtNewValue.Text)='TRUE' then begin
      MainForm.SockSend('set'+suffix+'boolean '+source+' 1');
    end else begin
      MainForm.SockSend('set'+suffix+'boolean '+source+' 0');
    end;
  end else if IsANum(txtNewValue.Text) then begin
    // dobbiamo mandare un numero
    MainForm.SockSend('set'+suffix+'number '+source+' '+txtNewValue.Text);
  end else begin
    // manda una stringa
    MainForm.SockSend('set'+suffix+'string '+source+' '+txtNewValue.Text);
  end;

  self.close;
end;

procedure TFrmSetVar.txtNewValueKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = 13 then Button1Click(Sender);
end;

end.
