Make a text box without echoing in VB

Source: Internet
Author: User

In applications, users are sometimes required to enter content that is not appropriate to be displayed directly on the screen, such as user passwords or confidential data. As we have already known, this feature can be implemented in Foxbase/foxpro with the Setconsoleoff command turned off to the screen echo. So, how do you do that in visualbasicforwin-dows?

An easy way to think is to use the Key-press event of a text box, but in order to handle the user's edits to the text (such as delete, insert), it is necessary to use other variables to store the actual input content, and must write a large number of complex control procedures. Practice has proved that this method is more cumbersome and inconvenient to use.

The author in VB programming found that Windows text box can respond to many messages, then, you can use the WINDOWSAPI function SendMessage control characteristics of the message passed to the text box? If feasible, the message that causes the target to be exported as a password is passed to the text box. Is it possible to make a text box without echoing text?

The answer is yes. The author uses the above ideas to achieve this function. Here are a few API functions and related messages that are used by the program:

GetFocus (): Returns the target handle that gets the input focus;

GetWindowLong (): Gets the information of the specified target, and the second parameter is Gwl_style and returns the "style" property of the target;

SetWindowLong (): Assigns the new attribute value to the specified target. The three parameters of the function are target handle, attribute type and new attribute value respectively;

SendMessage (): Sends a message to the destination. The four parameters of the function are the target handle, message, message parameter, and message parameter two of the receiving message respectively;

Gwl_style (simplified style in the program): The value is-16, which indicates the "style" of the target;

Es_password (in the program for pass) shorthand for pass): its value is &H20, indicating that the target has input password attributes, that is, when the input does not echo;

Em_setpasswordchar (abbreviated as Passchar in the program): The value is &h41c, which indicates the pass-through password attribute. Passing this message to the target with SendMessage is successful, the target gives its output information in the specified password form.

The following program, through the SendMessage function, modifies the "style" property of the text box Text1 to output in a specified password ——— regardless of what the user is typing, it is displayed in the same number of "*". The user can display the real content of Text1.Text through the single point "Command1" button.

Declare Function getfocus Lib"user"() As Integer
Declare Function getwindowlong Lib "user"(ByVal hand As Integer,ByVal index
As Integer) As Long
Declare Function setwindowlong Lib "user"(ByVal hand As Integer,ByVal index
As Integer,ByVal newflag As Long) As Long
Declare Function sendmessage Lib"user"(ByVal hand As Integer,ByVal msg As
Integer,ByVal wpara As Integer,ByVal lpara As Long) As Long
ConstSTYLE=-16
ConstPASS=&H20
ConstPASSCHAR=&H41C
Sub Command1_Click()
MsgBox text1.Text
End Sub
Sub Command2_Click()
End
End Sub
Sub Form_Load()
Show
text1.SetFocus
hand%=getfocus()
flag&=getwindowlong(hand%,STYLE)OrPASS
flag&=setwindowlong(hand%,STYLE,flag&)
flag&=sendmessage(hand%,PASS-CHAR,Asc("*"),0&)
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.