请问高手:关于MSFlexGrid里,与TEXT配合使用时如何让它像EXCEL一样可以用回车改变光标位置?

nofound007 2003-10-17 09:08:27
如题,按回车时,可以让光标移动到右边的一个格子里或者往下的格子里,正如EXCEL一样,注意移到另外的格子里要直接可以进行编辑!

同时也可以支持TAB键。

或者有什么其他相当的控件!!
...全文
169 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nofound007 2003-10-19
  • 打赏
  • 举报
回复
多谢楼上的几位兄弟

我想我应该可以解决这些问题了!
nofound007 2003-10-18
  • 打赏
  • 举报
回复
难道这个问题没有人能解决?
AresChen 2003-10-18
  • 打赏
  • 举报
回复
补充一句,因为在我找的过程,有位朋友又回答了你的问题。但他的程序是在两个Textbox间切换光标,就像我们在输入用户名和口令似的,输入完用户名一按回车就把光标自动移到密码的地方。他的程序适合于基本信息类的录入,但不是Grid的需求。
AresChen 2003-10-18
  • 打赏
  • 举报
回复
//改变Grid的大小。
Private Sub ResizeTable()
Dim lngWidth As Long

With flxData
lngWidth = (.Width - SCROLL_BAR_WIDTH - SP_WIDTH) / 4

.ColWidth(0) = lngWidth * 2 - TYPE_CELL_WIDTH
.ColWidth(1) = TYPE_CELL_WIDTH
.ColWidth(2) = SP_WIDTH
.ColWidth(3) = lngWidth * 2 - TYPE_CELL_WIDTH
.ColWidth(4) = .Width - SCROLL_BAR_WIDTH - .ColWidth(0) - .ColWidth(1) - .ColWidth(2) - .ColWidth(3)

.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 -

.Height - flxData.CellHeight Else lngTop = lngTop + 60
.Top = lngTop
.Left = lngLeft

.Visible = True
End With
End If
End Sub

//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
AresChen 2003-10-18
  • 打赏
  • 举报
回复
找到了,见下面的程序,但我的需求和你的不太一样,你看一下吧。
berylw 2003-10-18
  • 打赏
  • 举报
回复
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

稍加修改,应该足够能完成你的要求
AresChen 2003-10-18
  • 打赏
  • 举报
回复
当然是在grid里加。代码???我找找看~~~
yoki 2003-10-18
  • 打赏
  • 举报
回复
建议用vsflexgrid控件
luckspring 2003-10-18
  • 打赏
  • 举报
回复
难道用焦点不能控制吗?
nofound007 2003-10-17
  • 打赏
  • 举报
回复
没有人能提供一点代码吗?
nofound007 2003-10-17
  • 打赏
  • 举报
回复
能否提供一段简单的代码吗?

我很菜的!!

是在TEXT里加入还是在MSFlexGrid里加入?
AresChen 2003-10-17
  • 打赏
  • 举报
回复
自己在keydown事件中加代码,我从来都是这么作的。

7,759

社区成员

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

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