when hitting enter in text box the value disappearing - c#

I wish to accept maximum two digits to my text box I used this code.
if(textbox1.textlength==1)
{
this.selectNextcontrol((control)sender,true,true,true,true);
}
now the problem is that the text box also accept single value like 6, it is accepting but when I hit enter after placing 6 the value disappearing.

You can set maximum length property of textbox to 2.

Related

How to look for a specific amount of digits within texbox textChanged event in C#?

In my Windows Form Application, I want to implement a feature where the user has to fill in the serial number of a product that is when matched with any product in the database, the product must appear in a grid. I want to do so using textbox textChanged event.
I am confused in figuring out that either I must prevent firing the textChanged event before the textbox value matches any value in the database. Is there any way to make the textbox expect a specific amount of text or number (my serial numbers are going to be fixed length - like 10001, 10002, 10003) before running the remaining code for showing product in the grid?
You can use TextLength property of the TextBox to get length of text. For example:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.TextLength < 5)
return;
//Send query to database
}
Note: As it's also mentioned by Jimi in the comments, it's good idea to set MaxLength of TextBox to prevent entering more text.
Consider the use of a [MaskedTextBox][1]. A MaskedTextBox is similar to a standard TextBox, except you define the format of the input text. This can be anything, letters, numbers, dashes, etc.
In your case you accept only input of five digits.
Use the windows forms designer to add a MaskedTextBox, or add one yourself:
this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox();
this.maskedTextBox1.Location = ...
this.maskedTextBox1.Size = ...
// etc. Advise: let forms designer do this
// accept only input of five digits:
this.Mask = "00000";
The operator sees an indication of the length of the requested input. It is impossible for the operator to type a non-digit. Event when all five digits are entered, but also while typing (TextChanged), so if desired you can implement autocompletion. You can even get notified if operator presses one invalid key, so you can inform the operator about the error

Determine what line user is editing in textbox

I want to be able to determine what line the user is editing in a multi-line text-box.
So could anyone suggest how i get the line number that the user is editing.
Also, is it possible to have textboxes side by side which when the user takes a new line in one, we can programatically take a new line on the other one?
You can use:
label_currentLine.Text = Textbox1.GetLineFromCharIndex(Textbox1.SelectionStart) + 1
Plus 1 because index starts at 0.

How To add * in Textbox Starting value and print into label in C#

I have some text boxes in my form where the user need to enter the different prices of article, what I want to do is to automatically add Starting value * whenever text is changed . So when the user types 1 it is displayed like *****1
and text box Length are 6 size fix. then again user type 111 it is display like ***111
Not sure if you are on web, forms or what, but here is what you looking for:
txt1.Text = txt1.Text.PadLeft(6, '*');
Reference: PadLeft
You can use the PadLeft method for a string:
textBox1.Text = textBox1.Text.PadLeft(6, '*');
See an example here: https://dotnetfiddle.net/GPfFsx

How to mask some characters in a textbox while entering information into it?

I have a textbox on a windows form that takes a credit card number and it shows the number in plain text. I need to display "x" for the first 12 characters and only the last 4 digits should be displayed in plain text while entering information into it. Also, do we get the actual information from textbox's text property if we mask the value?
Thanks for any help!
I would just split the textbox in two parts, one masked using a MaskedTextBox, the other plain for the last 4 digits. You get your functionality for free in this case, no need to reinvent the wheel. Use a Mask property of "0000-0000-0000" and a passwordChar of 'X'.
Once a user has entered the first 12 digits, you can automatically set focus on the second textbox to set the remaining digits.
In your OnTextChanged event of the textbox, you should keep a tab of the number of entered char's and as long as that count is <= 12 you should replace the entered char's with a "x".

Masked TextBox how to not show underlines in text box and allow user to enter 3 digits

I am having some troubles with a masked textbox.
How can I hide those underlines which are shown in the textbox by default?!
Also I want the user be able to enter up to 3 digits (he/she can enter atleast 1 and maximum 3 digits). how to set that?
Set empty space(leave one space) in Promptchar property of masked textbox.
and second answer you already get. or you can put validation on Leave event for that.
I think you'd be better off using a regular TextBox for this. Setting it to allow only digits and max 3 digits are done with properties. Then you just have to check manually that at least one digit exists.

Categories

Resources