0

I'm looking to know how I can draw a grid in a multine textbox, where in each square of the grid there will be a letter, something like this:

enter image description here

But, for a multiline textbox, I want to use some fixed width font, like, CONSOLAS, so, this is will help a lot... I tried to change this code ( How to customize Winforms textbox text characters separated? ) to work on textbox, but, didn't work.

  • TextBoxes are legacy controls and do not like being drawn upon. – TaW Mar 03 '21 at 19:53
  • You can test the two answers here: [Dotted lines for typing text on it](https://stackoverflow.com/q/64672298/7444103). Mine draws a dashed line directly onto the TextBox surface, while LarsTech's uses a border-less TextBox and a Panel; the graphic line is drawn on the Panel instead. You have to measure the char box and draw your grid accordingly. Of course you need a mono-spaced Font. You can use `Graphics.MeasureCharacterRanges` as described here: [How to compute the correct width of a digit in pixels?](https://stackoverflow.com/a/54772134/7444103) to build the grid. – Jimi Mar 03 '21 at 20:50
  • You should probably build a Custom Control / UserControl. To setup the char spacing, take a look at the [SetTextCharacterExtra](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-settextcharacterextra) GDI function (to use with `TextRenderer.DrawText()` - Your Text color is the same as the background or simply redirected, while you draw the text content as graphics). Otherwise, you'll have to directly handle the input and add/remove a white space char to/from each char entered. – Jimi Mar 03 '21 at 21:04
  • Another option is to use a WebBrowser control, change it to Edit Mode and format the text using CSS (defining the `letter-spacing`) – Jimi Mar 03 '21 at 21:10
  • As a note (in case you're interested in GDI rendering), the `SetTextCharacterExtra()` function (which is very simple to use) should be replaced with [ExtTextOut](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-exttextoutw) to handle RTL, non-Latin languages. – Jimi Mar 03 '21 at 21:20

1 Answers1

0

I think that you want to show the operator some fill-in box: A box where he should type something that fits some format, like a postcode, or telephone number, or the house number in an address, or car registration sign

For this, consider to use a MaskedTextBox control.

enter image description here

With the MaskedTextBox you can easily define the format of some input string: how many characters, where do we want a Letter, and where a Digit? Where should brackets occur.

A MaskedTextBox control is just like an ordinary Textbox, only with a specific format. The Mask:

  • The "#" character in the mask is used for digit only
  • The "&" character accepts letter only
  • The "!" character accepts letter or digit
  • You can use any mask symbol, letter and digit other than # & and !

See the image what happens with certain formats.

The mask shows a predefined format what to expect: where do we want brackets, where do we want spaces, numbers, etc. The mask prevents typing incorrect characters.

The control notifies you when the operator has typed text that matches a complete mask.

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116