Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

I'm looking for a tip or hint for the following problem.

I have a Textbox. The user can only insert digits and i want to transform the input to a currency value

e.g.:

The user inserts 564
and the tb converts it into some kind of: € 5,64

or:
the users inserts 65
-> tb converts it into: € 0,65

or
input: 456125
-> convert: € 4.561,25

my problem is the write direction: i want the user to write right to left, so you first write the "4", the cursor jumps to the right end of line, than you write the 5 and so on

I've tried many thing like maskedTextBox with mask:
$ #,###,###.#0
or
$ ###.##
and so on

i've also tried
string.format("{0:c}");
But here i get, when i insert for example 15354 this result : 15.354,00 €
i want the last 2 digits behind the ","


i hope you understood where's my problem. I don't a finished code, just a hint or something..
Thanks in advance!
Posted
Comments
Sergey Alexandrovich Kryukov 30-Dec-11 22:01pm    
Tag it properly. Is it WPF, Silverlight, Forms, ASP.NET, something else?
--SA

If you're converting the string to for example double, why not simply divide it by 100 and use that for your output. Optionally you can first check if decimal separator is present and if it is, then don't use division.

I'd guess the tricky part would be the formatting. For this you could store the keystrokes in a separate, constantly growing string which the would be properly formatted in your textbox. So the text box would actually just show the contents of your string variable.
 
Share this answer
 
Comments
[no name] 30-Dec-11 18:34pm    
I suggest multiply by 0.01 but that is near paranoia to have it a tick more efficient. It was only my comment for the 5 ;)
Regards
Wendelius 30-Dec-11 18:39pm    
Good idea, thanks :)
I'd approach this in three steps:

1. if I had thoroughly examined every possible use of a MaskedTextBox, with setting the 'Culture property to "eu-ES", so that, theoretically, I could get results in € vs. US $ ... and was not satisfied with the result ...

... then ...

2. if I had searched CP, and the net, for somebody already having worked out an elegant solution for this, and I did not find something already adequate:

... then ...

3. I'd create a UserControl that contained two NumericUpDown Controls, both with the 'DecimalPlaces property set to #0. The left one would hold the integer Pound amount, the right the integer that will become the fractional part of the return value.

a. I'd create a Public Property of Type Decimal to make the result available to outside consumers of the UserControl.

b. I'd set the right NumericUpDown to have a Minimum value of #0, and a Maximum value of #99.

c. I'd set the Maximum value of the left NumericUpDown to the largest possible amount I wanted to allow to be entered, and its Minimum to #0,

d. In that UserControl I'd "wire up" the same KeyPress EventHandler to both NumericUpDown Controls: in that KeyPress EventHandler (as shown below): I'd intercept the Enter key

e. On the Enter key being pressed: I'd update the Public property described in "3a." above. Dividing the right NumericUpDown control value by #100 to get the "decimal part" (as shown below).

The code might look like this:
C#
namespace TestCurrencyEntry
{
    public partial class MyPoundEntry : UserControl
    {
        public MyPoundEntry()
        {
            InitializeComponent();
        }

        public decimal ResultInPounds { get; set; }

        // assign KeyPress Event of both NumericUpDown Controls to this
        private void PoundEntry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ResultInPounds = numericUpDown1.Value + (numericUpDown2.Value/100);

               // perform extra validation here ?

               // raise an Event here ?
            }
        }
    }
}
Discussion:

1. I'd consider on Entry into the UserControl setting the Focus to the Left NumericUpDown Control.

2. Given the two NumericUpDown Controls are in the proper tab-order, then you can easily start entering the integer amount in the left NUD control, then, hit the tab-key, and continue entering the integers in the right NUD that will become the decimal amount.

3. Depending on application requirements, I might want to raise an Event whenever the value was changed.
 
Share this answer
 
v3
You can always select text direction using System.Windows.Forms.TextBox.RightToLeft; see also a number of RtlTranslate… methods, http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.aspx[^].

WPF provides different ways to support input direction, see http://msdn.microsoft.com/en-us/library/aa350685.aspx[^].

I found your description of input contradictory though: putting cursor on right means normal left-to-write method, your example confirms it. Anyway, from this answer you can find our what you need, experiment with input.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900