让StringGrid控件显示下拉菜单
1、效果预览
2、关键代码
void __fastcall TForm1::FormCreate(TObject *Sender) { StringGrid1->ColCount = 4; StringGrid1->RowCount = 6; StringGrid1->DefaultRowHeight = ComboBox1->Height; StringGrid1->RowHeights[0] = 18; StringGrid1->ColWidths[2] = 80; StringGrid1->Cells[0][0] = "FirstName"; StringGrid1->Cells[1][0] = "LastName"; StringGrid1->Cells[2][0] = "SBVLEdit LV1"; StringGrid1->Cells[3][0] = "Action"; ComboBox1->Text = ""; ComboBox1->Items->Add("Teen"); ComboBox1->Items->Add("Adult"); ComboBox1->Items->Add("Senior"); ComboBox1->Visible = False; } //--------------------------------------------------------------------------- void __fastcall TForm1::ComboBox1Change(TObject *Sender) { StringGrid1->Cells[StringGrid1->Col][StringGrid1->Row] = ComboBox1->Items->Strings[ComboBox1->ItemIndex]; } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1DblClick(TObject *Sender) //双击事件 { TRect r; if(StringGrid1->Col == 2) { int ACol = StringGrid1->Col; int ARow = StringGrid1->Row; r = StringGrid1->CellRect(ACol,ARow); ComboBox1->Top = StringGrid1->Top + r.Top + StringGrid1->GridLineWidth + 1; ComboBox1->Left = StringGrid1->Left + r.Left + StringGrid1->GridLineWidth +1; ComboBox1->Width = r.Right - r.Left; ComboBox1->ItemIndex = ComboBox1->Items->IndexOf(StringGrid1->Cells[ACol][ARow]); ComboBox1->Visible = true; } else ComboBox1->Visible = false; } //---------------------------------------------------------------------------
参考:http://ymg97526.blog.163.com/blog/static/17365816020135264552765/