質問

これは、DevExpressグリッドのフィルターからfiltertype演算子を取得するために使用するコードスニペットです。 OperatorKindToStrは、フィルターからoperatorkindを文字列として抽出し、xmlファイルに格納するために使用されます。 StrToOperatorKindを使用して、xmlから文字列を変換し直し、フィルターにoperatorkindを設定します。

const
  CUSTFILTER_FILTERITEM     = 'FilterItem';

function OperatorKindToStr(const aOperatorKind: TcxFilterOperatorKind): string;
begin
  Result := 'foEqual';
  case aOperatorKind of
    foEqual:        Result := 'foEqual';
    foNotEqual:     Result := 'foNotEqual';
    foLess:         Result := 'foLess';
    foLessEqual:    Result := 'foLessEqual';

  // Plus a boring list of other constants
end;

function StrToOperatorKind(const aOpKindStr: string): TcxFilterOperatorKind;
begin
  Result := foEqual;
  if aOpKindStr       = 'foNotEqual' then
    Result := foNotEqual
  else if aOpKindStr  = 'foLess' then
    Result := foLess
  else if aOpKindStr  = 'foLessEqual' then
    Result := foLessEqual
  else if aOpKindStr  = 'foGreater' then
    Result := foGreater
  else if aOpKindStr  = 'foGreaterEqual' then
    Result := foGreaterEqual

  // Plus a boring list of other if-else
end;

procedure UseStrToOperatorKind(const aFilterItem: IXmlDomElement);
begin
  if aFilterItem.nodeName = CUSTFILTER_FILTERITEM then
  begin                              // It is an FilterItem
    vStr := VarToStr(aFilterItem.getAttribute(CUSTFILTER_COLPROP));  // Get the columnname
    vOperatorKind := StrToOperatorKind(aFilterItem.getAttribute(CUSTFILTER_ITEMOPERATOR));
end;

procedure UseOperatorKindToStr(const aFilterItem: TcxCustomFilterCriteriaItem);
var
  vStr: String;
begin
  if Supports(TcxFilterCriteriaItem(aFilterItem).ItemLink, TcxGridColumn, GridCol) then
    vStr := OperatorKindToStr(TcxFilterCriteriaItem(aFilterItem).OperatorKind);
end;

どうやら、StrToOperatorKindとOperatorKindToStrを少しスマートにしたい。 VCL TypeInfoでGetEnumPropメソッドを試しましたが、機能しません。 それでは、TcxFilterOperatorKindプロパティをaFilterItem変数から文字列に抽出し、TcxFilterOperatorKindに戻すにはどうすればよいですか?

役に立ちましたか?

解決

Masonが指摘したように、 GetEnumName GetEnumValue のデュエットを使用します。

そして、あなたの機能はもっとシンプルになるはずです:

function OperatorKindToStr(const aOperatorKind: TcxFilterOperatorKind): string;
begin
  Result := GetEnumName(TypeInfo(TcxFilterOperatorKind), Ord(aOperatorKind));
end;

function StrToOperatorKind(const aOpKindStr: string): TcxFilterOperatorKind;
begin
  Result := TcxFilterOperatorKind(GetEnumValue(TypeInfo(TcxFilterOperatorKind), aOpKindStr));
end;

他のヒント

GetEnumPropは、あなたがしようとしていることに対して間違った関数であるため、機能しませんでした。しかし、あなたは近いです。 TypInfoユニットにもあるGetEnumNameとGetEnumValueを試してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top