Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to allow inside in textBox1 next items:
- allow only numbers (0-9)
- also allow next letters: M B G T
- also allow: the dot "." and comma ","


What I have tried:

C#
 private void textBox1_TextChanged(object sender, EventArgs e)
        {
//
        }
Posted
Updated 18-Mar-20 10:41am
Comments
Richard MacCutchan 18-Mar-20 12:20pm    
Just add your code where the comment marker is.
gggustafson 18-Mar-20 13:37pm    
I think you are capturing using the wrong event. From Microsoft "Occurs when the Text property value changes." That's not going to allow you to test individual characters as they are entered into the TextBox. I'd suggest that you consider the KeyDown event. It provides information about the key that has been pressed BEFORE the value is displayed. Thus you can test the key and inhibit its display. Hope that helps.

I'd suggest to use validate event with ErrorProvider and Regex.

Here is a complete example: Control.Validating Event (System.Windows.Forms) | Microsoft Docs[^]

C#
string[] somestrings = {"Ads20.e", "MG20.9", "BT3,5.0", "mt29.00", "32TG,5.99", "58BM5.11"};
string pattern = @"[[M|G|B|T|][0-9]|\.\,]|[[0-9][M|G|B|T|]|\.\,]";

foreach(string s in somestrings)
{
	Console.WriteLine("{0} => {1}", s, Regex.IsMatch(s, pattern));
}

Result:
Ads20.e => False
MG20.9 => True
BT3,5.0 => True
mt29.00 => False
32TG,5.99 => True
58BM5.11 => True
 
Share this answer
 
v2
Comments
Member 10410972 19-Mar-20 12:20pm    
Maciej Los thank you so much. The problem is solved.
Maciej Los 19-Mar-20 12:22pm    
You're very welcome.
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Just writing the method signature ans expecting us to "fill it in" isn't going to get you anywhere.
Hint: I'd use a Regex.
 
Share this answer
 
v2
Comments
Member 10410972 18-Mar-20 12:39pm    
Ok, sorry, forget it ... I'll handle it myself.
OriginalGriff 18-Mar-20 12:44pm    
Excellent - you'll learn more that way anyway!
gggustafson 18-Mar-20 13:40pm    
I don't like your attitude! This site is to help, not embarrass OPs. Yes, we acknowledge that you are knowledgeable. But there is absolutely no reason to cause discomfort to an OP. A simple answer, like mine, goes a long way to improve OP's knowledge base.
Maciej Los 18-Mar-20 16:50pm    
Not sure why some one down-voted your answer, Paul...
Up-voted!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900