wsxcv123
一般會員

 發表:36 回覆:27 積分:12 註冊:2004-11-10
發送簡訊給我
|
如何讓Stringgrid 的某個 column 都為combobox.
網路找到的 example 大多是將 combobox 的 Top, Left, Height, Width 設成
Stringgrid 的 某個 cell.
有沒有 BCB 的丫?
|
Zard
尊榮會員
    
 發表:24 回覆:396 積分:539 註冊:2003-11-26
發送簡訊給我
|
給你一個範例 //---------------------------------------------------------------------------
// 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
發送簡訊給我
|
因為有好多個 combobox 所以 stringgrid 會自動出現 scrollbar
Click 在 scroll bar , combo 的 位子不會變。 ![]()
要怎麼讓他可以安照 > 謝謝
|
Zard
尊榮會員
    
 發表:24 回覆:396 積分:539 註冊:2003-11-26
發送簡訊給我
|
如果要按下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
發送簡訊給我
|
//--------------------------------------------------------------------------- #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
發送簡訊給我
|
安照你的做發 combo 已經可以隨者 scroll bar 變
但 scroll bar 安到底,最後的 combo 會出現 2次
要怎ㄇ解決呢?
|
Zard
尊榮會員
    
 發表:24 回覆:396 積分:539 註冊:2003-11-26
發送簡訊給我
|
有改的地方我貼下面, 其它部份和上篇我給你的一樣
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
發送簡訊給我
|
已經可以了
好棒喔 3Q 3Q
|
paulsky
一般會員

 發表:0 回覆:2 積分:0 註冊:2005-06-22
發送簡訊給我
|
請問為何在關閉時會有Access violation address的錯誤訊息出現...
|
paulsky
一般會員

 發表:0 回覆:2 積分:0 註冊:2005-06-22
發送簡訊給我
|
是我試錯了,程式關閉沒問題,不過拉動水平ScrollBar拉動時ComboBox有問題....
|