我在窗体创建的时候给组件的索引赋值,但是我显示这些索引的值的时候,却不是我事先赋的值,这是怎么回事啊?procedure TForm1.FormCreate(Sender: TObject);
begin
  BitBtn1.ComponentIndex := 18;
  BitBtn2.ComponentIndex := 19;
  BitBtn3.ComponentIndex :=  2;
  BitBtn4.ComponentIndex := 17;
  BitBtn5.ComponentIndex := 20;
end;procedure TForm1.Button1Click(Sender: TObject);
var
  i :integer;
begin
  for i:=0 to ComponentCount-1 do
  if (Components[i] is TBitBtn ) then
    showmessage(Components[i].Name+'的ComponentIndex是:'+
                inttostr(Components[i].ComponentIndex));
end;

解决方案 »

  1.   

    The first component in the list has a ComponentIndex value of 0, the second has a value of 1, and so on. Therefore, when using ComponentIndex with ComponentCount, note that ComponentCount is always 1 more than the highest Components index.
      

  2.   

    你必須連續執行兩次, 修改如下就可解決: 還有保證你在窗體上的控件 > 20 個!!
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      BitBtn1.ComponentIndex := 18;
      BitBtn2.ComponentIndex := 19;
      BitBtn3.ComponentIndex :=  2;
      BitBtn4.ComponentIndex := 17;
      BitBtn5.ComponentIndex := 20;  BitBtn1.ComponentIndex := 18;
      BitBtn2.ComponentIndex := 19;
      BitBtn3.ComponentIndex :=  2;
      BitBtn4.ComponentIndex := 17;
      BitBtn5.ComponentIndex := 20;
    end;
      

  3.   

    to : aiirii(ari) 
    你的方法是成功了,但我不明白为什么,能说说是什么原因吗
      

  4.   

    還有一件事, 最好是小數在前!!
    就是說如下連續兩次付值比較好!!
      BitBtn3.ComponentIndex :=  2;
      BitBtn4.ComponentIndex := 17;
      BitBtn1.ComponentIndex := 18;
      BitBtn2.ComponentIndex := 19;
      BitBtn5.ComponentIndex := 20;
      

  5.   

    to : aiirii(ari) 
    为什么要写两次啊,为什么要大于20个控件啊??
      

  6.   

    The first component in the list has a ComponentIndex value of 0,
    the second has a value of 1, and so on.赋值无效
      

  7.   

    因为所有组件的Index必须连续唯一,而且从零开始,所以你对一个控件设置Index的时候有可能导致其它控件的Index的重新分布
      

  8.   

    to : : manfeng() 
    那请问有没有解决方法啊
      

  9.   

    是這樣的, "所有组件的Index必须连续唯一", 你第一次設置, 是將一個 ComponentIndex
    改變, 如改到20,  會引致原來位置的那個 ComponentIndex = 20 自動尋找一個新的有效的值,測試中, 原位置的并不是自動退到最後一位,  而且, 系統保證排在它後面的控件還是排在它後面, 所以, 可能就會引起一串控件改變ComponentIndex,  就會再次引起 ComponentIndex 的變化, 但第二次, 由于你設置的位置已經相對比較接近了! 所以, 影響到的重排列的控件就不是那麼多, 就成功了!!