0

I have a winform where I have a textbox that will take upto 14 numbers. Now if user enters less that 14 i have to populate the rest of the fields with 0s. Eg. if a user 10 numbers the i have to include 4 more 0's to to make it 14.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Drone
  • 181
  • 8
  • 22

3 Answers3

4

Change the MaxLength property of the text box to 14. After you get the Text property, use, the PadLeft, or PadRight methods on the String class.

Example

void textBox_LostFocus(object sender, EventArgs e)
{
    var text = this.textBox.Text;
    text = text.PadLeft(14, '0');

    this.textBox.Text = text;
}

Results

var value = "abcd";
var leftPadded = value.PadLeft(14, '0'); // <- "0000000000abcd"
var rightPadded = value.PadRight(14, '0'); // <- "abcd0000000000"

You might also want to consider using the MaskedTextBox class.

Dan
  • 9,717
  • 4
  • 47
  • 65
  • this should be use in `textchanged` event of the textbox? – Drone Sep 25 '12 at 05:50
  • i have written this on the textChanged event of the textbox `var text = atxt.Text; var right = text.PadRight(14, '0');` but i am not getting the 0's in my string – Drone Sep 25 '12 at 05:54
  • 1
    @Drone Good question. If you execute this code in a handler to the `TextChanged` event, as soon as you typed 1 character, or backspaced 1 character, the text would be replaced with the 14 character version. I would not execute it in a `TextChanged` handler, but rather a lost focus event handler: `Leave` or `LostFocus`. – Dan Sep 25 '12 at 05:55
  • 1
    @Drone Make sure you reassign the text box's `Text` property to the string that you've padded. – Dan Sep 25 '12 at 06:01
  • 1
    @Drone You need to assign the new string back to the text box. So the two lines you have, plus `atxt.Text = right;`. Or if you want to do all three steps in one line: `atxt.Text = atxt.Text.PadRight(14, '0');`. – Jeppe Stig Nielsen Sep 25 '12 at 06:05
  • ok wht i am now doing is in textbox.leave event i written this `var text = atxt.Text; var right = text.PadLeft(14, '0');` and in another button click event i am doing this `str = atxt.Text.Trim();` now i want this `str` string to contain those extra 0's but i am not getting it. – Drone Sep 25 '12 at 06:07
  • @Dan that solved my problem nicely... but is there a way not to show the 0's in the textbox? – Drone Sep 25 '12 at 06:11
  • @JeppeStigNielsen that solved my problem nicely... but is there a way not to show the 0's in the textbox? – Drone Sep 25 '12 at 06:11
0
textbox.TextChanged += new EventHandler( textbox_TextChanged );

private textbox_TextChanged(Object sender, EventArgs e) {
    textbox.Text = textbox.Text.PadLeft(14, '0');
}
Dai
  • 141,631
  • 28
  • 261
  • 374
0

If you use databinding, and if you just want to add the numbers in the display of the textbox, and not in the underlying datasource, you can use standard .net formatting when you create your own textbox derivative, as described here. Might come in handy.

Community
  • 1
  • 1
Maarten
  • 22,527
  • 3
  • 47
  • 68