I m Developing A windows Application i m easily handle to enter the only numbers in Text box using key press event. but i still able to paste alpha bates in text boxes how can i restrict to paste alpha bates in text box
us the use the TextChanged event.
private void textBox1_TextChanged(object sender, EventArgs e)
{
var rgx = new Regex(#"\D");
textBox1.Text = rgx.Replace(textBox1.Text, "");
}
Use KeyPress event,
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
You don't explicitly state what UI technology you're using, but if it's WinForms you could try using a MaskedTextBox with the appropriate mask property setting.
MSDN MaskedTextBox Mask Property
Related
I have a textbox for verbs and I want it to auto add "ing" to any word the user tries typing in that box if its not there. Is this possible? And how?
My verb textbox is:
tbVerb
Think this should do what you want.
if(textbox.text.substring(textbox.text.lenght-3) != "ing")
textbox.text += "ing";
But you will need a validate or event as the guy in the other answer suggests.
Use the TextChanged Event:
private void tbVerb_TextChanged(object sender, EventArgs e)
{
// add your value to checkbox
// this code is WinForms
// tbVerb.Text = tbVerb.Text + "ing";
}
private void tbVerb_TextChanged(object sender, EventArgs e)
{
if (!tbVerb.Text.EndsWith("ing"))
tbVerb.Text += "ing";
}
I want to use numeric input in Windows Phone 8. But I only want to used Numeric one with no char "." in it.
This is the picture of numeric input that I used in windows phone 8, what im gonna to do is disable the "." in the bottom left of the picture. How i can do that ? Thanks before
Hi you can use like this---
<TextBox .... InputScope="Digits" ....> in xaml
This will still add the '.' key in the keyboard. To prevent users from typing it you add the KeyUp event to the TextBox and do the following:
in code behind--
private void KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
TextBox txt = (TextBox)sender;
if (txt.Text.Contains('.'))
{
txt.Text = txt.Text.Replace(".", "");
txt.SelectionStart = txt.Text.Length;
}
}
You can just write the following code on key down event of the TextBox control
private void YourTextBox_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.PlatformKeyCode == 190)
{
e.Handled = true;
}
}
Use the following code, which does ignore the press of the "." key:
XAML:
<TextBox InputScope="Number" KeyDown="TextBox_OnKeyDown" />
C#:
private void TextBox_OnKeyDown(object sender, KeyEventArgs e)
{
e.Handled = !System.Text.RegularExpressions.Regex.IsMatch(e.Key.ToString(), "[0-9]");
}
In a TextBox I'm monitoring the text changes. I need to check the text before doing some stuff. But I can only check the old text in the moment. How can I get the new Text ?
private void textChanged(object sender, EventArgs e)
{
// need to check the new text
}
I know .NET Framework 4.5 has the new TextChangedEventArgs class but I have to use .NET Framework 2.0.
Getting the NEW value
You can just use the Text property of the TextBox. If this event is used for multiple text boxes then you will want to use the sender parameter to get the correct TextBox control, like so...
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
}
}
Getting the OLD value
For those looking to get the old value, you will need to keep track of that yourself. I would suggest a simple variable that starts out as empty, and changes at the end of each event:
string oldValue = "";
private void textChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if(textBox != null)
{
string theText = textBox.Text;
// Do something with OLD value here.
// Finally, update the old value ready for next time.
oldValue = theText;
}
}
You could create your own TextBox control that inherits from the built-in one, and adds this additional functionality, if you plan to use this a lot.
Have a look at the textbox events such as KeyUp, KeyPress etc. For example:
private void textbox_KeyUp(object sender, KeyEventArgs e)
{
// Do whatever you need.
}
Maybe these can help you achieve what you're looking for.
Even with the older .net fw 2.0 you should still have the new and old value in the eventArgs if not in the textbox.text property itself since the event is fired after and not during the text changing.
If you want to do stuff while the text is being changed then try the KeyUp event rather then the Changed.
private void stIDTextBox_TextChanged(object sender, EventArgs e)
{
if (stIDTextBox.TextLength == 6)
{
studentId = stIDTextBox.Text; // Here studentId is a variable.
// this process is used to read textbox value automatically.
// In this case I can read textbox until the char or digit equal to 6.
}
}
Here's my code:
private void Form1_Load(object sender, EventArgs e)
{
maskedTextBox1.Mask = "*[L]";
maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
}
How can I set it to accept only letters, but however many the user wants? Thanks!
This would be easy if masked text boxes accepted regular expression, but unfortunately they don't.
One (albeit not very pretty) way you could do it is to use the optional letter ? mask and put in the same amount as the maximum length you'll allow in the text box, i.e
maskedTextBox1.Mask = "????????????????????????????????.......";
Alternatively you could use your own validation instead of a mask and use a regular expression like so
void textbox1_Validating(object sender, CancelEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(textbox1.Text, #"^[a-zA-Z]+$"))
{
MessageBox.Show("Please enter letters only");
}
}
Or yet another way would be to ignore any key presses other than those from letters by handling the KeyPress event, which in my opinion would be the best way to go.
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), #"^[a-zA-Z]+$"))
e.Handled = true;
}
If you want only letters to be entered you can use this in keyPress event
if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)) //The latter is for enabling control keys like CTRL and backspace
{
e.Handled = true;
}
How can only valid characters be allowed in a Windows file system in a TextBox that can only appear as uppercase in Windows Forms?
Is there an easy way for this?
About the set of characters allowed in a Windows file system (Char.IsLetterOrDigit is not enough)
How do I make the typed characters uppercase?
Create a Textbox key press handler and Use Path.GetInvalidPathChars(), Path.GetInvalidFileNameChars() to check for a valid char and return the uppercase version if the char is valid.
textBox1.CharacterCasing = CharacterCasing.Upper;
...
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Path.GetInvalidFileNameChars().Contains(e.KeyChar) ||
Path.GetInvalidPathChars().Contains(e.KeyChar))
{
e.Handled = true;
}
}
[Of course, it would be more reusable to create a method rather than placing this code directly in the handler.]
UPDATED to reflect comments.
Here's my solution. It works perfectly for windows file names convention. Cheers.
// Prevent user from wrong input - \/:*?"<>|
private void textBoxMP3Name_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), #"[^?:\\/:*?\""<>|]"))
{
e.Handled = true;
}
}
A Better way for me was to use the TextChanged Event ala:
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
...
private void textBox1_TextChanged(object sender, EventArgs e)
{
char[] invalidChars = Path.GetInvalidFileNameChars();
textBox1.Text = string.Join("", textBox1.Text.Split(invalidChars));
textBox1.SelectionStart = textBox1.Text.Length + 1;
}
because ... you need backspace and user simply love copy & paste ...