Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to set the textbox format to numberic
Message
De
14/03/2011 09:46:23
 
 
À
14/03/2011 05:06:41
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows XP
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01503574
Message ID:
01503591
Vues:
39
Hi Alex

This isn't very easy to do, or at least I never found an easy way.
These are some methods I put together based on what I read in Microsoft's documentation.
They work pretty well but you have to be aware of them when you create blank rows in the tables.. the data types/values must make sense or these methods will throw exceptions.
   #region String/Decimal Conversion


    private void SetTextBoxCurrencyBinding(TextBox txtbox, quote ds, string fieldname)
    {
      Binding b = new Binding("Text", ds, fieldname);
      {
        b.Format += new ConvertEventHandler(DecimalToCurrencyString);
        b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
      }
      txtbox.DataBindings.Clear();
      txtbox.DataBindings.Add(b);

    }


    private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
    {
      // The method converts only to string type..
      if (cevent.DesiredType != typeof(string)) return;

      // Use the ToString method to format the value as currency ("c").
      cevent.Value = ((decimal)cevent.Value).ToString("N2");
    }


    private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
    {
      // The method converts back to decimal type only. 
      if (cevent.DesiredType != typeof(decimal)) return;

      // Converts the string back to decimal using the static Parse method.
      cevent.Value = Decimal.Parse(cevent.Value.ToString(),
      NumberStyles.Currency, null);
    }



    private void CurrencyStringToDollars(object sender, ConvertEventArgs cevent)
    {
      // The method converts back to decimal type only. 
      if (cevent.DesiredType != typeof(Int32)) return;

      // Converts the string back to Integer using the static Parse method.
      cevent.Value = Int32.Parse(cevent.Value.ToString(),
      NumberStyles.Currency, null);
    }
    private void SetTextBoxDollarsBinding(TextBox txtbox, DataSet ds, string fieldname)
    {
      Binding b = new Binding("Text", ds, fieldname);
      {
        b.Format += new ConvertEventHandler(DecimalToDollarsString);
        b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
      }
      txtbox.DataBindings.Clear();
      txtbox.DataBindings.Add(b);
    }

    private void DecimalToDollarsString(object sender, ConvertEventArgs cevent)
    {
      // The method converts only to string type. Test this using the DesiredType.
      if (cevent.DesiredType != typeof(string)) return;

      // Use the ToString method to format the value as currency ("c").
      cevent.Value = ((Decimal)cevent.Value).ToString("N0");
    }

    #endregion
Anyone who does not go overboard- deserves to.
Malcolm Forbes, Sr.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform