How to lock a text box to a specific amount of characters - c#

I'm making a little password generator and I want it to lock the output text box to 10 characters, so that when the user clicks the password genenerate button it does not just continue with an endless line of randomly generated passwords.
How can I do this?

There's a control on the text box in Visual Studio.
If you click the button and they go into properties there should be something called "max length". Set that to however long you want it.

You can use Substring() function to cut the extra characters in your generated password.
For an instance
string GeneratedPassword = "1234567891345678";
textBox1.Text = GeneratedPassword.Substring(0,10);
Also the textBox1.MaxLength = 10 will not give you the desired result. It will only limit input of the character manually in the textbox. It will not limit input of characters programmatically.

Related

Dynamic Interpretation of Regular Expressions

I have this problem where I have created a User Control which mimics a keyboard to use on an application running on a touchscreen kiosk. The keyboard can be passed a regular expression and has an 'Enter' button that when clicked will validate the input, however I was wondering whether there is a way to dynamically validate the input as it is going in and disable certain keys dependent on whether it is a valid character for the next input.
To give some context, if I pass the keyboard the regex for a UK postcode, and the user has typed 2 letters, I would then like the keyboard to disable all other letters and only enable numbers etc.
Thanks in advance.
here is what I would try.
have an onKeyUp javascript function that checks the field for letters. if it contains the 2 letters then disable the letters with javascript and change the javascript function associated the enter button (let's call this function CheckUKPostCode). In CheckUKPostCode you would validate for a UK PostCode. If the user starts out entering numbers the onKeyUp function would disable the letters and change the function associated with the enter button to USPostCode or whatever it is you need and then this function would validate the US Post Code
You might try dynamically creating a RegularExpressionValidator Control and adding it to the Page's control collection on Page_Load()
var regexValidator = new RegularExpressionValidator
{
ControlToValidate = "keyboardTexBox",
ValidationExpression =
#"^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$"
};

Add text bullets to a C# form

I am creating a form in C# and need to display text on the form. I need some of the text to show up in a bulleted, unordered list. Is it possible to do this while using a label? Or a rich text box? I am not using ASP.NET and this is for a desktop app.
My message should look like this:
To continue please selected one of the actions below:
Click ButtonA to do this action.
Click ButtonB to do this action.
Thanks!
Just to add as a suggestion to the OP, I have found that using a StringBuilder to construct multi-line messages helps me keep the formatting straight:
StringBuilder messageBuilder = new StringBuilder(200);
messageBuilder.Append("To continue, please select one of the actions below:");
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append("\t\u2022 Click Button A to do this action.");
messageBuilder.Append(Environment.NewLine);
messageBuilder.Append("\t\u2022 Click Button B to do this action.");
MessageBox.Show(messageBuilder.ToString());
You can use ASCII or UniCode characters, and reformat your string to replace a marker character with the formatting character.
Say you defined your string like so:
var myMessage = "To continue please selected one of the actions below: * Click ButtonA to do this action. * Click ButtonB to do this action."
you could do a Regex.Replace that looked for asterisks and converted to bullets:
var formattedString = Regex.Replace(myMessage, "\*", "\r\n \u2022");
Since the string was defined on one line, we also put in a newline ("\r\n").
If you'll take a look at the Character Map (Start > Programs > Accessories > System Tools > Character Map), you can locate the keystroke "code" for most characters.
For Arial Text, the "Middle Dot" keystroke combination is:
Alt+0183
To place this in your text using Visual Studio, hold down the Alt key while typing in "0183" on the Numeric Keypad (not the keys over the alpha pad).
This should give you "·" in your text.
That's as close as I have come.
Note that other special characters (degrees - Alt+0176 °, copyright - Alt+0169 ©, and many others) can be included with this technique.
Yes you can use text below:
const string text = #"To continue please selected one of the actions below:
. Click ButtonA to do this action.
. Click ButtonB to do this action.";
label1.Text = text;
You can use special characters for the bullet as well such as *.
You could use a WebBrowser control. I.E.:
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.DocumentText 1 = "<li>Click ButtonA ...</li><li>Click ButtonB ...</li>"

asp.net formatting an email

I am working on an application that involves the user clicking on a hyperlink to popup an outlook email template. I have the SendTo values, and the subject values.
How do I enter text into the body of the email?
In the hyperlink you can add the ?body= variable after the email address. Formatting the body will be a challenge because for symbols like spaces you have to use hex codes (space = %20, newline = %0D). I don't know what your design criteria are, but most of the time I prefer to create a form with the desired input fields and let .NET handle the sending of the e-mail message on the submit.
Are you searching for this?
...

C# Button Text Unicode characters

C# doesn't want to put Unicode characters on buttons. If I put \u2129 in the Text attribute of the button, the button displays the \u2129, not the Unicode character, (example - I chose 2129 because I could see it in the font currently active on the machine).
I saw this question before, link text, but the question isn't really answered, just got around. I am working on applications which are going all over the world, and don't want to install all the fonts, more then "don't want", there are that many that I doubt the machine I am working on has sufficient disk space. Our overseas sales agents supply the Unicode character "numbers". Is there another way forward with this?
As an aside, (curiosity), why does it not work?
The issue is:
C# will let you put Unicode in, like button1.Text = "Hello \u2129";, no problem
but the Visual Studio Forms designer will not recognize '\u2129' as anything special. By design.
So just paste in the '℩' in the Properties Window or use code.
Change the "Font" of the button to the "Font" (From google:Arial Unicode MS) which supports "u2129". It may help you
have you tried entering the characters manually? also, have you tried using a literal string with #"blahblahblah" ?
I was trying to include copyright symbol (\u00a9) in the form title. Using escape characters or changing fonts didn't work for me. I simply copy-pasted the symbol from text editor.

Which control to use for quick text input (inputbox)?

I need a quick text input dialog box (MessageBox with a single text box in it). Is there any control available or should I use a form?
I just want user to enter some ID. And in other occasion I want 2 texboxes for username and password.
Microsoft.VisualBasic.dll has an InputBox method which you can use from C# to get a single string.
For example (Add a reference to Microsoft.VisualBasic.dll first)
using Microsoft.VisualBasic;
string response = Interaction.InputBox("Enter a string:", "MyApp", "DefaultString", 0, 0);
Othewise, you'll have to make your own form.
simple one is inputbox

Categories

Resources