Diffuse text in a Textbox

1

Index

Stats

4,314 visits, 6,808 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 15 Dec, 2011. Last updated 19 Feb, 2019

1. First, after I have created the textbox that I want to diffuse (I call it "Password_Textbox"), I set a couple of global values and a text change event in the event sheet:

Global variables:

- DiffusedText (Text) is where I keep the diffused text. This is basicly just a bunch of * in a row. One star represent one character, so if you type in 10 characters, you will have 10 in this variable.

- ClearText (Text) is where I keep the orginal text. This text is exactly how it was entered into the textbox.

- PasswordLength (Number) Is a value I use to check how long the inputted text is, so I can create the correct amount of diffused characters for the textbox.

As a only Event, I use the "on text changed" Event for the textbox.

So when the user is adding or removing text from the textbox, the Text diffuse function will be called, so that the text is instantly diffused.

When the Diffuse text function is done, I take the DiffusedText variable and I set it to the Textbox, replacing what the user has written with the correct amount of diffuse characters.

2.

The main "Diffuse Text" function, looks like this:

Password_Textbox.Text = ""

- The first control I make is to check if the textbox is empty or not.

If its empty because the user has removed all letters, I want to make sure its cleared of all text to avoid errors.

Password_Textbox.Text != ""

len(Password_Textbox.Text) > PasswordLength

- In the next control, if the textbox is not empty, and the user has entered a character, I take the last character from the textbox and I store it in a temp local variable called TempStr (Text). I take the last character from the textbox, because you can only add one character and then this function is called, so there is no need to check for more characters than one at the time, making it a lot simpler.

To get the last character, I use the following code:

    mid(String, StartIndex, Length)
    len(string)

mid is a string function, that returns a number of characters from a sting, from the StartIndex until the length added (ie one character is 1, two characters 2 etc). len is a string function, that returns the length of a string.

When I have recieved the newly added character, I add this to the ClearText variable. I never store any text in the textbox since it will be overwritten anyway.

Password_Textbox.Text != ""

len(Password_Textbox.Text) < PasswordLength

- The next check is valid if the user has removed a single character from the textbox, for instance using the backspace button.

To remove the last character from the ClearText variable, I use the following code:

    left(String, Length)
    len(String)

This is a string function, that returns a number of characters from the beginning of the string. Here I use the length of the ClearText variable MINUS one (since I want to remove one character), and I set the result in the same ClearText variable. So now the Text is one character shorter!

- Between the two checks, I set everytime two variables.

First I set the password length variable, by checking how long the textbox text is, using the following code:

    len(String)

And then to be on the safe side, I set the DiffusedText variable to nothing (or ""). This is to avoid getting extra characters in the end.

Now for the last check!

For "" from 1 to PasswordLength

Password_Textbox != ""

- This is done everytime the function is called.

I want to run this for loop for each character there is in the textbox as long as the textbox is not empty. I do this so I can replace all characters with a diffuse character. This diffuse character can be changed in to any kind of keyboard character.

And thats all I need to do inside the Diffuse Text function.

Now I have the orginal text, and I have the diffused version of the text. All I need to do now, is to replace the existing text inside the textbox with the diffused text.

Here is a simple project example:

http://dl.dropbox.com/u/52716812/Diffuse_Textbox.capx

  • 0 Comments

Want to leave a comment? Login or Register an account!