MSpaint TextBox..
-
Hi all :cool: I am C# programmer.. For my project i want to create a textbox like in Mspaint,which restrict the entering of characters upon reaching the Size. I tried with MeasureString.. But not works Pls tell if anyone knows how to do it.. Warm regards Krishnan If u can Dream... U can do it
-
Hi all :cool: I am C# programmer.. For my project i want to create a textbox like in Mspaint,which restrict the entering of characters upon reaching the Size. I tried with MeasureString.. But not works Pls tell if anyone knows how to do it.. Warm regards Krishnan If u can Dream... U can do it
MeasureString
is graphics, which is why it's defined on theGraphics
class. To limit text in aTextBox
, you can post-validate the control (which fires when the focus is lost for that control) by handling theValidating
event. If you want to limit which characters will even work, override theIsInputChar
method for theTextBox
:public class NumericTextBox : TextBox
{
protected override bool IsInputChar(char charCode)
{
return charCode >= '0' &&charCode <= '9';
}
}This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]
-
MeasureString
is graphics, which is why it's defined on theGraphics
class. To limit text in aTextBox
, you can post-validate the control (which fires when the focus is lost for that control) by handling theValidating
event. If you want to limit which characters will even work, override theIsInputChar
method for theTextBox
:public class NumericTextBox : TextBox
{
protected override bool IsInputChar(char charCode)
{
return charCode >= '0' &&charCode <= '9';
}
}This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]
In my case i want to know how many charcaters can be enter in the textbox . How it can be know. So when reaching that number i can disable entering characters.. The problem here is to find the number of characters that can be enter in the text box (No scrolls).. What u think ? Regards Krishnan If u can Dream... U can do it
-
In my case i want to know how many charcaters can be enter in the textbox . How it can be know. So when reaching that number i can disable entering characters.. The problem here is to find the number of characters that can be enter in the text box (No scrolls).. What u think ? Regards Krishnan If u can Dream... U can do it
The
MaxLength
property of theTextBox
lets you set a maximum number of characters that the user can enter, no matter the width (it can be 10 m's or 10 i's). But if you set theTextBox
to be, say, 100 pixels wide, and you want only 100 pixels of letters, be it 5 m's or 8 i's, it's a bit more difficult. As an option, try using theMaxLength
property and making theTextBox
wide enough. But if you absolutely need to allow only a certain width of characters, try getting the font of the control, and using some graphics function (MeasureString
could be, I am no expert.) to measure the current contents plus the new character in theKeyDown
event, and if the new text is wider than the control, don't accept the key. I hope to have given you some pointers. Good luck! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!