context: //
  // DO NOT REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  //
  // @Authors:
  //       christiank
  //
  // Copyright 2004-2013 by OM International
  //
  // This file is part of OpenPetra.org.
  //
  // OpenPetra.org is free software: you can redistribute it and/or modify
  // it under the terms of the GNU General Public License as published by
  // the Free Software Foundation, either version 3 of the License, or
  // (at your option) any later version.
  //
  // OpenPetra.org is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU General Public License for more details.
  //
  // You should have received a copy of the GNU General Public License
  // along with OpenPetra.org.  If not, see <http://www.gnu.org/licenses/>.
  //
  using System;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Windows.Forms;
  
  namespace Ict.Common.Controls
  {
      /// <summary>
      /// Contains a TTxtNumericTextBox which restricts the data entry to decimal numbers and shows a currency symbol next to the TextBox.
      /// </summary>
      public partial class TTxtCurrencyTextBox : UserControl
      {
          private const string COLUMNNAME_CURRENCY_NAME = "a_currency_name_c";
          private const string COLUMNNAME_DISPLAYFORMAT_NAME = "a_display_format_c";
  
          private int FOriginalTxtNumericWidth;
          private int FLastControlWidth = -1;
          private string FCurrencyName;
  
          private static TRetrieveCurrencyList FRetrieveCurrencyList;
          private static DataTable GCurrencyList;
          string FCurrencyDisplayFormat = "->>>,>>>,>>>,>>9.99";
  
          bool FSuppressTextChangeDetection = false;
          #region Properties (handed through to TTxtNumericTextBox!)
  
          /// <summary>
          /// This Property is ignored (!) unless ControlMode is 'NormalTextMode'! For all other cases, the value to be displayed needs to be set programmatically through the 'NumberValueDecimal' or 'NumberValueInt' Properties.
          /// </summary>
          [Description(
               "This Property is ignored (!) unless ControlMode is 'NormalTextMode'! For all other cases, the value to be displayed needs to be set programmatically through the 'NumberValueDecimal' or 'NumberValueInt' Properties.")
          ]
          public override string Text
          {
              get
              {
                  return FTxtNumeric.Text;
              }
  
              set
              {
                  FTxtNumeric.Text = value;
              }
          }
  
          /// <summary>
          /// Determines the number of decimal places (valid only for Decimal and Currency ControlModes).
          /// </summary>
          [Category("NumericTextBox"),
           RefreshPropertiesAttribute(System.ComponentModel.RefreshProperties.All),
           DefaultValue(2),
           Browsable(true),
           Description("Determines the number of decimal places (valid only for Decimal and Currency ControlModes).")]
          public int DecimalPlaces
          {
              get
              {
                  return FTxtNumeric.DecimalPlaces;
              }
  
              set
              {
                  FTxtNumeric.DecimalPlaces = value;
              }
          }
  
          /// <summary>
          /// Determines whether the control allows a null value, or not.
          /// </summary>
          [Category("NumericTextBox"),
           RefreshPropertiesAttribute(System.ComponentModel.RefreshProperties.All),
           DefaultValue(false),
           Browsable(true),
           Description("Determines whether the control allows a null value, or not.")]
          public bool NullValueAllowed
          {
              get
              {
                  return FTxtNumeric.NullValueAllowed;
              }
  
              set
              {
                  FTxtNumeric.NullValueAllowed = value;
              }
          }
  
          /// <summary>
          /// Determines whether the control allows a negative value, or not. Default = true.
          /// </summary>
          [Category("NumericTextBox"),
           RefreshPropertiesAttribute(System.ComponentModel.RefreshProperties.All),
           DefaultValue(true),
           Browsable(true),
           Description("Determines whether the control allows a negative value, or not. Default = true.")]
          public bool NegativeValueAllowed
          {
              get
              {
                  return FTxtNumeric.NegativeValueAllowed;
              }
  
              set
              {
                  FTxtNumeric.NegativeValueAllowed = value;
              }
          }
  
          /// This property gets hidden because it doesn't make sense in the Designer!
          [Browsable(false),
           DefaultValue(0.00)]
          public decimal ? NumberValueDecimal
          {
              get
              {
                  string NumericStr;
  
                  if (!DesignMode)
                  {
                      NumericStr = FTxtNumeric.Text;
  
                      if (NumericStr != String.Empty)
                      {
                          decimal? Ret = null;
  
                          if (((NumericStr.Length == 1) && (NumericStr.IndexOfAny(new char[] { '-', '.', ',' }) != -1)))
                          {
                              NumericStr = "0";
  
                              if (!NegativeValueAllowed)
                              {
                                  FSuppressTextChangeDetection = true;
  
                                  FTxtNumeric.SetCurrencyValue(0, FCurrencyDisplayFormat);
  
                                  FSuppressTextChangeDetection = false;
                              }
                          }
  
                          try
                          {
                              Decimal LocalCultureVersion;
  
                              if (Decimal.TryParse(NumericStr, out LocalCultureVersion))
                              {
                                  Ret = LocalCultureVersion;
                              }
                              else
                              {
                                  Ret = Convert.ToDecimal(NumericStr, FTxtNumeric.Culture);
                              }
                          }
                          catch (Exception)
                          {
                          }
                          return Ret;
                      }
                      else
                      {
                          return null;
                      }
                  }
                  else
                  {
                      return 0;
                  }
              }
  
              set
              {
                  if (value != null)
                  {
                      FTxtNumeric.SetCurrencyValue(value.Value, FCurrencyDisplayFormat);
                  }
                  else
                  {
                      if (FTxtNumeric.FNullValueAllowed)
                      {
                          FTxtNumeric.NumberValueDecimal = null;
                          return;
                      }
                      else
                      {
                          throw new ArgumentNullException(
                              "The 'NumberValueDecimal' Property must not be set to null if the 'NullValueAllowed' Property is false.");
                      }
                  }
              }
          }
  
          /// This property gets hidden because it doesn't make sense in the Designer!
          [Browsable(false),
           DefaultValue(0.00)]
          public double ? NumberValueDouble
          {
              get
              {
                  string NumericStr;
  
                  if (!DesignMode)
                  {
                      NumericStr = FTxtNumeric.Text;
  
  
gt:                     if (NumericStr != String.Empty)