vb中如何读取textbox中某一行内容?
在textbox中已经输入了学号200612001200612002200612003200612004200612005……200612078请问如何随机读取其中的某一...
在textbox中已经输入了学号
200612001
200612002
200612003
200612004
200612005
……
200612078
请问如何随机读取其中的某一行? 展开
200612001
200612002
200612003
200612004
200612005
……
200612078
请问如何随机读取其中的某一行? 展开
4个回答
展开全部
最好是放在listbox中,方便,如果你非要用textbox,你可以尝试下面的代码,文本最后不能有空行,否则会有空白:
参考
Private Sub Command1_Click()
Dim a
Dim i As Integer
a = Split(Trim(Text1.Text), vbCrLf)
Randomize
i = Int(Rnd * (UBound(a) - LBound(a)) + 1) + LBound(a)
Print a(i)
End Sub
参考
Private Sub Command1_Click()
Dim a
Dim i As Integer
a = Split(Trim(Text1.Text), vbCrLf)
Randomize
i = Int(Rnd * (UBound(a) - LBound(a)) + 1) + LBound(a)
Print a(i)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
'读取指定行的函数,用法 【?】=getline(【源数据】,【行号(可以多选)】)
Function getline(ByVal s As String, ParamArray a())
Dim i As Integer, st(1 To 1000) As String, s1 As String, s2 As String, rnum As Integer, v
s1 = ""
rnum = 0
For i = 1 To Len(s)
If Mid(s, i, 1) <> Chr(13) Then
s1 = s1 + Mid(s, i, 1)
Else
rnum = rnum + 1
st(rnum) = s1
s1 = ""
End If
Next i
rnum = rnum + 1
st(rnum) = s1
s1 = ""
For Each v In a
s1 = s1 + st(v)
Next v
getline = s1
End Function
Function getline(ByVal s As String, ParamArray a())
Dim i As Integer, st(1 To 1000) As String, s1 As String, s2 As String, rnum As Integer, v
s1 = ""
rnum = 0
For i = 1 To Len(s)
If Mid(s, i, 1) <> Chr(13) Then
s1 = s1 + Mid(s, i, 1)
Else
rnum = rnum + 1
st(rnum) = s1
s1 = ""
End If
Next i
rnum = rnum + 1
st(rnum) = s1
s1 = ""
For Each v In a
s1 = s1 + st(v)
Next v
getline = s1
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
分二步:
一、建立一个模块,复制下面代码:
Option Explicit
Const EM_GETLINE = &HC4
Const EM_LINELENGTH = &HC1
Const EM_LINEINDEX = &HBB
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Sub TB_GetLine(ByVal hWnd As Long, ByVal whichLine As Long, Line As String)
Dim length As Long, bArr() As Byte, bArr2() As Byte, lc As Long
lc = SendMessage(hWnd, EM_LINEINDEX, whichLine, ByVal 0&)
length = SendMessage(hWnd, EM_LINELENGTH, lc, ByVal 0&)
If length > 0 Then
ReDim bArr(length + 1) As Byte, bArr2(length - 1) As Byte
Call RtlMoveMemory(bArr(0), length, 2)
Call SendMessage(hWnd, EM_GETLINE, whichLine, bArr(0))
Call RtlMoveMemory(bArr2(0), bArr(0), length)
Line = StrConv(bArr2, vbUnicode)
Else
Line = ""
End If
End Sub
二、调用举例:
Dim S As String
Call TB_GetLine( Text1.hWnd, 5, S )
' 传回值 S 即等于到 5 行的资料
注:TextBox 的行次是从 0 起算。
一、建立一个模块,复制下面代码:
Option Explicit
Const EM_GETLINE = &HC4
Const EM_LINELENGTH = &HC1
Const EM_LINEINDEX = &HBB
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Sub TB_GetLine(ByVal hWnd As Long, ByVal whichLine As Long, Line As String)
Dim length As Long, bArr() As Byte, bArr2() As Byte, lc As Long
lc = SendMessage(hWnd, EM_LINEINDEX, whichLine, ByVal 0&)
length = SendMessage(hWnd, EM_LINELENGTH, lc, ByVal 0&)
If length > 0 Then
ReDim bArr(length + 1) As Byte, bArr2(length - 1) As Byte
Call RtlMoveMemory(bArr(0), length, 2)
Call SendMessage(hWnd, EM_GETLINE, whichLine, bArr(0))
Call RtlMoveMemory(bArr2(0), bArr(0), length)
Line = StrConv(bArr2, vbUnicode)
Else
Line = ""
End If
End Sub
二、调用举例:
Dim S As String
Call TB_GetLine( Text1.hWnd, 5, S )
' 传回值 S 即等于到 5 行的资料
注:TextBox 的行次是从 0 起算。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
兄弟,你把这些数据放到listbox中不是就没有这些问题了吗?一切都变得很容易了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询