關於動態產生 CheckBox 的疑問 |
答題得分者是:jow
|
elva349
一般會員 發表:15 回覆:21 積分:17 註冊:2007-04-17 發送簡訊給我 |
需求是這樣的
我想要在button1 設定 動態產生 CheckBox 的數量 然後在button2 依造 Edit1 內輸入什麼創造出CheckBox 我現在是這樣寫 這是.h [code cpp] class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TEdit *Edit1; void __fastcall FormCreate(TObject *Sender); void __fastcall FormClose(TObject *Sender, TCloseAction &Action); void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations int a; int top; TCheckBox *A[10]; int counter; __fastcall TForm1(TComponent* Owner); }; [/code] 這是button2 [code cpp] void __fastcall TForm1::Button2Click(TObject *Sender) { A[a]=new TCheckBox(this); A[a]->Caption=Edit1->Text; A[a]->Top=top; A[a]->Parent=Form1; A[a]->Left=56; a=a 1; top=top 24; }[/code] 我現在已經能動態產生CheckBox ,但是我想在 Button1 動態決定可產生的數量.而不是一開始就固定好! 能敎敎我該怎麼改嗎? 假設 可產生的數量 是從 Edit2->Text 來好了! 感恩~ |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
(1)Button1 - 動態產生若干 TCheckBox物件
(2)Button2 - 利用自定陣列屬性,來存取 動態產生之TCheckBox (3)Button3 - 以一個TList來排序. (4)Button4 -清除 動態產生之TCheckBox 測試碼下載: http://delphi.ktop.com.tw/board.php?cid=31&fid=130&tid=91038 [code cpp] //--------------------------------------------------------------------------- #ifndef fMainH #define fMainH //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />//--------------------------------------------------------------------------- class TForm1 : public TForm { __published: TEdit *Edit1; TEdit *Edit2; TButton *Button1; TButton *Button2; TButton *Button3; TListBox *ListBox1; TButton *Button4; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); void __fastcall Button3Click(TObject *Sender); void __fastcall Button4Click(TObject *Sender); private: DynamicArray int top; int left; int __fastcall GetCount(); TCheckBox* __fastcall GetCheckBoxes(int Index); public: __fastcall TForm1(TComponent* Owner); __fastcall ~TForm1(){Clear();} void __fastcall Clear(); __property int Count={read=GetCount}; __property TCheckBox* CheckBoxes[int Index]={read=GetCheckBoxes}; }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [/code] [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Clear(); Edit2->Text = 40; } //--------------------------------------------------------------------------- void __fastcall TForm1::Clear() { left=0; top=0; if(ArrayOfCheckBox.Length>0){ for(int i=0;i ArrayOfCheckBox.Length=0; } } //--------------------------------------------------------------------------- int __fastcall TForm1::GetCount() { return ArrayOfCheckBox.Length; } //--------------------------------------------------------------------------- TCheckBox* __fastcall TForm1::GetCheckBoxes(int Index) { AnsiString Key="CheckBox" IntToStr(Index); return (TCheckBox*)FindComponent(Key); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { TCheckBox *O; int MaxCheckboxCount = StrToInt(Edit2->Text); int count=ArrayOfCheckBox.Length; if(ArrayOfCheckBox.Length ArrayOfCheckBox.Length = count; ArrayOfCheckBox[count-1]=new TCheckBox(this); AnsiString S="CheckBox" IntToStr(count-1); O=ArrayOfCheckBox[count-1]; O->Tag=random(MaxInt);//亂數值,測試用. O->Name=S; O->Caption=S " [" IntToStr(O->Tag) "]"; O->Left=left; O->Top=top; O->Width=150; O->Height=20; O->Parent=this; top =21; if(top>630){ top=0; left =150; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { for(int i=0;i if(O)O->Checked=!O->Checked; } } //--------------------------------------------------------------------------- int __fastcall CompareTag(void *Item1, void *Item2) { TCheckBox *p1=(TCheckBox*)Item1; TCheckBox *p2=(TCheckBox*)Item2; if(p1->Tag>p2->Tag)return 1; else if(p1->Tag return 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { TList *L=new TList; TCheckBox *O; try{ for(int i=0;i if(O)L->Add(O); } L->Sort(CompareTag);//排序, 依Tag值.(之前產生的亂數) ListBox1->Clear(); for(int i=0;i O=(TCheckBox*)L->Items[i]; ListBox1->Items->Add("[" IntToStr(O->Tag) "]" O->Name); } } __finally{ delete L; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button4Click(TObject *Sender) { Clear(); } //--------------------------------------------------------------------------- [/code] |
elva349
一般會員 發表:15 回覆:21 積分:17 註冊:2007-04-17 發送簡訊給我 |
|
elva349
一般會員 發表:15 回覆:21 積分:17 註冊:2007-04-17 發送簡訊給我 |
多謝大師指教~~我已經會用DynamicArray 來控制數量了~
只是在最後離開的時候 是否需要刪除所有的 ArrayOfCheckBox[i]; 呢? [code cpp] void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { if(ArrayOfCheckBox.Length>0) { for(int i=0;i } ArrayOfCheckBox.Length=0;} [/code]
編輯記錄
elva349 重新編輯於 2007-11-01 08:42:47, 註解 無‧
|
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
並不需要, 因為在產生TCheckBox時, Owner指定為 this, 也就是TForm1,
所以TForm1在destroy時, 就會負責將 TForm1.Components[]內的元件 釋放. 只是在此例中, 因為會用同一按鍵重複產生TCheckBox, 所以在產生 新的TCheckBox之前, 先叫用Clear()將原先產生的TCheckBox釋放. ===================引 用 elva349 文 章=================== 多謝大師指教~~我已經會用DynamicArray 來控制數量了~ 只是在最後離開的時候 是否需要刪除所有的 ArrayOfCheckBox[i]; 呢? |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |