VB將輸入文字框的數字分割並按十六進製制傳送

2022-05-22 21:33:13 字數 3199 閱讀 1471

◆首先文字輸入區域新增如下**,實現將輸入文字按回車符分割,並提前有效數字。

private sub text2_keypress(keyascii asinteger)

『注意選擇文字文件事件的型別

dim tmp() as string

dim aa as string * 1

dim rd as string

dim i as integer

dim reload as integer

dim response

aa = chr$(keyascii)

if aa = chr(13) and pc_draw_flag = truethen

tmp = split(text2.text, chr(13)) '把目標陣列按分隔符chr(13)分割

for i = 0 to ubound(tmp)    'ubound()取陣列內元素的個數

rd = getnum(cstr(tmp(i)))

next i

route(route_num) = "&h"& hex(rd)『提示,此處處理較為重要

route_num = route_num + 1

text8.text = route_num

if route_num = 10 then

response = msgbox("路徑儲存已達上限!是否重新規劃?", vbyesno + vbdefaultbutton2 + vbinformation, "提示資訊!")

if response = vbyes then

route_num = 0

else

msgbox "路徑儲存已完成,傳送按鈕!", vbokonly + vbinformation, "提示資訊!"

end if

end if 

end if

end sub

其中用到了getnum過程,其**如下,起到提取數字並進行連線的作用。

function getnum(sp as string) as string

dim vi as string

for i = 1 to len(sp)

vi = mid(sp, i)

if isnumeric(vi) then

getnum = getnum & vi

exit for

end if

next

end function

◆mscomm1初始化時注意將引數,若設定不正確,則接受會出現問題,其他引數正常設定即可。

mscomm1.inputmode= cominputmodebinary

'重要突破點,vb參考:串列埠控制項的inputmode屬性確定input屬性讀取的資料型別,如果設定inputmode為cominputmodetext,input屬性通過乙個variant返回文字資料,如果設定inputmode為cominputmodebinary,input屬性通過乙個variant返回二進位制資料的陣列,此時方可進行二進位制轉換。

◆然後在mscomm1的事件處理中,做如下處理,注意劃線語句,

select case mscomm1.commevent

case 2

buffer = mscomm1.input

staylonglowpower = staylonglowpower + 1

fori = 0 to ubound(buffer)

if len(hex(buffer(i))) = 1 then

rec_data = rec_data & "0"& hex(buffer(i)) & " "

else

rec_data = rec_data & hex(buffer(i)) & " "

end if

next i

endselect

顯示如下寫即可

text1.text =text1.text + rec_data + " "

當然在txt控制項的change事件裡做如下處理使其支援動態顯示或者說游標總是位於最前位置。

text1.selstart =len(text1)

◆傳送部分如下寫

private subcmdsend_click()

ifmscomm1.portopen = true then

if pc_draw_flag = true and optsendhex.value= true then

mscomm1.output = route『定義的乙個0-9十個byte的陣列

send_num = send_num + 10

text7.text = send_num

end if

if text2.text = "" orpc_draw_flag = false then

msgbox "傳送資料不能為空,請進行路徑規劃或輸入字元!", vbokonly + vbinformation, "提示資訊!"

else

if optsendhex.value = true then

mscomm1.inputmode =cominputmodebinary

mscomm1.inputlen = 0

else

mscomm1.inputmode =cominputmodetext

mscomm1.inputlen = 1

end if

'        mscomm1.output = trim(val(text2.text))

end if

else

msgbox "串列埠沒有開啟,請開啟串列埠!",vbokonly + vbinformation, "提示資訊!"

文字框只能輸入數字

許多時候我們在製作表單時需要限制文字框輸入內容的型別,下面我們用正規表示式限制文字框只能輸入數字 小數點 英文本母 漢字等各類 輸入大於0的正整數 1.文字框只能輸入數字 小數點也不能輸入 keyup this.value this.value.replace d g,on afterpaste t...

jq 文字框只能輸入數字

onkeyup 當輸入值的鍵盤抬起時觸發。value表示此輸入框的值,d g為正規表示式,用來匹配所有非數字字元 此句功能為 將輸入值為非數字的字元替換為空 上面的做法已過時,兩年後再看到這篇文章,又重新想了下這個問題。完善了 2018 06 22 selector為選擇器名稱,如 input na...

文字框限制只能輸入數字

oninput value math.abs this.value 作用 輸出輸入值的絕對值 缺點 輸入小數提示英文 oninput validity.valid value 作用 只能輸入數字 缺點 但是可以輸入小數點和減號 onkeypress return event.charcode 8 e...