Search code examples
c#.netwinformsc#-4.0maskedtextbox

MaskedTextBox does not showing starting characters


I have C# DotNet Windows Application form


in my application have two MaskedTextBox and they are showing different values.

Property of Mask 1

  • FONT : Microsoft Sans Serif Regular size 16pt
  • RIGHT TO LEFT : NO
  • TEXT ALIGN : LEFT
  • MASK : >&&&&&&&&&
  • WIDTH : 100
  • HEIGHT : 31
  • TEXT : ASHISH PATIDAR

Property of Mask 2

  • FONT : Microsoft Sans Serif Regular size 16pt
  • RIGHT TO LEFT : NO
  • TEXT ALIGN : LEFT
  • MASK :
  • WIDTH : 100
  • HEIGHT : 31
  • TEXT : ASHISH PATIDAR

1st MaskedTextBox have Mask and 2nd have not Mask, rest property are same.

  • Case 1
    when set the mask and value are shown like "HISH_PAT" as MASK 1 text box.(as show in image).

  • Case 2
    when does not set the mask and value are shown like "ASHISH P" as MASK 2 text box (as show in image).

I want to show value like "ASHISH P" as MASK 2 text box (as show in image). When the Mask is SET, but I do not want to change width of MaskedTextBox.

How to resolve this ?


Solution

  • I just played around bit with the MaskedTextBox properties and it looks to be default behaviour of the control and co-related with Width. So when the length of the text to be displayed exceeds proportionate to the width of the control, the text towards end is shown.

    I managed to make use of the maskedTextBox.Select property manually move the to the 1st character of with the below code line;

    maskedTextBox1.Select(0, 1);
    

    The above line selects the first character of the control, which enables to show the start of the entered text/ selection. The code should be called from the maskedTextBox1.Leave event. If your app displays some data at the start of the application/ screen then the same would have to be called from the Form.Load event as well.

    enter image description here