IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Problème de MessageBox s'ouvre deux fois.


Sujet :

C#

  1. #1
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2012
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2012
    Messages : 102
    Points : 41
    Points
    41
    Par défaut Problème de MessageBox s'ouvre deux fois.
    Mon problème, c'est que ce code, m'affiche deux fois le messagebox, hors, je veux qui s'affiche qu'une seule fois.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
            private void Accueil_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (MessageBox.Show("Voullez-vous vous déconnecter?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    e.Cancel = true;
                else
                {
                    MySqlConnection conn = new MySqlConnection("SERVER = db4free.net; PORT = 3306; DATABASE = drocs; USERID = ; PASSWORD = ");
                    conn.Open();
                    MySqlCommand delete_connexion = new MySqlCommand("DELETE FROM Connexion WHERE userid = '" + user_id + "'", conn);
                    try
                    {
                        delete_connexion.ExecuteNonQuery();
                        conn.Close();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    Application.Exit();
                }
     
            }

  2. #2
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2008
    Messages
    233
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2008
    Messages : 233
    Points : 336
    Points
    336
    Par défaut
    Est-ce que ta Form "Accueil" est celle appelée par l'application ? Application.Run(new Form1()); dans Program.cs ?

    Si oui, essaye de mettre le code contenu dans ton "else" après cette ligne (Application.Run(new Form1()) dans ton Program.cs

    OU

    Retire ton Application.Exit(); de ton else vu qu'à la base, il va quitter l'application si tu ferme la form principale (appelée dans ton Program.cs)
    "Hope for the best, but prepare for the worst."

  3. #3
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2012
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2012
    Messages : 102
    Points : 41
    Points
    41
    Par défaut
    Accueil n'est pas la form principal, quand j'enleve Application.Exit, ça fonctionne bien, ça m'affiche pas une deuxième MessageBox. Mais il quitte pas l'Application en entier, car ma form principal c'est Form1. Car dans la Form1 j'ouvre la fenêtre Accueil, et je cache la Form1, car si je ferme la Form1 ça quitte l'application.
    Bref, ça fonctionne bien, mais l'application et encore en route, car la Form1 est cacher, il est pas fermer d'où la continuité de l'application.

  4. #4
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2008
    Messages
    233
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2008
    Messages : 233
    Points : 336
    Points
    336
    Par défaut
    Il y a quoi dans ta Form1 ? ^^

    Essaye de mettre le code qui se trouve dans le else juste après ton FormAccueil.Show()
    "Hope for the best, but prepare for the worst."

  5. #5
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2012
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2012
    Messages : 102
    Points : 41
    Points
    41
    Par défaut
    Tout ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    using MetroFramework.Forms;
    using MySql.Data.MySqlClient;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Net;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    namespace RPGM_Share_Manager
    {
        public partial class Form1 : MetroForm
        {
            private int ID;
            private string IP;
            private string username;
            private string email;
            private string date_inscrit;
     
            public Form1()
            {
                InitializeComponent();
                this.txtUsername.Text = (string)Properties.Settings.Default.Identifiant;
                this.txtPassword.Text = (string)Properties.Settings.Default.Password;
                if (this.txtUsername.Text != "" && this.txtPassword.Text != "")
                    this.metroToggle1.Checked = true;
            }
     
            private void SaveIdentifiant()
            {
                if (this.metroToggle1.Checked == true)
                {
                    Properties.Settings.Default.Identifiant = (string)this.txtUsername.Text;
                    Properties.Settings.Default.Password = (string)this.txtPassword.Text;
                }
                else
                {
                    Properties.Settings.Default.Identifiant = "";
                    Properties.Settings.Default.Password = "";
                }
                Properties.Settings.Default.Save();
            }
     
            private void metroButton1_Click(object sender, EventArgs e)
            {
                SaveIdentifiant();
                ConnectDataSQL();
            }
     
            private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                Inscription i = new Inscription();
                i.ShowDialog();
            }
     
            static string GetMd5Hash(MD5 md5Hash, string input)
            {
     
                // Convert the input string to a byte array and compute the hash.
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
     
                // Create a new Stringbuilder to collect the bytes
                // and create a string.
                StringBuilder sBuilder = new StringBuilder();
     
                // Loop through each byte of the hashed data 
                // and format each one as a hexadecimal string.
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }
     
                // Return the hexadecimal string.
                return sBuilder.ToString();
            }
     
            // Verify a hash against a string.
            static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
            {
                // Hash the input.
                string hashOfInput = GetMd5Hash(md5Hash, input);
     
                // Create a StringComparer an compare the hashes.
                StringComparer comparer = StringComparer.OrdinalIgnoreCase;
     
                if (0 == comparer.Compare(hashOfInput, hash))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
     
            public int valueID()
            { return this.ID; }
     
            string hash;
            private void ConnectDataSQL()
            {
                using (MD5 md5Hash = MD5.Create())
                {
                    hash = GetMd5Hash(md5Hash, this.txtPassword.Text);
     
                    Console.WriteLine("The MD5 hash of " + this.txtPassword.Text + " is: " + hash + ".");
     
                    Console.WriteLine("Verifying the hash...");
     
                    if (VerifyMd5Hash(md5Hash, this.txtPassword.Text, hash))
                    {
                        Console.WriteLine("The hashes are the same.");
                    }
                    else
                    {
                        Console.WriteLine("The hashes are not same.");
                    }
                }
                var userHost = Dns.GetHostName();
                var GetComputer_InternetIP = Dns.GetHostEntry(userHost).AddressList[2].ToString();
                MySqlConnection conn = new MySqlConnection("SERVER = db4free.net; PORT = 3306; DATABASE = drocs; USERID = ; PASSWORD = ");
                conn.Open();
                MySqlCommand ip_user = new MySqlCommand("UPDATE User SET ip ='" + GetComputer_InternetIP.ToString() + "' WHERE username='" + this.txtUsername.Text + "'", conn);
                MySqlCommand verify_user = new MySqlCommand("SELECT userid, username, password, email, date, ip FROM User WHERE username='" + this.txtUsername.Text + "' and password = '" + hash + "'", conn);
                MySqlCommand add_friend = new MySqlCommand("SELECT ID_Personne_Principal, ID_Personne_Ami FROM List_Amis WHERE ID_Personne_Principal = '" + ID + "'", conn);
                try
                {
                    MySqlDataReader reader_verify_user = verify_user.ExecuteReader();
                    if (reader_verify_user.HasRows)
                    {
                        while (reader_verify_user.Read())
                        {
                            ID = reader_verify_user.GetInt32(0);
                            username = reader_verify_user.GetString(1);
                            email = reader_verify_user.GetString(3);
                            date_inscrit = reader_verify_user.GetString(4);
                            IP = reader_verify_user.GetString(5);
                        }
                        reader_verify_user.Close();
                        conn.Close();
                        conn.Open();
                        ip_user.ExecuteNonQuery();
                        conn.Close();
                        conn.Open();
                        MySqlCommand verify_user_connexion = new MySqlCommand("SELECT userid FROM Connexion WHERE userid = '" + ID + "'", conn);
                        MySqlDataReader reader_user_connexion = verify_user_connexion.ExecuteReader();
                        if (reader_user_connexion.HasRows)
                        {
                            reader_user_connexion.Close();
                            if (MessageBox.Show("Vous êtes déjà connecter!\nSe reconnecter?", "Server", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                            {
                                MySqlCommand delete_connexion = new MySqlCommand("DELETE FROM Connexion WHERE userid = '" + ID + "'", conn);
                                try
                                {
                                    delete_connexion.ExecuteNonQuery();
                                    conn.Close();
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            reader_user_connexion.Close();
                            conn.Close();
                            conn.Open();
                            MySqlCommand insert_connexion = new MySqlCommand("INSERT INTO Connexion(IP, userid, last_connexion) VALUE('" + GetComputer_InternetIP.ToString() + "', '" + ID + "', '" + DateTime.Now.ToString() + "')", conn);
                            insert_connexion.ExecuteNonQuery();
                            conn.Close();
                            this.Hide();
                            Accueil a = new Accueil(ID, username, email);
                            a.Show();
                        }
                    }
                    else
                    {
                        MessageBox.Show("L'identifiant ou le mot de passe est incorrect!", "Connexion", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    conn.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                    throw;
                }
            }
        }
    }

  6. #6
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2012
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2012
    Messages : 102
    Points : 41
    Points
    41
    Par défaut
    En faite le code permet d'enlever le client dans la BDD Connexion.
    Etant donner qui ferme l'Application il fonctionne, mais c'est juste le MessageBox qui s'affiche deux fois.
    Sinon tant pis, je vais laisser comme ça.

  7. #7
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2008
    Messages
    233
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2008
    Messages : 233
    Points : 336
    Points
    336
    Par défaut
    Citation Envoyé par iVarlix Voir le message
    En faite le code permet d'enlever le client dans la BDD Connexion.
    Etant donner qui ferme l'Application il fonctionne, mais c'est juste le MessageBox qui s'affiche deux fois.
    Sinon tant pis, je vais laisser comme ça.
    La seule explication que je vois, c'est sue lors de la fermeture de la form Accueil, tu lui dis de fermer l'application (avant le Accueil.FormClosed), donc du coup, quand tu appelles Application.Exit(), ta form Accueil est toujours ouverte et du coup, il ferme toutes les fenêtres encore ouvertes.

    L'appel de Application.Exit() sur un FormClosing() n'est pas bon, il serait mieux le mettre dans FormClosed()
    "Hope for the best, but prepare for the worst."

  8. #8
    Expert confirmé
    Avatar de wallace1
    Homme Profil pro
    Administrateur systèmes
    Inscrit en
    Octobre 2008
    Messages
    1 966
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Administrateur systèmes
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 966
    Points : 4 005
    Points
    4 005
    Billets dans le blog
    7
    Par défaut
    Bonjour,

    En lieu et place de Application.exit il faut mettre si tu penses avoir bien libéré toutes les ressources liées à ton application (bdd ouverte, fichier ouvert, ...etc....) :

    A+

  9. #9
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 184
    Points : 5 755
    Points
    5 755
    Par défaut
    Tu peux te désabonner avant de quitter ton application
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    this.FormClosing -= Accueil_FormClosing;
    Application.Exit();
    Ou libérer ta fenêtre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    this.Dispose();
    Application.Exit();
    Ou utiliser une variable Static, pour vérifier si ton application est en train de s’arrêter.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public static bool IsExiting = false;

  10. #10
    Membre du Club
    Homme Profil pro
    Inscrit en
    Septembre 2012
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Septembre 2012
    Messages : 102
    Points : 41
    Points
    41
    Par défaut
    Merci ce code ma bien résolu:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    this.FormClosing -= Accueil_FormClosing;
    Application.Exit();
    Merci beaucoup!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Excel.exe s'ouvre deux fois
    Par Tomacup dans le forum Excel
    Réponses: 4
    Dernier message: 20/02/2013, 19h46
  2. Problème: Numéro de socket attribué deux fois
    Par bactria dans le forum Réseau
    Réponses: 4
    Dernier message: 04/01/2012, 19h15
  3. [WD14] connexion/reconnexion ouvre deux fois l'application
    Par heiti dans le forum WinDev
    Réponses: 16
    Dernier message: 22/09/2009, 20h13
  4. Problème pour imprimer deux fois
    Par jodan33 dans le forum Débuter
    Réponses: 6
    Dernier message: 07/04/2008, 15h52
  5. Pop-up qui s'ouvre deux fois
    Par arnapou dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 26/06/2006, 08h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo