Gérer une virgule dans un textbox

Résolu
cs_ammany Messages postés 2 Date d'inscription mardi 22 janvier 2008 Statut Membre Dernière intervention 22 janvier 2008 - 22 janv. 2008 à 04:49
nhervagault Messages postés 6063 Date d'inscription dimanche 13 avril 2003 Statut Membre Dernière intervention 15 juillet 2011 - 22 janv. 2008 à 19:16
Salut , je voudrais gérer une petite calculatrice . Dans mon programme je dois gérer un nombre a virgule ( ex: 10.20) dans une textbox et ensuite faire des calculs avec le chiffre . Mais lorsque je met mon point "." pour faire ma virgule , mon programme n'arrive pas a faire la conversion en double et ca bloque mon programme. voila mon code source.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace EgliseVieNouvelle
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button boutonTaxe1;
        private System.Windows.Forms.Button boutonTaxe2;
        private System.Windows.Forms.TextBox PrixSansTaxe;
        private System.Windows.Forms.TextBox PrixTaxé;
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

       
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.boutonTaxe1 = new System.Windows.Forms.Button();
            this.boutonTaxe2 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.PrixSansTaxe = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.PrixTaxé = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // boutonTaxe1
            //
            this.boutonTaxe1.Font = new System.Drawing.Font("Verdana", 14.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.boutonTaxe1.Location = new System.Drawing.Point(408, 104);
            this.boutonTaxe1.Name = "boutonTaxe1";
            this.boutonTaxe1.Size = new System.Drawing.Size(136, 40);
            this.boutonTaxe1.TabIndex = 0;
            this.boutonTaxe1.Text = "1 Taxe";
            this.boutonTaxe1.Click += new System.EventHandler(this.boutonTaxe1_Click);
            //
            // boutonTaxe2
            //
            this.boutonTaxe2.Font = new System.Drawing.Font("Verdana", 14.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.boutonTaxe2.Location = new System.Drawing.Point(408, 160);
            this.boutonTaxe2.Name = "boutonTaxe2";
            this.boutonTaxe2.Size = new System.Drawing.Size(136, 40);
            this.boutonTaxe2.TabIndex = 2;
            this.boutonTaxe2.Text = "2 Taxes";
            this.boutonTaxe2.Click += new System.EventHandler(this.boutonTaxe2_Click_1);
            //
            // label1
            //
            this.label1.Font = new System.Drawing.Font("Verdana", 15.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label1.Location = new System.Drawing.Point(16, 48);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(432, 32);
            this.label1.TabIndex = 3;
            this.label1.Text = "Veuillez entrer le prix de l\'article";
            //
            // PrixSansTaxe
            //
            this.PrixSansTaxe.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.PrixSansTaxe.Location = new System.Drawing.Point(64, 112);
            this.PrixSansTaxe.Multiline = true;
            this.PrixSansTaxe.Name = "PrixSansTaxe";
            this.PrixSansTaxe.Size = new System.Drawing.Size(264, 48);
            this.PrixSansTaxe.TabIndex = 4;
            this.PrixSansTaxe.Text = "";
            //
            // label2
            //
            this.label2.Font = new System.Drawing.Font("Verdana", 14.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label2.Location = new System.Drawing.Point(72, 200);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(112, 40);
            this.label2.TabIndex = 5;
            this.label2.Text = "Total";
            //
            // PrixTaxé
            //
            this.PrixTaxé.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.PrixTaxé.Location = new System.Drawing.Point(208, 192);
            this.PrixTaxé.Multiline = true;
            this.PrixTaxé.Name = "PrixTaxé";
            this.PrixTaxé.ReadOnly = true;
            this.PrixTaxé.Size = new System.Drawing.Size(120, 40);
            this.PrixTaxé.TabIndex = 6;
            this.PrixTaxé.Text = "";
            //
            // button1
            //
            this.button1.Font = new System.Drawing.Font("Verdana", 14.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.button1.Location = new System.Drawing.Point(408, 216);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(136, 40);
            this.button1.TabIndex = 7;
            this.button1.Text = "Effacer";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(560, 278);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.PrixTaxé);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.PrixSansTaxe);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.boutonTaxe2);
            this.Controls.Add(this.boutonTaxe1);
            this.Name = "Form1";
            this.Text = "Calculatrice Librairie Vie Nouvelle";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void boutonTaxe1_Click(object sender, System.EventArgs e)
        {
           
            String ValeurEntrée = PrixSansTaxe.Text;
            double prix = Double.Parse(ValeurEntrée);
            prix = prix+(prix*0.05);
            this.PrixTaxé.Text = "      "+prix.ToString();
           
        }
        private void boutonTaxe2_Click_1(object sender, System.EventArgs e)
        {
            String ValeurEntrée = PrixSansTaxe.Text;
            double prix = Double.Parse(ValeurEntrée);
            prix = prix+(prix*0.05);
            prix = prix+(prix*0.075);
            this.PrixTaxé.Text = "      "+prix.ToString();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            this.PrixSansTaxe.Text = "    ";
            this.PrixTaxé.Text = "      ";
        }

       

    }
}

Dans mon cas la valeur ValeurEntrée doit etre transfomé en double pour pouvoir effectuer le cacul. merci de votre aide.

Yohan

5 réponses

nhervagault Messages postés 6063 Date d'inscription dimanche 13 avril 2003 Statut Membre Dernière intervention 15 juillet 2011 37
22 janv. 2008 à 09:17
Salut

Pour avoir le caractere de séparation CultureInfo
.
CurrentCulture
.
NumberFormat
.
NumberDecimalSeparator. et , comme ça ton programme fonctionnera dans les deux cas.

Ensuite tu as Decimal.Parse pour faire une conversion, aussi.

Bon courage


 
3
lemmingperceval Messages postés 128 Date d'inscription mardi 18 décembre 2007 Statut Membre Dernière intervention 2 août 2009 6
22 janv. 2008 à 08:19
Salut,

Dans la class String tu as une méthode Replace,
regarde de ce coté:
http://msdn2.microsoft.com/fr-fr/library/czx8s9ts(VS.80).aspx
0
nhervagault Messages postés 6063 Date d'inscription dimanche 13 avril 2003 Statut Membre Dernière intervention 15 juillet 2011 37
22 janv. 2008 à 09:20
PS : Si tu ne veux pas avoir trop de problème d'arrondi, je te conseille d'utiliser des decimals

Car les doubles sont des nombres flottants et donc pas fait pour des opérations bancaires ou de gestion.
0
cs_ammany Messages postés 2 Date d'inscription mardi 22 janvier 2008 Statut Membre Dernière intervention 22 janvier 2008
22 janv. 2008 à 18:36
Salut [auteur/NHERVAGAULT/84425.aspx nhervagault]

je sais pas vraiment ou mettre ton truc la: CultureInfo
.
CurrentCulture
.
NumberFormat
.
NumberDecimalSeparator.
 jai essayé de faire ca

private void boutonTaxe1_Click(object sender, System.EventArgs e)
        {
           
            String ValeurEntrée = PrixSansTaxe.Text;
            double prix = Double.Parse(ValeurEntrée);
            prix = Decimal.Parse(prix);
            prix = prix+(prix*0.05);
            this.PrixTaxé.Text = "      "+prix.ToString();
           
        }
mais ca ne marche pas . Merci pour ton aide
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
nhervagault Messages postés 6063 Date d'inscription dimanche 13 avril 2003 Statut Membre Dernière intervention 15 juillet 2011 37
22 janv. 2008 à 19:16
As-tu trouvé pour valider la réponse

Tu ne dois pas parser deux fois c'est pour passer de string -> decimal
et pas de double -> decimal comme dans ton code

Sinon pour la CultureInfo
.
CurrentCulture
.
NumberFormat
.
NumberDecimalSeparator
Ca te donnes , ou . en fonction du séparateur choisit par ton utilisateur et definit dans les options regional.
Google ou msdn sont la pour t'aider dans son utilistation

Bon courage
0
Rejoignez-nous