下面是一个通用过程,控制控件的位置, 注意一点,是设计时将控件设置为.Visible = False 然后需要在lostfocus时间中写:.Visible = False 在flexgrid的click事件里,根据相应列调用过程s_SetPositionPrivate Sub s_SetPosition(oObj As Object) Dim i With Flex1 'flex1是flexgrid对象 oObj.Top = .Top + .RowPos(.Row) + 50 If .Col = 5 Then oObj.Top = oObj.Top + 20 oObj.Left = .Left + 50 For i = 0 To .Col - 1 oObj.Left = oObj.Left + .ColWidth(i) Next i oObj.Visible = True oObj.SetFocus End With End Sub
End If End SubPrivate Sub CboControl_Change() '将下拉列表框信息写入表格中 Call CboControl_Click End SubPrivate Sub CboControl_Click() '将下拉列表框信息写入表格中 If Trim(CboControl.Text) = "" Then Exit Sub
' Try this: ' Need: lstS(ListBox),MSG(MsFlexGrid) Dim cRow As Integer, cCol As Integer, cWidth As Single, cLeft As Single, cTop As SinglePrivate Sub Form_Load() Dim i As Integer For i = 1 To 1000 DoEvents lstS.AddItem "items" & CStr(i) Next ' MSG.Move 0, 0, Me.Width, Me.Height End SubPrivate Sub Form_Resize() If Me.WindowState <> 1 Then MSG.Move 0, 0, Me.Width - 300, Me.Height - 300 End SubPrivate Sub Form_Unload(Cancel As Integer) lstS.Clear End SubPrivate Sub lstS_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyReturn Then lstS.Visible = False MSG.Clip = lstS.List(lstS.ListIndex) MSG.SetFocus End If If KeyCode = 27 Then lstS.Visible = False End If If KeyCode = vbKeyLeft Or KeyCode = vbKeyRight Then lstS.Visible = False End If End SubPrivate Sub lstS_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then lstS.Visible = False MSG.Clip = lstS.List(lstS.ListIndex) MSG.SetFocus End If End SubPrivate Sub MSG_EnterCell() MSG.CellBackColor = vbBlue MSG.CellForeColor = vbWhite If MSG.Row > 0 And MSG.Col = 2 Then cRow = MSG.Row cCol = MSG.Col cWidth = MSG.CellWidth cLeft = MSG.CellLeft + MSG.Left cTop = MSG.CellTop + MSG.Top + MSG.CellHeight For i = 0 To lstS.ListCount - 1 If lstS.List(i) = MSG.Clip Then lstS.Selected(i) = True Exit For End If DoEvents Next lstS.Move cLeft, cTop ', cWidth ', lstS.Height lstS.Visible = True lstS.SetFocus End If End SubPrivate Sub MSG_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 27 Then lstS.Visible = False End If End SubPrivate Sub MSG_LeaveCell() lstS.Visible = False MSG.CellBackColor = vbWhite MSG.CellForeColor = vbBlack End SubPrivate Sub MSG_Scroll() lstS.Visible = FalseEnd Sub
注意一点,是设计时将控件设置为.Visible = False
然后需要在lostfocus时间中写:.Visible = False
在flexgrid的click事件里,根据相应列调用过程s_SetPositionPrivate Sub s_SetPosition(oObj As Object)
Dim i
With Flex1 'flex1是flexgrid对象
oObj.Top = .Top + .RowPos(.Row) + 50
If .Col = 5 Then oObj.Top = oObj.Top + 20
oObj.Left = .Left + 50
For i = 0 To .Col - 1
oObj.Left = oObj.Left + .ColWidth(i)
Next i
oObj.Visible = True
oObj.SetFocus
End With
End Sub
其实你只需在当前cell放置一个就可以,
其余的放一张类似combobox的图片做背景,当用户移动到cell时,把真正的combobox移动到当前cell即可。
如果你一定要全放上combobox的化,你可以使用控件数组,放一个index为0的combobox,然后动态生成新的combobox,以行为控件索引,假设窗体上combobox控件为cbo(0),则你可以在代码中这样实现。
1. 先移动cbo(0)到第一行的cell
2.利用循环加载并生成combox,并挪动到适当cell
for i=1 to flexgrid.rows-1
load cbo(i)
...... '移动cbo(i)到适当位置,可把我前面那段代码适当修改放在这里
cbo(i).visible=true
next
3.为cbo数组赋值。
'CboControl 是 combobox控件
If MsgTg.Col <> 1 Then 假如第一列要显示combobox则<>1
CboControl.Visible = False
Exit Sub
Else
CboControl.Text = MsgTg.TextMatrix(MsgTg.row, MsgTg.Col)
With flexgrid
.RowSel = .row
.ColSel = .Col
'下面是把文本框 cbocontrol 移到当前单元格
CboControl.Move .Left + .CellLeft, .Top + .CellTop, _
.CellWidth - ScaleX(1, vbPixels, vbTwips)
CboControl.Visible = True
CboControl.ZOrder
CboControl.SetFocus
CboControl.BackColor = &HE0E0E0
CboControl.SelLength = Len(CboControl.Text)
' CboControl.Text = .TextMatrix(.row, .Col)
End With
End If
End SubPrivate Sub CboControl_Change()
'将下拉列表框信息写入表格中
Call CboControl_Click
End SubPrivate Sub CboControl_Click()
'将下拉列表框信息写入表格中 If Trim(CboControl.Text) = "" Then Exit Sub
flexgrid.TextMatrix(flexgrid.row, flexgrid.Col) = Trim(CboControl.Text)
flexgrid.CellBackColor = &HFFFFFF
'CboControl.Visible = False
End Sub
msgtg和flexgrid是同一个Msflexgrid控件,你自己改改吧!
在鼠标点击到需要显示combobox的那一列你再把他显示 并根据位置调整高度宽度
并做一定的初始化 在用户作出选择以后在将该行的数值变成选择的值 再将combobox变成不可见 一切就OK了 呵呵
给分!
同时把上面的flexgrid替换成你的MsFlexGrid的名称(上面的msgtg和flexgrid实际上是同一个控件)
再把CboControl替换成你的ComboBox就可以了。
' Need: lstS(ListBox),MSG(MsFlexGrid)
Dim cRow As Integer, cCol As Integer, cWidth As Single, cLeft As Single, cTop As SinglePrivate Sub Form_Load()
Dim i As Integer
For i = 1 To 1000
DoEvents
lstS.AddItem "items" & CStr(i)
Next
' MSG.Move 0, 0, Me.Width, Me.Height
End SubPrivate Sub Form_Resize()
If Me.WindowState <> 1 Then MSG.Move 0, 0, Me.Width - 300, Me.Height - 300
End SubPrivate Sub Form_Unload(Cancel As Integer)
lstS.Clear
End SubPrivate Sub lstS_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
lstS.Visible = False
MSG.Clip = lstS.List(lstS.ListIndex)
MSG.SetFocus
End If
If KeyCode = 27 Then
lstS.Visible = False
End If
If KeyCode = vbKeyLeft Or KeyCode = vbKeyRight Then
lstS.Visible = False
End If
End SubPrivate Sub lstS_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
lstS.Visible = False
MSG.Clip = lstS.List(lstS.ListIndex)
MSG.SetFocus
End If
End SubPrivate Sub MSG_EnterCell()
MSG.CellBackColor = vbBlue
MSG.CellForeColor = vbWhite
If MSG.Row > 0 And MSG.Col = 2 Then
cRow = MSG.Row
cCol = MSG.Col
cWidth = MSG.CellWidth
cLeft = MSG.CellLeft + MSG.Left
cTop = MSG.CellTop + MSG.Top + MSG.CellHeight
For i = 0 To lstS.ListCount - 1
If lstS.List(i) = MSG.Clip Then
lstS.Selected(i) = True
Exit For
End If
DoEvents
Next
lstS.Move cLeft, cTop ', cWidth ', lstS.Height
lstS.Visible = True
lstS.SetFocus
End If
End SubPrivate Sub MSG_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then
lstS.Visible = False
End If
End SubPrivate Sub MSG_LeaveCell()
lstS.Visible = False
MSG.CellBackColor = vbWhite
MSG.CellForeColor = vbBlack
End SubPrivate Sub MSG_Scroll()
lstS.Visible = FalseEnd Sub