VB6 How can I adjust maximum characters in a textbox depending on width of text entered so far?
Posted on 2009-04-12
I need to optimize the maxchars allowed in a VB6 textbox. It makes a big difference in the maximum number of characters that will fit if the users enter wide letters like "WWW" or narrow letters like "iii". Since textbox.maxchars can't be changed at runtime, I'm using code in the keypress event to limit the number of characters.
I found there may be a potential solution using a hidden picturebox to determine the width of text entered so far to determine if more characters should be allowed in the textbox. But I have not been able to get this work very accurately yet.
intWidthRemaining = Textbox1.Width - PictureboxHidden.TextWidth(Textbox1.text)
intMaxWidthOfPossibleNextChar = PictureboxHidden.TextWidth("W")
If intWidthRemaining > intMaxWidthOfPossibleNextChar Then blnAllowAnotherCharToBeTyped
Can a picturebox be used to determine the width of the text entered so far? Or, is there an api call that could tell me if a textbox has room for more characters?