假如Form1有個TextBox1,雙擊TextBox1填入123
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供仙桃網(wǎng)站建設(shè)、仙桃做網(wǎng)站、仙桃網(wǎng)站設(shè)計(jì)、仙桃網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、仙桃企業(yè)網(wǎng)站模板建站服務(wù),10余年仙桃做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
Public Class Form1
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.DoubleClick
System.Windows.Forms.SendKeys.Send("123")
End Sub
End Class
看你的VB.NET 版本了
用 My.Computer.Keyboard.SendKeys("123", True) 代替 System.Windows.Forms.SendKeys.Send("123") 也可以
原碼:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.DoubleClick
My.Computer.Keyboard.SendKeys("123", True)
End Sub
End Class
'使用VBS例,下載dynwrap.dll,然后拷貝至c:\windows\system32,運(yùn)行regsvr32 dynwrap.dll進(jìn)行注冊
Set ws=CreateObject("WSCript.shell")
Set Wrap1 = CreateObject("DynamicWrapper")
Wrap1.Register "USER32.DLL", "FindWindow","I=ss","f=s", "R=l"
Wrap1.Register "USER32.DLL", "SetForegroundWindow", "I=h", "f=s", "R=l"
hWin = Wrap1.FindWindow(vbNullString, CStr("窗口名稱"))
if hWin 0 then
Clipboard = "MsHta vbscript:ClipBoardData.setData(""Text"",""" "您要輸入的文本" """)(Close)"
ws.Run(Clipboard) '復(fù)制文本
ws.sendkeys "{tab}" '發(fā)送tab鍵移動焦點(diǎn)
ws.sendkeys "^v" '發(fā)送粘貼,確保剪切板有內(nèi)容
end if
之前l(fā)oad的一個VB的打字程序的一段, 思路和語言跟C#差不多,你借鑒一下吧
Private Sub txtType_Change()
Dim Start As Integer, i As Integer
On Error Resume Next
Start = txtType.SelStart
'If DelTF = True And Len(txtType.Text) = 1 Then txtType.Text = ""
Hang = Num \ 44 '打完一行,就開始打下一行(計(jì)算現(xiàn)在應(yīng)該打第幾行)
'If Hang = 8 Then Hang = 0
Hang = Hang Mod 8 'Hang始終都是0-7的順序下去
No = Num Mod 44 '每一行的第幾個
If Start = 0 Or DelTF = True Then Exit Sub '如果在KeyDown中按下刪除鍵則退出Chang
'If Hang = 3 And No = (44 - 5) Then '到第4行倒數(shù)第6個,則第1行隱藏,第8行現(xiàn)身
' lblEgg(0).Caption = ""
' 'lblegg(0).Visible=False '可能不需要隱藏,因?yàn)槭峭该髂J?,沒有文字效果與隱藏一樣
' For i = 1 To 44 '卸掉現(xiàn)在行的前面第3行,中間隔著2行 例:(44*3+39)=171,終止:171-44*2-39,開始:171-44*3-39+1
' Unload lblOK(i) '卸掉第1行的標(biāo)示對錯的顏色標(biāo)簽,節(jié)約內(nèi)存
' Next '實(shí)時錯誤'340',控件數(shù)組元素'1'不存在,卸過一次后又再卸
' lblEgg(7).Caption = Mid$(StrText, mNumber * 44 + 1, 44) '繼續(xù)的文字內(nèi)容(后面第4行,如果例現(xiàn)在是第6行,后面剩2行,后第4行就越過算到正數(shù)第2行
'End If
'If Hang = 3 And No = (44 - 6) Then '(44 - 5),上面示例舊文本的消失與顯示新文本
If lblEgg((Hang + 4) Mod 8).Caption = "" And No = (44 - 6) Then
'If lblEgg(Hang - 3).Caption "" Then '檢測前面第3行是否已經(jīng)空掉,因?yàn)榕轮貜?fù),如打過第4行第40個,然后又刪除,然后再打會重復(fù),導(dǎo)致后面第4行的內(nèi)容會變化
If lblEgg((Hang + 5) Mod 8).Caption "" Then
lblEgg((Hang + 5) Mod 8).Caption = "" ''lblEgg((Hang - 3) Mod 8).Caption = ""
For i = Num - 44 * 3 - 39 + 1 To Num - 44 * 2 - 39 + 1
Unload lblOK(i)
Next
lblEgg((Hang + 4) Mod 8).Caption = Mid$(StrText, mNumber * 44 + 1, 44) '(Hang + 4) Mod 7
mNumber = mNumber + 1
End If
End If
If Hang = 6 And No = (44 - 6) Then TypeSPace = True '模擬打字區(qū)的舊文本消失,當(dāng)打到第6行(倒數(shù)第2行),第0行開始消失,Hang-6(前第6行),Hang+2(后第2行)
If lblType((Hang + 2) Mod 8).Caption "" And TypeSPace Then
'TypeHang = Hang + 2
'if typehang=8 then typehang=0
'lblType(Hang - 6).Caption = ""
lblType((Hang + 2) Mod 8).Caption = ""
End If
lblType(Hang).Caption = lblType(Hang).Caption Mid$(txtType.Text, Len(txtType.Text), 1) 'txtType.Text
'Debug.Print txtType.SelStart
'Debug.Print Num
Num = Num + 1
Load lblOK(Num)
If Mid$(lblType(Hang).Caption, Len(lblType(Hang).Caption), 1) = Mid$(lblEgg(Hang).Caption, Len(lblType(Hang).Caption), 1) Then
lblOK(Num).BackColor = vbGreen '正確就用綠色,65280
lblOK(Num).Tag = 1
mTrue = mTrue + 1 '正確的字?jǐn)?shù)
lblTrue.Caption = "對:" mTrue
Else
lblOK(Num).BackColor = vbRed '錯誤就用紅色,255
lblOK(Num).Tag = 0
mFalse = mFalse + 1 '錯誤的字?jǐn)?shù)
lblFalse.Caption = "錯:" mFalse
End If
'算出正確率
mTF = mTrue / (mTrue + mFalse) * 1000
lblTF.Caption = mTF
With lblOK(Num)
If Num Mod 44 = 1 Then
'Select Case Num
'Case 44 * 1 + 1
'Hang = Num \ 44 '打完一行,就開始打下一行
.Left = 50
Else
.Left = lblOK(Num - 1).Left + lblOK(Num - 1).Width '上一個之后
End If
If (Num - 1) \ 44 = 1 Then '如果不是第一行,每行44個字
'.Top = 50 + (Num - 1) \ 30 * 180 'TextHeight(Mid$(txtType.Text, Len(lblType.Caption), 1))
'.Top = lblOK(Num - 30).Top + lblOK(Num - 30).Height ' 180 + 60
.Top = lblEgg(Hang).Top
Else
.Top = 0 '50
End If
.Caption = Mid$(lblEgg(Hang).Caption, Len(lblType(Hang).Caption), 1)
.AutoSize = True
.Visible = True
End With
Line1.X1 = lblOK(Num).Left + lblOK(Num).Width: Line1.X2 = lblOK(Num).Left + lblOK(Num).Width
Line1.Y1 = lblOK(Num).Top: Line1.Y2 = lblOK(Num).Top + 250
If Num Mod 44 = 0 And Num 0 Then 'Hang 0
Line1.X1 = lblType(Hang + 1).Left - 1: Line1.X2 = lblType(Hang + 1).Left - 1
Line1.Y1 = lblType(Hang + 1).Top: Line1.Y2 = lblType(Hang + 1).Top + 250
End If
End Sub
在TextChanged事件中:privatevoidrichTextBox1_TextChanged(objectsender,EventArgse)
{
if(label1.Text==richTextBox1.Text)
richTextBox1.ForeColor=Color.Black;
else
richTextBox1.ForeColor=Color.Red;
}-------------------繼續(xù)答復(fù):
首先,還是建議你使用TextChange方法。KeyPress方法只有在松開鍵后才觸發(fā),如果一直按著鍵比如寫a,那這串a(chǎn)顏色不會變的,直到你松手。
然后上代碼,測試可用的,你直接放到KeyPress里也能用:
PrivateSubrichTextBox1_TextChanged(senderAsObject,eAsEventArgs)
ForiAsInteger=0TorichTextBox1.TextLength-1
richTextBox1.SelectionStart=i
richTextBox1.SelectionLength=1
Ifi=label1.Text.LengthThen
richTextBox1.SelectionColor=Color.Blue
ElseIflabel1.Text(i)richTextBox1.Text(i)Then
richTextBox1.SelectionColor=Color.Red
Else
richTextBox1.SelectionColor=Color.Black
EndIf
Next
richTextBox1.SelectionLength=0
richTextBox1.SelectionStart=richTextBox1.TextLength
EndSub這里寫錯的是紅色,多寫出來的是藍(lán)色,具體你自己可以調(diào)整。
順便鄙視一下那個把我代碼直接轉(zhuǎn)VB貼上來的,我直接用工具轉(zhuǎn)成VB了,方便樓主參考。
有問題可以繼續(xù)追問。