请问如何修改MSFlexGrid显示单元格中的内容?

lovecissi 2007-05-21 03:03:33
请教各位前辈~~:
请问如何修改MSFlexGrid显示单元格中的内容?
最好当我选中某一单元格时,鼠标变成" I " 这种形状,就像文本框一样修改?
...全文
418 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
guyehanxinlei 2007-05-22
  • 打赏
  • 举报
回复
用vsflex更简单一些
junki 2007-05-21
  • 打赏
  • 举报
回复
支持楼上上的,通过模拟实现
glowing_yu 2007-05-21
  • 打赏
  • 举报
回复
学习
clear_zero 2007-05-21
  • 打赏
  • 举报
回复
思路是这样的
用一个textbox覆盖在得到焦点的cell上,然后编辑textbox,当编辑完毕后textbox.visible=false,其内容转移到cell里面

Option Explicit

Dim bDoNotEdit As Boolean
Dim bOnFixedPart As Boolean

Private Sub Form_Click()
Call pSetCellValue
End Sub

Private Sub gridTest_Click()
'
' Display the textbox if the user clicked
' on a non-fixed row or column.
'
If bOnFixedPart Then Exit Sub
Call pEditGrid(32)
End Sub
Private Sub pEditGrid(KeyAscii As Integer)
'
' Populate the textbox and position it.
'
With txtEdit
Select Case KeyAscii
Case 0 To 32
'
' Edit the current text.
'
.Text = gridTest
.SelStart = 0
.SelLength = 1000

Case 8, 46, 48 To 57
'
' Replace the current text but only
' if the user entered a number.
'
.Text = Chr(KeyAscii)
.SelStart = 1
Case Else
'
' If an alpha character was entered,
' use a zero instead.
'
.Text = "0"
End Select
End With
'
' Show the textbox at the right place.
'
With gridTest
If .CellWidth < 0 Then Exit Sub
txtEdit.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth, .CellHeight
'
' NOTE:
' Depending on the style of the Grid Lines that you set, you
' may need to adjust the textbox position slightly. For example
' if you use raised grid lines use the following:
'
'txtEdit.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth - 8, .CellHeight - 8
End With

txtEdit.Visible = True
txtEdit.SetFocus
End Sub
Private Sub gridTest_GotFocus()

If bDoNotEdit Then Exit Sub
'
' Copy the textbox's value to the grid
' and hide the textbox.
'
Call pSetCellValue
End Sub
Private Sub gridTest_KeyPress(KeyAscii As Integer)
'
' Display the textbox.
'
Call pEditGrid(KeyAscii)
End Sub
Private Sub gridTest_LeaveCell()

If bDoNotEdit Then Exit Sub
Call gridTest_GotFocus
End Sub
Private Sub gridTest_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim l As Long
Dim lWidth As Long

With gridTest
For l = 0 To .Cols - 1
If .ColIsVisible(l) Then
lWidth = lWidth + .ColWidth(l)
End If
Next
'
' See if we are on the fixed part of the grid.
'
bOnFixedPart = (x < .ColWidth(0)) Or _
(x > lWidth) Or _
(y < .RowHeight(0)) Or _
(y > .Rows * .RowHeightMin)
End With
End Sub
Private Sub gridTest_Scroll()
Call gridTest_GotFocus
End Sub
Private Sub Form_Load()
Dim i As Long
'
' Set the grid and textbox
' to the same font.
'
With txtEdit.Font
.Name = gridTest.Font.Name
.Size = gridTest.Font.Size
.Weight = gridTest.Font.Weight
End With
txtEdit.BackColor = vb3DLight
'
' Add some rows and columns to the grid so we
' have something to start with.
'
With gridTest
.RowHeightMin = txtEdit.Height

' Size the first fixed column.
.ColWidth(0) = .ColWidth(0) / 2
.ColAlignment(0) = 1 ' Center center.

' Label the rows.
For i = .FixedRows To .Rows - 1
.TextArray(fLabel(i, 0)) = i
Next

' Label the columns.
For i = .FixedCols To .Cols - 1
.TextArray(fLabel(0, i)) = i
Next

' Right align data.
For i = .FixedCols To .Cols - 1
.ColAlignment(i) = flexAlignRightCenter
Next
End With

txtEdit = ""
bDoNotEdit = False
End Sub
Private Function fLabel(lRow As Long, lCol As Long) As Long
fLabel = lCol + gridTest.Cols * lRow
End Function
Private Sub txtEdit_KeyDown(KeyCode As Integer, Shift As Integer)
'
' See what key was pressed in the textbox.
'
With gridTest
Select Case KeyCode
Case 13 'ENTER
.SetFocus
Case 27 'ESC
txtEdit.Visible = False
.SetFocus
Case 38 'Up arrow
.SetFocus
DoEvents
If .Row > .FixedRows Then
bDoNotEdit = True
.Row = .Row - 1
bDoNotEdit = False
End If
Case 40 'Down arrow
.SetFocus
DoEvents
If .Row < .Rows - 1 Then
bDoNotEdit = True
.Row = .Row + 1
bDoNotEdit = False
End If
End Select
End With
End Sub
Private Sub txtEdit_KeyPress(KeyAscii As Integer)
'
' Delete carriage returns to get rid of beep
' and only allow numbers.
'
Select Case KeyAscii
Case Asc(vbCr)
KeyAscii = 0
Case 8, 46
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub pSetCellValue()
'
' NOTE:
' This code should be called anytime
' the grid loses focus and the grid's
' contents may change. Otherwise, the
' cell's new value may be lost and the
' textbox may not line up correctly.
'
If txtEdit.Visible Then
gridTest.Text = txtEdit.Text
txtEdit.Visible = False
End If
End Sub

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧