textbox only for integer in window application [duplicate] - c#

This question already has answers here:
How do I make a textbox that only accepts numbers?
(41 answers)
Closed 9 years ago.
I have a Windows application, in which there is a login form and registration form.
In the registration form there are 4 textboxes in which one of them is for getting the user contact number (Mobile Number), where only numbers are allowed.
I want that user can enter only integer number not char just like in asp application, where using ajax textfilter we restrict the user to enter only integer number.
Does this type of functionality exist, such that the Windows application can restrict the user?

1)You can handle the TextChanged event of the TextBox and make sure that no key other than the digits(0-9) are displayed in the TextBox.
You can use Regex to check whether the text contains letters or not.
Another option is to validate input of text before processing the text, if it contains characters other than numbers, issue a warning to the user and allow him to change the text of the TextBox.
private void Text_Changed(object sender, EventArgs e )
{
if(!char.isDigit(textBox1.Text[textBox1.Text.Length-1]))
{
string temp = string.SubString(textBox1.Text, 0, textBox1.Text.Length-1) ;
textBox1.Text = temp ;
}
}
WORKING:-
Every time the text changes, it checks the last character added and if it's not a digit then,it doesn't display it.
For eg if after entering 971 I enter r then the if condition becomes true and the Text field of TextBox resets the text to 971 and doesn't display the "r".
2)Otherwise, you can use a MaskedTextBox
Change the masked property of maskedTextBox to Digits like this:-
Check the following link:
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox(v=vs.110).aspx
http://www.codeproject.com/Tips/666932/Using-Masked-TextBox-in-NET
http://www.codeproject.com/Articles/1534/Masked-C-TextBox-Control
3)OPTION-3:- You can create a custom control after deriving from the TextBox class.
http://msdn.microsoft.com/en-us/library/ms229644(v=vs.90).aspx

Related

C# Masked textbox with date format skips first character

I have a problem in a winform app.
I have several masked textbox which use the short date mask (//____), the problem is that if I select all the text (either with Ctrl + a or from code) and I write a new date is like that the first character goes at an 11th position (the mask disappear and i see only the character I wrote) and if I press backspace the text becomes something like this _1/01/1979 and I have to select all again and press backspace or delete everything.
I handled in this way
private void maskedTxt_KeyPress(object sender, KeyPressEventArgs e)
{
MaskedTextBox msk = sender as MaskedTextBox;
if (msk != null)
{
if (!string.IsNullOrEmpty(msk.Text.Replace("/", string.Empty).Replace(":", string.Empty).Trim()) && _focused)
{
_focused = false;
SendKeys.SendWait("{BACKSPACE}");
}
}
}
_focused is a Boolean variable that I set to true if a validation error happens at the leave event of the masked textbox (invalid date, the date is too big or too small etc...)
so that when someone enters the textbox the text can be written correctly
Is there a better way to handle this "error" or this is good? I tried it and it worked but a lot of people will have to use this application and probably there will be some errors along the way.
Thanks

Concatenate some strings into a global variable in C#

i'm having a problem . I'm trying to make an application in C# (Windows Form Application) and i'm stuck into something.
I want to do a log-in form like in the photo. For username i'm having a combo-box where i can choose the user , but for password i want to place some buttons(like in the photo) , and when i press the button 1 for example , i want to have a string which is 1 .After that i will place 2 ,and i want to concatenate 1 and 2 into that string . Do you know how can i do that
In your case, I believe you have one event for each button. In each event you could just sum the values to a global variable or directly change the textbox. Like this:
txtPassword.Text += "0"; //the 0 button
It will change the textbox value and when you click "Sign in" you can get the txtPassword.Text as the final string.
You can also declare a string or StringBuilder globally and append the value in every button click, tho this won't change the textbox text directly.
Use a StringBuilder to concatenate strings.
StringBuilder builder = new StringBuilder();
builder.Append(a);// where "a" is your input character
And whenever you get a new character input call builder.Append(a);
In the end you get your final string by calling builder.ToString();
As a variation on the answer from #LeoFormaggi, you could have a single event handler for all the numeric buttons (i.e., one that every button's click handler points to). Then, in the handler:
var buttonText = (sender as Button)?.Text;
txtPassword.Text += buttonText ?? String.Empty;
Don't forget to make that textbox be a password field (by setting the PasswordChar property).
You don't want to bother with a StringBuilder here. It's normally the right tool for concatenating strings, but it buys you nothing in this case since you want the result to appear immediately in the text box. In this case it would simply add overhead for no good reason.
i want to thank you, i made my solution like this :
`private void nb6_Click(object sender, EventArgs e)
{
password_box.Text += "6";
}`
in every button event that i have, the password_box (which is my textbox) and i concatenate everytime the string that i want(in this case is number 6 )
Thank you and have a nice day . :)

Read and store values from Windows Form in Methods

I wrote a programme in VB to calculate the efficency of a heat exchanger with the temperature and other variables as user imput in a windows form. In VB is very simple, you just type:
Tcin = Val(Form1.Tempcin.Text);
Thin = Val(Form1.Temphotin.Text);
and the values are stored in Tcin and Thin.
But I have to translate the whole code to C# and I'm facing a lot of problems just to get the values form the text boxes in the windows form (all numerical). I have to use them in several methods as well, not only in the main form. The same happens with RadioButtons and CheckBoxes.
How do I call the variables or the "check status" of the buttons and checkboxes in the namespace and the several methods?
Inside your Form1.cs class, you can call:
Checkbox:
bool checked = this.checkBox1.Checked;
Radiobutton:
bool checked = this.radioButton1.Checked;
Textbox:
string text = this.textBox1.Text; // For text
- or -
int number = this.Int32.Parse(textBox1.Text); // For numbers
// If the textbox contains letters, there will be an error thrown (an exception)
Update the control names accordingly. You may have named your textbox myTextBox instead of textBox1.
This topic has to do with controls in C#. A button, a textbox, a checkbox, a radiobutton, a progressbar, and even a form ... these are all controls. This should help you with terminology if you want to Google further.

Switch after 5 characters

I'm trying to swipe a card and after the 5 characters are entered I want it to go to the next textfield. I'm scanning a card.
Currently I have:
private void membernumber1_TextChanged(object sender, EventArgs e)
{
}
But this changes it right after it enters one character, is there anyway to make it switch after 5 characters entered?
Just count the number of characters in the Text property. Using a counter won't work if they use the backspace key.
if( membernumber1.Text.Length == 5 )
SwitchFocus();
Be aware though that this may not work for text that was pasted into the control (i.e., if it was > 5 characters). You'll need proper validation for that case or you can just disable pasting, but validation is preferable as there are certainly other restrictions like being all numeric.

Require Input in Masked Text Box

I have a masked text box (number of days) in a form that has to always have an integer inside. It cannot be blank when the user pressed the "OK" button on the form.
What I need is when the user pressed "OK", if the text box is not filled as it should be, a tool tip come up and show the user they need to enter information there before they can proceed.
What should I use for this? I'm guessing a variation of a Tool Tip and Masked Text Box, I just need a push in the right direction as I can't find the information anywhere.
use a regex control to validate the input and a required field validator. on the validation you could fill out a div with display:none then use jquery tooltip (or something like it) to display a tooltip with the information in that div. thats my first blush response anyway.
it will work for you
private void button1_Click(object sender, EventArgs e)
{
if (maskedTextBox1.Text == "")
this.errorProvider1.SetError(this.maskedTextBox1, "Plz fill it");
else
this.errorProvider1.Dispose();
}
Use Validation controls provided by asp.net. You can set a required field validator and a Regular Expression Validator for the input field. Then, set the CausesValidation property of the input control to true. Validator controls provide error message labels which will be visible if the input value is invalid. For more information, you can visit http://msdn.microsoft.com/en-us/library/debza5t0.aspx

Categories