.ColAlignment(0) = flexAlignLeftCenter
.ColAlignment(1) = flexAlignLeftCenter
.ColAlignment(2) = flexAlignLeftCenter
.ColAlignment(3) = flexAlignLeftCenter
.ColAlignment(4) = flexAlignLeftCenter
End With
End Sub
//设置选中行的颜色。
//当时这个程序的目的是修改数据库中产品的分类信息(好像是,要不然就是其他信息),
//因为信息量非常简单,实际放到表格上只有两列,而那时候我们的流行计算机还是800×600的,
//所以,如果用两列的话会非常难看,我就在一行中显示两条数据,单元格1、2是第一个产品的,
//单元格3是分隔,单元格4、5是第二个产品。后面的程序也是围绕着这个要求,所以不同与常用
//的Grid,但思路是一样的。
Private Sub SetCellColor(ByVal row As Integer, ByVal col As Integer, ByVal ColorType As Integer)
Dim c As Integer
With flxData
If col = 1 Or col = 4 Then
col = col - 1
End If
c = col + 1
.row = row
.col = col
If ColorType = 0 Then
.CellBackColor = vbWindowBackground
.CellForeColor = vbWindowText
Else
.CellBackColor = &H996633
.CellForeColor = vbWhite
End If
.col = c
If ColorType = 0 Then
.CellBackColor = vbWindowBackground
Else
.CellBackColor = &HFFCC99
End If
End With
End Sub
//显示一个Popup来修改信息。
//我没有采取直接在Grid上修改的方式,而是用一个PictureBox来模拟了一个Popup Window,
//因为要求是从一个List中去选择改产品所属的类别,虽然是单选(好像是),但用ListBox的目的
//可能当时没有完全决定是单选还是多选,所以实现作了个准备,而且操作性、美观性也更高,记不
//清了~~~。
//这个函数就是显示这些信息,前面一段是确定PictureBox的位置,中间是显示信息,最后是如果
//PictureBox超出了窗体的范围,则改为在选定行的上面出现。
Private Sub ShowPop()
Dim lngTop As Long, lngLeft As Long
Dim strName As String, strType As String
Dim i As Integer
With flxData
lngTop = .Top + (.CellHeight + 15) * (intOldRow - .TopRow + 1)
If intOldCol = 0 Or intOldCol = 1 Then
lngLeft = .Left + 45
strName = .TextMatrix(intOldRow, 0)
strType = .TextMatrix(intOldRow, 1)
Else
lngLeft = .Left + 45 + .ColWidth(0) + .ColWidth(1) + .ColWidth(2)
strName = .TextMatrix(intOldRow, 3)
strType = .TextMatrix(intOldRow, 4)
End If
End With
If strName = "" Then
picPop.Visible = False
Else
lblName.Caption = strName
lstType.ListIndex = -1
For i = 0 To lstType.ListCount - 1
If Trim(UCase(lstType.List(i))) = Trim(UCase(strType)) Then
lstType.ListIndex = i
Exit For
End If
Next i
With picPop
If lngTop + .Height > flxData.Height And lngTop - .Height - flxData.CellHeight >= 0 Then lngTop = lngTop -
//EnterCell是Grid的事件,在鼠标或键盘控制当前焦点进入一个单元格时产生。
//注意blnInDo这个变量,是防止重复调用而设置的,因为在这个事件中的一些
//代码(在SetCellCorlor函数中)会让Grid重新产生EnterCell事件。
Private Sub flxData_EnterCell()
Static blnInDo As Boolean
Dim intRow As Integer, intCol As Integer
If flxData.col = 2 Then Exit Sub
If blnInDo Or blnNoEntry Then Exit Sub
blnInDo = True
With flxData
.Redraw = False
intRow = .row
intCol = .col
If intOldRow <> -1 And intOldCol <> -1 Then
SetCellColor intOldRow, intOldCol, 0
End If
SetCellColor intRow, intCol, 1
intOldRow = intRow
intOldCol = intCol
If picPop.Visible Then ShowPop
.Redraw = True
End With
blnInDo = False
End Sub
//Scroll是Grid在滚动的时候产生的事件,关掉Popup Window。
//注意,这是程序里唯一关掉Popup Window的地方,因为我是让这个Popup Window在鼠标或键盘
//在不同的Cell间移动的时候一直显示的。对于一般以Textbox类的需求,可以在TextBox的
//KeyPrees、KeyDown事件中判断是否按下了Escape、Return,是的话则移到下个单元格、或者
//关闭输入。
Private Sub flxData_Scroll()
picPop.Visible = False
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
With Text2
.SetFocus
.SelStart = 0
.SelLength = Len(Text2.Text)
End With
End If
End Sub
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
With Text1
.SetFocus
.SelStart = 0
.SelLength = Len(Text1.Text)
End With
End If
End Sub