怎样把Textbox中输入的字母转换成大写?

shgz 2000-06-21 07:40:00
怎样把Textbox中输入的字母转换成大写?(也就是说无论输入大小字母,写显示的都是小写)
...全文
583 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wingsun 2000-06-22
  • 打赏
  • 举报
回复
我的方法是使用WindowsAPI修改TextBox的属性,将他的行为改成所有的输入显示成大写。你可以在MSDN中查询到关于Edit控件的相关资料,Edit的Style属性决定了Edit的行为特性,使用不同的参数可以使得Edit有不同的行为特性。Edit就是TextBox.具体的可以查看MSDN。使用Windows API一次修改TextBox的属性,也就不用再他的事件中编写代码了。
Janven 2000-06-21
  • 打赏
  • 举报
回复
看你的意思,是需要即时转换,
如果只需输入转换,加上下面的语句就可以实现大小写即时转换了,
若粘贴得内容也得转换。那么在Text1_Change 中还得加入相应的转化语句

’大写转小写
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 65 And KeyAscii <= 90 Then
KeyAscii = KeyAscii + 32

End If
End Sub

’小写转大写
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 97 And KeyAscii <= 123 Then
KeyAscii = KeyAscii - 32
End If
End Sub

Janven 2000-06-21
  • 打赏
  • 举报
回复
看你的意思,是需要即时转换,
如果只需输入转换,加上下面的语句就可以实现大小写即时转换了,
若粘贴得内容也得转换。那么在Text1_Change 中还得加入相应的转化语句

’大写转小写
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 65 And KeyAscii <= 90 Then
KeyAscii = KeyAscii + 32

End If
End Sub

’小写转大写
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 97 And KeyAscii <= 122 Then
KeyAscii = KeyAscii - 32
End If
End Sub

Wingsun 2000-06-21
  • 打赏
  • 举报
回复
使用Windows API,如下:
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long


Private Const GWL_STYLE = (-16)

Private Const ES_UPPERCASE = &H8&
Private Const GWL_EXSTYLE = (-20)

Private Sub Form_Load()
Dim OldStyle As Long
OldStyle = GetWindowLong(Text1.hwnd, GWL_STYLE)
OldStyle = OldStyle Or ES_UPPERCASE
SetWindowLong Text1.hwnd, GWL_STYLE, OldStyle
End Sub
Wonny 2000-06-21
  • 打赏
  • 举报
回复
UCase(...)
mxp 2000-06-21
  • 打赏
  • 举报
回复
用Lcase 和 Ucase 转换大小写是很好的主意
再加一句 Text1.SelStart = Len(Text1.text)
将光标移动到最尾

另外:

转换大小写以前别人写过另一种做法
小写
Private Sub Text1_Change()
Dim I As Integer
Dim lngChar As Long
Dim strTemp As String

For I = 1 To Len(Text1.Text)
lngChar = Asc(Mid(Text1.Text, I, 1))
If lngChar >= 65 And lngChar <= 90 Then
strTemp = strTemp & Chr(lngChar + 32)
Else
strTemp = strTemp & Chr(lngChar)
End If
Next

Text1.Text = strTemp
end sub
是不是也挺灵活!

转换大写:
Private Sub Text1_Change()
Dim I As Integer
Dim lngChar As Long
Dim strTemp As String

For I = 1 To Len(Text1.Text)
lngChar = Asc(Mid(Text1.Text, I, 1))
If lngChar >= Asc("a") And lngChar <= Asc("z") Then
strTemp = strTemp & Chr(lngChar - 32)
Else
strTemp = strTemp & Chr(lngChar)
End If
Next

Text1.Text = strTemp
Text1.SelStart = Len(strTemp)
End Sub
mafangsan 2000-06-21
  • 打赏
  • 举报
回复
Private Sub Text1_Change()
Text1.Text = LCase(Text1.Text)
End Sub
shgz 2000-06-21
  • 打赏
  • 举报
回复
用mafangsan 和ml_zhang 的方法为什么光标会在最前面? 谁能告诉我? Wingsun 的方法我看不懂。




ml_zhang 2000-06-21
  • 打赏
  • 举报
回复
你们这帮人搞这么麻烦干吗,怕显不出自己水平?别忘了编程的宗旨:简单+实用。
我很赞成mafangsan的方法,微软提供了那么多现成的函数,不用才傻瓜呢。
再一个简单的方法:
Private Sub Text1_Change()
Text1.Text = Format(Text1.Text,">") '转大写
End Sub
小写嘛,呵呵

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧