Multiline mask for MaskedTextBox

2019-06-23 21:58发布

问题:

I have a MaskedTextBox in my Winforms application. I need a multiline mask on it, f.e:

"999999\r\n
 999999\r\n
 999999\r\n
 00/00/0000"

I read the msdn documentation and was suprised to see that there is no "new line"or something like that.

I know that i can write my own user control to fix this issue, but a masked textbox would be an easier solution. So i have 2 Questions: Is there a way to add a new Line to a mask? If not, why does the Control supports multiline - isnt that useless?

Thanks in advance

回答1:

For that I would create a custom control that would combine set of MaskedTextBox for each line Now, depending on the need, either "dumb" control with constant amount of MaskedTextBox one under another and corresponding properties for format strings

public string Format1 {get;set;} 
public string Format2 {get;set;} 
public string FormatX {get;set;} 

Or create a "smart" version, with property "LinesCount" etc, when you set to 5 it would add 5 MaskedTextBox with set anchor to left and right (for the whole control to be stretchable)

And then property

public List<string> Formats {get;set;}

each line would correspond to each MaskedTextBox

Also property values

public List<X> Values 

each line would correspond to each MaskedTextBox.Value, where X is type I don't remember it returns.

The more complicated and smart and useful control you want the more coding you need to put :) but doable