線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:4959
推到 Plurk!
推到 Facebook!

Stringgrid 裡放 combobox

尚未結案
wsxcv123
一般會員


發表:36
回覆:27
積分:12
註冊:2004-11-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-07 16:15:06 IP:61.222.xxx.xxx 未訂閱
如何讓Stringgrid 的某個 column 都為combobox. 網路找到的 example 大多是將 combobox 的 Top, Left, Height, Width 設成 Stringgrid 的 某個 cell. 有沒有 BCB 的丫?
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-07 20:32:40 IP:61.64.xxx.xxx 未訂閱
給你一個範例    //---------------------------------------------------------------------------
// Unit1.h    #ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
  TStringGrid *StringGrid1;
  void __fastcall FormDestroy(TObject *Sender);
  void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow,
          TRect &Rect, TGridDrawState State);
private:        // User declarations
public:                // User declarations
  __fastcall TForm1(TComponent* Owner);
  void __fastcall ComboBoxChange(TObject *Sender);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
/////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
// Unit1.cpp
#include 
#pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TList* ComboBoxList;    //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  ComboBoxList = new TList();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  for (int i = 0; i < ComboBoxList->Count; i  )
    delete (TComboBox*)(ComboBoxList->Items[i]);
  delete ComboBoxList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{
  TComboBox* ComboBoxItem;
  TPoint OrgPoint, TempPoint, NewPoint;
  AnsiString szText[3] = {"1111", "2222", "3333"};      // 固定在第一個Column
  if (ARow >= 1 && ACol == 1)
  {
    ComboBoxItem = new TComboBox(Form1);
    ComboBoxItem->Tag = ARow;
    OrgPoint.x = Rect.Left;
    OrgPoint.y = Rect.Top;
    // 轉換座標.
    TempPoint = ((TStringGrid*)Sender)->ClientToScreen(OrgPoint);
    NewPoint = ScreenToClient(TempPoint);
    ComboBoxItem->SetBounds(NewPoint.x   TStringGrid(Sender).GridLineWidth*2,
                            NewPoint.y   TStringGrid(Sender).GridLineWidth*2,
                            Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);
    InsertControl(ComboBoxItem);        // 加入字串
    for(int i = 0; i < 3; i  )
      ComboBoxItem->Items->Add(szText[i]);
      
    ComboBoxItem->ItemIndex = 0;
    ComboBoxItem->OnChange = ComboBoxChange;
    ComboBoxList->Add(ComboBoxItem);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBoxChange(TObject *Sender)
{
  // 秀出被選取的ComboBox的值
  ShowMessage("第"   IntToStr(((TComboBox*)Sender)->Tag)  
              "行的ComboBox被選取, 值為"   ((TComboBox*)Sender)->Text);
}
//---------------------------------------------------------------------------
wsxcv123
一般會員


發表:36
回覆:27
積分:12
註冊:2004-11-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-07 23:52:09 IP:218.168.xxx.xxx 未訂閱
因為有好多個 combobox 所以 stringgrid 會自動出現 scrollbar Click 在 scroll bar , combo 的 位子不會變。 要怎麼讓他可以安照 > 謝謝
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-12-08 09:30:56 IP:210.243.xxx.xxx 未訂閱
如果要按下Scrollbar會隨之反應, 你要subclass StringGrid, 並攔Scrollbar事件 WM_VSCROLL, WM_HSCROLL.    Tag是用來判斷是哪個ComboBox被使用者選取    CPP檔在下篇
//---------------------------------------------------------------------------
//Unit1.h    #ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:        // IDE-managed Components
  TStringGrid *StringGrid1;
  void __fastcall FormCreate(TObject *Sender);
  void __fastcall FormDestroy(TObject *Sender);
  void __fastcall StringGrid1DrawCell(TObject *Sender, int ACol, int ARow,
          TRect &Rect, TGridDrawState State);
private:        // User declarations
public:                // User declarations
  __fastcall TForm1(TComponent* Owner);
  void __fastcall ComboBoxChange(TObject *Sender);
  TWndMethod OrgWndProc;
  void __fastcall NewWndProc(Messages::TMessage &Message);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif    
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-12-08 09:34:37 IP:210.243.xxx.xxx 未訂閱
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TList* ComboBoxList;    //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  ComboBoxList = new TList();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NewWndProc(Messages::TMessage &Message)
{
  OrgWndProc(Message);
  //攔 Scrollbar事件 WM_VSCROLL, WM_HSCROLL
  if (Message.Msg == WM_VSCROLL || Message.Msg == WM_HSCROLL)
    StringGrid1->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Subclass StringGrid1, 好用來攔 Scrollbar事件 WM_VSCROLL, WM_HSCROLL
  OrgWndProc = StringGrid1->WindowProc;
  StringGrid1->WindowProc = NewWndProc;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  for (int i = 0; i < ComboBoxList->Count; i  )
    delete (TComboBox*)(ComboBoxList->Items[i]);
  delete ComboBoxList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{
  TComboBox* ComboBoxItem;
  TPoint OrgPoint, TempPoint, NewPoint;
  AnsiString szText[3] = {"1111", "2222", "3333"};      // 固定在第一個Column
  if (ARow >= 1 && ACol == 1)
  {
    ComboBoxItem = new TComboBox(Form1);
    // 用Tag來判斷是哪個ComboBox被按下
    ComboBoxItem->Tag = ARow;
    OrgPoint.x = Rect.Left;
    OrgPoint.y = Rect.Top;
    // 轉換座標.
    TempPoint = ((TStringGrid*)Sender)->ClientToScreen(OrgPoint);
    NewPoint = ScreenToClient(TempPoint);
    ComboBoxItem->SetBounds(NewPoint.x   TStringGrid(Sender).GridLineWidth*2,
                            NewPoint.y   TStringGrid(Sender).GridLineWidth*2,
                            Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);
    InsertControl(ComboBoxItem);        // 加入字串
    for(int i = 0; i < 3; i  )
      ComboBoxItem->Items->Add(szText[i]);
      
    ComboBoxItem->ItemIndex = 0;
    ComboBoxItem->Text = IntToStr(ARow);
    ComboBoxItem->OnChange = ComboBoxChange;
    ComboBoxList->Add(ComboBoxItem);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBoxChange(TObject *Sender)
{
  // 秀出被選取的ComboBox的值
  ShowMessage("第"   IntToStr(((TComboBox*)Sender)->Tag)  
              "行的ComboBox被選取, 值為"   ((TComboBox*)Sender)->Text);
}
//---------------------------------------------------------------------------
wsxcv123
一般會員


發表:36
回覆:27
積分:12
註冊:2004-11-10

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-12-08 13:03:13 IP:61.222.xxx.xxx 未訂閱
安照你的做發 combo 已經可以隨者 scroll bar 變 但 scroll bar 安到底,最後的 combo 會出現 2次 要怎ㄇ解決呢?    
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-12-08 18:23:06 IP:210.243.xxx.xxx 未訂閱
有改的地方我貼下面, 其它部份和上篇我給你的一樣
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Subclass StringGrid1, 好用來攔 Scrollbar事件 WM_VSCROLL, WM_HSCROLL
  OrgWndProc = StringGrid1->WindowProc;
  StringGrid1->WindowProc = NewWndProc;      TComboBox* ComboBoxItem;
  for (int i = 0; i < StringGrid1->RowCount; i  )
  {
    ComboBoxList->Add(NULL);
  }
}    void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
{
  TComboBox* ComboBoxItem;
  TPoint OrgPoint, TempPoint, NewPoint;
  AnsiString szText[3] = {"1111", "2222", "3333"};      // 固定在第一個Column
  if (ARow >= 1 && ACol == 1)
  {
    if (ComboBoxList->Items[ARow])
    {
      ComboBoxItem = ((TComboBox*)(ComboBoxList->Items[ARow]));
      // 用Tag來判斷是哪個ComboBox被按下
      OrgPoint.x = Rect.Left;
      OrgPoint.y = Rect.Top;
      // 轉換座標.
      TempPoint = ((TStringGrid*)Sender)->ClientToScreen(OrgPoint);
      NewPoint = ScreenToClient(TempPoint);
      RemoveControl(ComboBoxItem);
      ComboBoxItem->SetBounds(NewPoint.x   TStringGrid(Sender).GridLineWidth*2,
                              NewPoint.y   TStringGrid(Sender).GridLineWidth*2,
                              Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);
      InsertControl(ComboBoxItem);
      ComboBoxItem->ItemIndex = 0;
      ComboBoxItem->Text = IntToStr(ARow);
    }
    else
    {
      ComboBoxItem = new TComboBox(Form1);          // 用Tag來判斷是哪個ComboBox被按下
      OrgPoint.x = Rect.Left;
      OrgPoint.y = Rect.Top;
      // 轉換座標.
      TempPoint = ((TStringGrid*)Sender)->ClientToScreen(OrgPoint);
      NewPoint = ScreenToClient(TempPoint);
      ComboBoxItem->SetBounds(NewPoint.x   TStringGrid(Sender).GridLineWidth*2,
                              NewPoint.y   TStringGrid(Sender).GridLineWidth*2,
                              Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);          InsertControl(ComboBoxItem);
      // 加入字串
      for(int i = 0; i < 3; i  )
        ComboBoxItem->Items->Add(szText[i]);
      ComboBoxItem->Tag = ARow;
      ComboBoxItem->ItemIndex = 0;
      ComboBoxItem->Text = IntToStr(ARow);
      ComboBoxItem->OnChange = ComboBoxChange;
      ComboBoxList->Items[ARow] = ComboBoxItem;      
    }
  }
}
wsxcv123
一般會員


發表:36
回覆:27
積分:12
註冊:2004-11-10

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-12-09 10:49:34 IP:61.222.xxx.xxx 未訂閱
已經可以了 好棒喔    3Q 3Q
paulsky
一般會員


發表:0
回覆:2
積分:0
註冊:2005-06-22

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-06-22 15:01:00 IP:202.39.xxx.xxx 未訂閱
請問為何在關閉時會有Access violation address的錯誤訊息出現...
paulsky
一般會員


發表:0
回覆:2
積分:0
註冊:2005-06-22

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-06-22 16:55:58 IP:202.39.xxx.xxx 未訂閱
是我試錯了,程式關閉沒問題,不過拉動水平ScrollBar拉動時ComboBox有問題....
系統時間:2024-04-29 16:21:45
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!