在動態產生的StringGrid貼上從EXCEL複製的資料 |
答題得分者是:taishyang
|
Brady_Chen
一般會員 ![]() ![]() 發表:6 回覆:14 積分:9 註冊:2013-02-07 發送簡訊給我 |
各位前輩好, 小弟又來麻煩各位了!!
這次的問題小弟也是獨力奮戰了許久而無法解決 所以只好上來麻煩各位了 目的: 在動態產生的StringGrid貼上從EXCEL複製的資料 目前進度: 我己經可以做到在固定的StringGrid上貼上從EXCEL複製的資料 程式碼如下: //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; TStringList *Lst = new TStringList; AnsiString Ls; int MouseRow; int MouseCol; int copy=0; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol, int ARow, bool &CanSelect) { MouseRow = ARow; // 這是用來記錄 目前滑鼠在 StringGrid 哪一列 MouseCol = ACol; // 這是用來記錄 目前滑鼠在 StringGrid 哪一行 } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { int copycount=0; if(Shift.Contains(ssCtrl) && Key == 86) { for(int i = 0; i < Clipboard()->FormatCount;i ) { if(Clipboard()-> GetAsHandle(Clipboard()-> Formats[i])) //剪貼簿有內容 { RichEdit1->PasteFromClipboard(); for(int j=MouseRow; j { Ls = RichEdit1->Lines->Strings[j-MouseRow]; Lst->Delimiter = '/t'; Lst->DelimitedText = Ls; RichEdit2->Lines = Lst; StringGrid1->Cells[MouseCol][j]=Lst->Strings[0]; } MouseRow=0; MouseCol=0; RichEdit1->Clear(); RichEdit2->Clear(); // Clipboard()->Clear(); break; } } } } //---------------------------------------------------------------------------
觀念是使用RichEdit讀取複製到的資料 再貼到StringGrid內 問題: 由於我希望在 "動態產生" 的 StringGrid上進行貼上的動作 而且我產生的StringGrid不只一個 產生的數量也不固定 所以我必須讓程式判斷 1. 選到哪一個StringGrid的第幾行第幾列 2. 再判斷我按下ctrl v 並進行貼上的動作 我目前就卡在不知道怎麼讓程式判斷以上兩點 先謝謝各位前輩了 希望能夠撥空指導 謝謝!! |
sryang
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:39 回覆:762 積分:920 註冊:2002-06-27 發送簡訊給我 |
|
herbert2
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:58 回覆:640 積分:894 註冊:2004-04-16 發送簡訊給我 |
1. 選到哪一個StringGrid的第幾行第幾列
void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol, int ARow, bool &CanSelect) { //---- // 每個 StringGrid 的 SelectCell 都指向此 Event, AnsiString sx1; int ix1; // Sender 即呼叫此 Event 的 Component 的 Object, 因確定它是 TStringGrid, 直接轉 Type. TStringGrid *SG = (TStringGrid *)Sender; sx1 = SG->Name; // 看您想如何運用 ix1 = SG->Tag; // 小弟較喜歡用各個 TStringGrid->Tag 各編一號, 再以此做區分的判斷 switch (ix1) { case 1: //.... break; case 2: // .... break; default: // .... } //---- MouseRow = ARow; // 這是用來記錄 目前滑鼠在 StringGrid 哪一列 MouseCol = ACol; // 這是用來記錄 目前滑鼠在 StringGrid 哪一行 } //--------------------------------------------------------------------------- 2. 再判斷我按下ctrl v 並進行貼上的動作 void __fastcall TForm1::fFormKeyUp(TObject *Sender) { // 須設 Form1->KeyPreview = true; if (Key == 'V' && Shift.Contains(ssCtrl)) { // User 按下 Ctrl-V (Key 值, 大小寫都視同大寫) // 其它就看您要如何處理了! } } //--------------------------------------------------------------------------- 以上, 或許不盡理想, 請參考! |
老大仔
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
假如沒有強迫要用ctrl+v
有時..可以用轉彎的方法... 1.加個全域變數 放個按鈕 當使用者點到哪個grid就把那個grid名稱指向到該變數 (當然~這部份的事件指向也要在create grid時就先指定好就是) 然後再讓使用者去那個按鈕 在按下的事件中開始判斷、開始放入資料~ 2.弄個popmenu給每個grid 在create grid時把屬性指向到popmenu 然後在popmenu被按下(事件)時去判斷是由哪個grid觸發的 再去做資料貼入的動作~ 嗯..我的方法也沒多好 僅供參考XD
編輯記錄
老大仔 重新編輯於 2013-03-07 11:12:58, 註解 無‧
|
Brady_Chen
一般會員 ![]() ![]() 發表:6 回覆:14 積分:9 註冊:2013-02-07 發送簡訊給我 |
|
taishyang
站務副站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
若元件是動態產生的話,就得自己手動寫好事件函式的宣告以及實作的code
並且將元件的事件指向你手動寫好的事件 當事件觸發後,便可以由sender得知是哪個元件觸發的 另外該事件的參數會帶有ARow以及ACol就沒有你說的問題了 你可以搜尋[動態產生元件],就會有類似範例供你參考 ===================引 用 Brady_Chen 文 章=================== 恩... 也許是小弟表達不佳 或是 能力太差無法了解前輩的意思 我所動態生成的StringGrid並不是StringGrid1 我是利用指標生成的 所以我無法產生事件 因此無法獲得ARow和ACol 而且還要判斷是點擊哪個StringGrid 請問是我不懂前輩的指導 還是 前輩不了解我的問題?? 如需補充 我會儘快回應 謝謝!!! |
Brady_Chen
一般會員 ![]() ![]() 發表:6 回覆:14 積分:9 註冊:2013-02-07 發送簡訊給我 |
我實在不太會動態產生事件
自己亂寫如下 void __fastcall MySelectCell(TObject *Sender, int ACol,int ARow, bool &CanSelect) { TStringGrid *sgd=(TStringGrid*)Sender; int MouseRow2 = ARow; // 這是用來記錄 目前滑鼠在 StringGrid 哪一列 int MouseCol2 = ACol; // 這是用來記錄 目前滑鼠在 StringGrid 哪一行 ShowMessage("OK"); } sgd是我動態產生StringGrid 的指標 已經查了很多資料 但小弟還是搞不懂 不知道有沒有前輩肯指點一下 衷心感謝!!! 謝謝!!! |
taishyang
站務副站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
老大仔
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:78 回覆:837 積分:1088 註冊:2006-07-06 發送簡訊給我 |
感謝 taishyang 大大把Code寫出來
我不會C XD 其實~我說的內容 taishyang 大大已經把大概的內容都寫出來了~重點在紅色字~ ===================引 用 taishyang 文 章=================== .h class TForm1 : public TForm { __published: // User declarations void __fastcall MyStringGridSelectCell(TObject *Sender, int ACol, int ARow, bool &CanSelect); public: sg->OnSelectCell = MyStringGridSelectCell; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { if (sg){ delete sg; sg = NULL; } } //--------------------------------------------------------------------------- void __fastcall TForm1::MyStringGridSelectCell(TObject *Sender, int ACol, int ARow, bool &CanSelect) { if (((TStringGrid*)Sender)->Name == "MyStringGrid"){ ShowMessage("ok"); } } |
Brady_Chen
一般會員 ![]() ![]() 發表:6 回覆:14 積分:9 註冊:2013-02-07 發送簡訊給我 |
謝謝各位前輩的指導 小弟實在是受益良多 也成功的寫出來了 以下將成功寫出的CODE放上來以供其他人參考 謝謝每一位前輩!!! //************** CPP *****************************************
for(int j=0; j { sgd[j]->OnSelectCell = MySelectCell; sgd[j]->OnKeyDown = MyKeyDown; } //------------------------------------------------------------------------------------ TStringList *copySL2 = new TStringList; AnsiString copyAS2; int MouseRow2; int MouseCol2; void __fastcall TForm3::MySelectCell(TObject *Sender, int ACol,int ARow, bool &CanSelect) { TStringGrid *sgd=(TStringGrid*)Sender; //利用動態產生StringGrid的指標sgd觸發事件 MouseRow2 = ARow; // 這是用來記錄 目前滑鼠在 StringGrid 哪一列 MouseCol2 = ACol; // 這是用來記錄 目前滑鼠在 StringGrid 哪一行 } //--------------------------------------------------------------------------------- void __fastcall TForm3::MyKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { int copycount=0; if(Shift.Contains(ssCtrl) && Key == 86) //ctrl v { for(int i = 0; i < Clipboard()->FormatCount;i ) { if(Clipboard()-> GetAsHandle(Clipboard()-> Formats[i])) //剪貼簿有內容 { Form4->RichEdit1->PasteFromClipboard(); copycount = Form4->RichEdit1->Lines->Count; for(int j=MouseRow2; j
{ if(Form4->RichEdit1->Lines->Strings[j-MouseRow2]=="\t") //如果複製到空白, 就以0填入 {copyAS2=0;} else {copyAS2 = Form4->RichEdit1->Lines->Strings[j-MouseRow2];} copySL2->Delimiter = '/t'; copySL2->DelimitedText = copyAS2; Form4->RichEdit2->Lines = copySL2; for(int k=0; k
{ if (((TStringGrid*)Sender)->Name == "sgd" IntToStr(k)) sgd[k]->Cells[2][j]=copySL2->Strings[0]; } } MouseRow2=0; MouseCol2=0; Form4->RichEdit1->Clear(); Form4->RichEdit2->Clear(); Clipboard()->Clear(); break; } } } } //**************************** H *********************************** class TForm3 : public TForm { __published: // User declarations void __fastcall TForm3::MySelectCell(TObject *Sender, int ACol,int ARow, bool &CanSelect); //自加 void __fastcall TForm3::MyKeyDown(TObject *Sender, WORD &Key, TShiftState Shift); //自加 public: // User declarations __fastcall TForm3(TComponent* Owner); };
編輯記錄
Brady_Chen 重新編輯於 2013-03-08 16:11:24, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |