Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my windows form I have a maskedtext box. For this text box user can type only a single character. And that character must be the "Y" or "N". Other characters not allowed for this. How can I use mask property for this case.
Posted

You can use TextChanged event handler.
Check if the text is not "Y" or "N", clear the textbox and show message if required. There is no need to mask. This would work fine for single letter textbox.

You can use dropdownlist instead.
 
Share this answer
 
Comments
Espen Harlinn 12-Jul-11 10:15am    
Nice and simple, my 5
Sergey Alexandrovich Kryukov 12-Jul-11 13:14pm    
Well, solution is correct but -- "Don't be a problem solver!".
The problem is inadequate choice of UI element.

Please see my solution.
--SA
Espen Harlinn 12-Jul-11 13:27pm    
For all we know, using a textbox might be part of the requirements ...
Sergey Alexandrovich Kryukov 12-Jul-11 13:30pm    
My answer to "requirements" -- "Don't be a problem solver". Actually a decent leader posting requirements expect developer's feedback in them.
--SA
Espen Harlinn 12-Jul-11 14:28pm    
There are, in some places, some cultural aspects that may prevent this ...
Why so complex and confusing way? Simply use too radio buttons: "Yes", "No". Better yet, one check box "I confirm it" or something.

—SA
 
Share this answer
 
Comments
Espen Harlinn 12-Jul-11 13:27pm    
... but you do have a point, my 5 :)
Sergey Alexandrovich Kryukov 12-Jul-11 13:30pm    
Thank you, Espen. I knew you would understand it.
--SA
I think perhaps the closest is to use the mask value of "L" - this will restrict the input to only one letter. You would then have to apply your own additional validation to decide if that value is valid (i.e. Y or N)

Can I ask why you are using a textbox thou? It seems you would be better suited to use a checkbox or a dropdownlist for this kind of UI behaviour
 
Share this answer
 

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