多个文本框,下拉MSHFLEXGRID时控制文本框的visible.你的跟贴:
1.建议利用scroll事件,让文本框可以跟随网格一起滚动
2.利用leftcol,toprow属性,控制文本框的visible
3.我倾向于用colpos和rowpos属性给文本框定位,因为利用这两个属性不需要移动单元格给个实例,好吗?谢谢!
1.建议利用scroll事件,让文本框可以跟随网格一起滚动
2.利用leftcol,toprow属性,控制文本框的visible
3.我倾向于用colpos和rowpos属性给文本框定位,因为利用这两个属性不需要移动单元格给个实例,好吗?谢谢!
这样的标题我会被BS的Private Sub Form_Load()
Dim i As Long, j As Long
With MSFlexGrid1
For i = 0 To .Rows - 1
.TextMatrix(i, 0) = CStr(i)
Next
For j = 0 To .Cols - 1
.TextMatrix(0, j) = CStr(j)
Next
End With
Text1.Tag = "3,5" '文本框定位在3行5列
Text1.BackColor = vbYellow
Text2.Tag = "4,6" '文本框定位在4行6列
Text2.BackColor = vbYellow
Call SetPos(Text1, MSFlexGrid1)
Call SetPos(Text2, MSFlexGrid1)
Call SetVisible(Text1, MSFlexGrid1)
Call SetVisible(Text2, MSFlexGrid1)
End SubPrivate Sub SetPos(txtText As TextBox, flgGrid As MSFlexGrid)
Dim r As Long, c As Long
Dim x As Variant
x = Split(txtText.Tag, ",")
r = CLng(x(0))
c = CLng(x(1))
With flgGrid
txtText.Move .Left + .ColPos(c) + 20, .Top + .RowPos(r) + 30, .ColWidth(c), .RowHeight(r)
End With
End Sub
Private Sub SetVisible(txtText As TextBox, flgGrid As MSFlexGrid)
Dim r As Long, c As Long
Dim x As Variant
x = Split(txtText.Tag, ",")
r = CLng(x(0))
c = CLng(x(1))
With flgGrid
If .RowIsVisible(r) And .ColIsVisible(c) Then
txtText.Visible = True
Else
txtText.Visible = False
End If
End With
End SubPrivate Sub MSFlexGrid1_Scroll()
Call SetPos(Text1, MSFlexGrid1)
Call SetPos(Text2, MSFlexGrid1)
Call SetVisible(Text1, MSFlexGrid1)
Call SetVisible(Text2, MSFlexGrid1)
End Sub
你的程序用的是控件数组,我用了两个文本框,该起来也很容易这段代码每个文本框都被固定在了指定的行列,
说是在的我没有这么用过,我都是用一个文本框在不同的行之间移动,编辑哪一行移动到哪一行有一个缺陷:文本框滚动到最右边的时候,如果最右边的一列只能显示一半的话,文本框会从右边冒出来一点,呵呵
但如果是控件数组 控制还是会出错! 并且如果水平移动滚动条不能正确移动并且如下程序运行时会触发Scroll事件[如果数组较大,如40]
Dim i As Integer
Dim objTxt() As TextBox
Dim objOpt() As CheckBoxReDim objTxt(Fg2.Rows - 1)
ReDim objOpt(Fg2.Rows - 1)For i = 1 To Fg2.Rows - 1 Fg2.row = i
Set objTxt(i - 1) = Me.Controls.Add("vb.textbox", "txt" & i) '增加文本框
With objTxt(i - 1)
Fg2.Col = 8
.Text = Fg2
.Move Fg2.Left + Fg2.CellLeft, Fg2.Top + Fg2.CellTop, Fg2.CellWidth, Fg2.CellHeight
.Alignment = 2
.BackColor = vbYellow
.Tag = Fg2.row & "," & Fg2.Col
.Visible = True
.ZOrder 0
End With
Next i
水平滚动也没有错误
Private Sub Form_Load()
Dim i As Long, j As Long
With MSFlexGrid1
For i = 0 To .Rows - 1
.TextMatrix(i, 0) = CStr(i)
Next
For j = 0 To .Cols - 1
.TextMatrix(0, j) = CStr(j)
Next
End With
Text1(0).Tag = "3,5" '文本框定位在3行5列
Text1(1).Tag = "4,6" '文本框定位在4行6列
Text1(2).Tag = "5,3" '文本框定位在5行3列
For i = 0 To Text1.Count - 1
Call SetPos(Text1(i), MSFlexGrid1)
Call SetVisible(Text1(i), MSFlexGrid1)
Text1(i).BackColor = vbYellow
Next
End SubPrivate Sub SetPos(txtText As TextBox, flgGrid As MSFlexGrid)
Dim r As Long, c As Long
Dim x As Variant
x = Split(txtText.Tag, ",")
r = CLng(x(0))
c = CLng(x(1))
With flgGrid
txtText.Move .Left + .ColPos(c) + 20, .Top + .RowPos(r) + 30, .ColWidth(c), .RowHeight(r)
End With
End Sub
Private Sub SetVisible(txtText As TextBox, flgGrid As MSFlexGrid)
Dim r As Long, c As Long
Dim x As Variant
x = Split(txtText.Tag, ",")
r = CLng(x(0))
c = CLng(x(1))
With flgGrid
If .RowIsVisible(r) And .ColIsVisible(c) Then
txtText.Visible = True
Else
txtText.Visible = False
End If
End With
End SubPrivate Sub MSFlexGrid1_Scroll()
Dim i As Long
For i = 0 To Text1.Count - 1
Call SetPos(Text1(i), MSFlexGrid1)
Call SetVisible(Text1(i), MSFlexGrid1)
Next
End Sub=======================================
Fg2.Col = 8 这句话移动了当前单元格,很可能触发Scroll事件
如果用colpos而不是cellLeft属性来定位文本框,就不需要改变当前单元格,也不会触发Scroll事件
=============================================
dim nCol as long ,nRow as long
dim i as longwith grid
for i=.TopRow to .Rows-1
if not .RowIsVisible(i) then
exit for
else
nRow=nRow+1
end if
next for i=.LeftCol to .Cols-1
if not .ColIsVisible(i) then
exit for
else
nCol=nCol+1
end if
next
end withdebug.Print "可见行:";cstr(nRow);"可见列:";cstr(nCol)
==================================
大就大呗,有什么问题?
位于不可见行列的控件会被设置visible=false
移植到我的程序中,跟MSHFLEXGRID结合就错误!头都搞大了!明日在加100分! 谢了!P.S 可以留个QQ吗?
不过因为默认的列宽是-1,所以需要初始化列宽
For j = 0 To .Cols - 1
.TextMatrix(0, j) = CStr(j)
.ColWidth(j) = 1000
Next