重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
假如Form1有个TextBox1,双击TextBox1填入123
创新互联公司专业为企业提供仙桃网站建设、仙桃做网站、仙桃网站设计、仙桃网站制作等企业网站建设、网页设计与制作、仙桃企业网站模板建站服务,10余年仙桃做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
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,运行regsvr32 dynwrap.dll进行注册
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) '复制文本
ws.sendkeys "{tab}" '发送tab键移动焦点
ws.sendkeys "^v" '发送粘贴,确保剪切板有内容
end if
之前load的一个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 '打完一行,就开始打下一行(计算现在应该打第几行)
'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行倒数第6个,则第1行隐藏,第8行现身
' lblEgg(0).Caption = ""
' 'lblegg(0).Visible=False '可能不需要隐藏,因为是透明模式,没有文字效果与隐藏一样
' For i = 1 To 44 '卸掉现在行的前面第3行,中间隔着2行 例:(44*3+39)=171,终止:171-44*2-39,开始:171-44*3-39+1
' Unload lblOK(i) '卸掉第1行的标示对错的颜色标签,节约内存
' Next '实时错误'340',控件数组元素'1'不存在,卸过一次后又再卸
' lblEgg(7).Caption = Mid$(StrText, mNumber * 44 + 1, 44) '继续的文字内容(后面第4行,如果例现在是第6行,后面剩2行,后第4行就越过算到正数第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行是否已经空掉,因为怕重复,如打过第4行第40个,然后又删除,然后再打会重复,导致后面第4行的内容会变化
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 '模拟打字区的旧文本消失,当打到第6行(倒数第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 '正确的字数
lblTrue.Caption = "对:" mTrue
Else
lblOK(Num).BackColor = vbRed '错误就用红色,255
lblOK(Num).Tag = 0
mFalse = mFalse + 1 '错误的字数
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;
}-------------------继续答复:
首先,还是建议你使用TextChange方法。KeyPress方法只有在松开键后才触发,如果一直按着键比如写a,那这串a颜色不会变的,直到你松手。
然后上代码,测试可用的,你直接放到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这里写错的是红色,多写出来的是蓝色,具体你自己可以调整。
顺便鄙视一下那个把我代码直接转VB贴上来的,我直接用工具转成VB了,方便楼主参考。
有问题可以继续追问。