枚举为整数,反之亦然(使用combobox itemindex)

时间:2013-02-09 21:52:53

标签: delphi combobox

不确定我在这里做错了什么

//update controls from main form Tshape
 form2.cbxShape.ItemIndex:= ord(Shape1.Shape);
 form2.cbxColor.Selected:= Shape1.Brush.Color;
 form2.cbxStyle.ItemIndex:= Ord(Shape1.Brush.Style);
 if form2.ShowModal = mrOK then
 begin
  //update main form Tshape from controls
   Shape1.Shape:= TShapeType(form2.cbxShape.ItemIndex);
   Shape1.Brush.Color:= form2.cbxColor.Selected;
   Shape1.Brush.Style:= TBrushStyle(form2.cbxStyle.ItemIndex);
 end;

我没有得到正确的形状或画笔样式(颜色效果很好)

列表框项目如下:

Circle
Ellipse
Rectangle
RoundRect
RoundSquare
Square

BDiagonal
Clear
Cross
DiagCross
FDiagonal
Horizontal
Solid

2 个答案:

答案 0 :(得分:5)

查看TShapeTypeTBrushStyle的声明:

  

TShapeType =(stRectangle,stSquare,stRoundRect,stRoundSquare,   stEllipse,stCircle);

     

TBrushStyle =(bsSolid,bsClear,bsHorizo​​ntal,bsVertical,   bsFDiagonal,bsBDiagonal,bsCross,bsDiagCross);

列表框中的项目必须采用相同的顺序,包含相同的相应枚举。

答案 1 :(得分:1)

LU RD已经回答了这个问题 另一种不受任何排序影响的方法可能是

implementation
uses TypInfo;
{$R *.dfm}

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
  SetPropValue(Shape1,'Shape','st' + Combobox1.Text);
  Caption := StringReplace(GetPropValue(Shape1,'Shape',true),'st','',[]);
end;