文字框屬性為允許多行顯示時,由於是軟回車實現的分行,無法用split(text1.text,vbcrlf)準確地取出指定行的內容。本文利用sendmessage 系列函式,通過傳送文字框訊息,實現了獲取包含指定字串的行,並演示了如何獲取文字框中文字總行數和任意指定行的文字內容。
'add a textbox with "multiline=true","scrollbars=2".
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 function sendmessagebynum lib "user32" _
alias "sendmessagea" (byval hwnd as long, byval wmsg as long, _
byval wparam as long, byval lparam as long) as long
private declare function sendmessagebystring lib "user32" alias _
"sendmessagea" (byval hwnd as long, byval wmsg as long, byval wparam _
as long, byval lparam as string) as long
private const em_lineindex = &hbb
private const em_getlinecount = &hba
private const em_getline = &hc4
private const em_linelength = &hc1
function getlinetext(byval txtbox as textbox, byval lineindex as long) as string '返回指定行的內容
dim lc as long, linechar as long
linechar = sendmessagebynum(txtbox.hwnd, em_lineindex, lineindex, 0)
lc = sendmessagebynum(txtbox.hwnd, em_linelength, linechar, 0) + 1
getlinetext = string(lc + 2, 0)
mid(getlinetext, 1, 1) = chr(lc and &hff)
mid(getlinetext, 2, 1) = chr(lc / &h100)
lc = sendmessagebystring(txtbox.hwnd, em_getline, lineindex, getlinetext)
getlinetext = left(getlinetext, lc)
end function
function getlinewithstr(byval txtbox as textbox, byval mystr as string) as string
dim linecount as long, temp() as string, i as long
linecount = sendmessage(txtbox.hwnd, em_getlinecount, 0, 0) '返回行數
redim temp(1 to linecount)
for i = 1 to linecount
temp(i) = "第" & i & "行:" & getlinetext(txtbox, i - 1) '新增行號
next
getlinewithstr = join(filter(temp, mystr), vbcrlf) ' 字串過濾
erase temp
end function
private sub command1_click()
msgbox getlinewithstr(text1, "csdn"), 0, "包含「csdn」的行"
end sub
private sub form_load()
dim a(25) as string, i as long
for i = 0 to 25
a(i) = string(50, chr(i + 97))
next
text1.text = join(a, "csdn")
end sub
如何取得文字框中包含指定字串的行
文字框屬性為允許多行顯示時,由於是軟回車實現的分行,無法用split text1.text,vbcrlf 準確地取出指定行的內容。本文利用sendmessage 系列函式,通過傳送文字框訊息,實現了獲取包含指定字串的行,並演示了如何獲取文字框中文字總行數和任意指定行的文字內容。add a textb...
轉換文字框字串為數字
try catch 只能輸入數字 0 9 只能輸入n位的數字 d 只能輸入至少n位的數字 d 只能輸入m n位的數字 d 只能輸入零和非零開頭的數字 0 1 9 0 9 只能輸入有兩位小數的正實數 0 9 0 9 只能輸入有1 3位小數的正實數 0 9 0 9 只能輸入非零的正整數 1 9 0 9 ...
JS 判斷字串中包含指定字元
1 使用includes 方法 es6 2 使用indexof 方法 es5或更老的版本 3 正規表示式 test 4 正規表示式 match includes 方法是es6中引入的,但需要注意的是不支援ie瀏覽器。實現 var str let s test substr test str.incl...