在一个窗体中我引入了mshflexgrid控件,由于mshflexgrid控件不能修改单元格中的内容,于是在窗体中加入textbox,将textbox中的值放到mshflexgrid中,程序如下:
Private Sub MSHFlexGrid1_KeyPress(KeyAscii As Integer)
If MSHFlexGrid1.Row <> MSHFlexGrid1.Rows - 1 Then ' 移动文本框至当前单元格 TextBox1.top = MSHFlexGrid1.CellTop + MSHFlexGrid1.top TextBox1.Left = MSHFlexGrid1.CellLeft + MSHFlexGrid1.Left ' 保存当前单元格行和列的位置 gRow = MSHFlexGrid1.Row gCol = MSHFlexGrid1.Col ' 设置文本框与当前单元格相同大小 TextBox1.Width = MSHFlexGrid1.CellWidth TextBox1.Height = MSHFlexGrid1.CellHeight '最后一列不能填如数据 If MSHFlexGrid1.Row <> MSHFlexGrid1.Rows - 1 Then TextBox1.Text = MSHFlexGrid1.Text ' 显示文本框 TextBox1.Visible = True TextBox1.ZOrder 0 ' 把 Text1 放到最前面! TextBox1.SetFocus '重新定位事件至文本框 If KeyAscii <> ASC_ENTER And KeyAscii <> ASC_ESC Then SendKeys Chr$(KeyAscii) End If End If End If End Sub
Private Sub TextBox1_Change() MSHFlexGrid1.Text = TextBox1.Text End Sub Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii = ASC_ENTER Then MSHFlexGrid1.SetFocus ' Set focus back to grid, see Text_LostFocus. KeyAscii = 0 ' Ignore this KeyPress. End If End Sub Private Sub TextBox1_LostFocus() Dim tmpRow As Integer Dim tmpCol As Integer ' Save current settings of Grid Row and col. This is needed only if ' the focus is set somewhere else in the Grid. tmpRow = Grid1.Row tmpCol = Grid1.Col ' Set Row and Col back to what they were before Text1_LostFocus: Grid1.Row = gRow Grid1.Col = gCol Grid1.Text = TextBox1.Text ' Transfer text back to grid. TextBox1.SelStart = 0 ' Return caret to beginning. TextBox1.Visible = False ' Disable text box. ' Return row and Col contents: Grid1.Row = tmpRow Grid1.Col = tmpCol End Sub
用这种方法基本实现了我要的功能要求,但现在的问题是,mshflexgrid无法聚焦,点击mshflexgrid中的单元格,无法看到光标所在的位置,也就是TextBox1_LostFocus()无效,我该怎么样才能使mshflexgrid聚焦?还有没有别的办法?请赐教,谢谢! |