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

 .NET Discussion :

Problème évènement formClosing jamais levée [C#]


Sujet :

.NET

  1. #1
    Membre actif
    Profil pro
    Développeur informatique
    Inscrit en
    Mars 2010
    Messages
    336
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2010
    Messages : 336
    Points : 227
    Points
    227
    Par défaut Problème évènement formClosing jamais levée
    Bonjours quand je clique sur le bouton close de ma forme cet event et sensé se produire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    private void btCancel_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    je dispose d'un textbox ou je verifie si il est vide ou non :
    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
     
     private void TxtValidating(object sender, CancelEventArgs e)
            {
                btValid.Enabled = IsValid();
            }
     
            private void SetErrorProviderWithCount(Control control, string message)
            {
                if (string.IsNullOrWhiteSpace(message))
                {
                    if (errorProvider.GetError(control) != "")
                        nbError--;
                }
                else
                    if (errorProvider.GetError(control) == "")
                        nbError++;
     
                errorProvider.SetError(control, message);
            }
     
            private bool HasErrorProvider()
            {
                return nbError != 0;
            }
     
            private bool IsValid()
            {
                if (string.IsNullOrWhiteSpace(txtServerAddress.Text))
                {
                    SetErrorProviderWithCount(txtServerAddress, "Field cannot be empty.");
                }
                else
                {
                    SetErrorProviderWithCount(txtServerAddress, "");
                }
     
                if (string.IsNullOrWhiteSpace(txtDefaultLoungeName.Text))
                {
                    SetErrorProviderWithCount(txtDefaultLoungeName, "Field cannot be empty.");
                }
                else
                {
                    SetErrorProviderWithCount(txtDefaultLoungeName, "");
                }
     
                if (string.IsNullOrWhiteSpace(txtServerInfo.Text))
                {
                    SetErrorProviderWithCount(txtServerInfo, "Field cannot be empty.");
                }
                else
                {
                    SetErrorProviderWithCount(txtServerInfo, "");
                }
     
                return !HasErrorProvider();
            }
    si la textbox n a pas le focus ok il ferme la form, si la textbox a le focus il fait la validation trouve une erreur (s il y en a une) et ne ferme pas la forme car ne passe jamais dans l'event click je comprend pas pourquoi? il devrait fermer ma forme quand même !
    dans le doute voici le code complet :
    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
     
            private int nbError = 0;
     
            public frm()
            {
                InitializeComponent();
     
                //this.ActiveControl = lbServerAddress;
     
                btValid.Enabled = false;            
            }
     
            private void frm_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (btValid.Enabled)
                {
                    if (IsValid())
                    {
     
                    }
                }
            }
     
            private void btCancel_Click(object sender, EventArgs e)
            {
                this.Close();
            }
     
            private void TxtValidating(object sender, CancelEventArgs e)
            {
                btValid.Enabled = IsValid();
            }
     
            private void SetErrorProviderWithCount(Control control, string message)
            {
                if (string.IsNullOrWhiteSpace(message))
                {
                    if (errorProvider.GetError(control) != "")
                        nbError--;
                }
                else
                    if (errorProvider.GetError(control) == "")
                        nbError++;
     
                errorProvider.SetError(control, message);
            }
     
            private bool HasErrorProvider()
            {
                return nbError != 0;
            }
     
            private bool IsValid()
            {
                if (string.IsNullOrWhiteSpace(txtServerAddress.Text))
                {
                    SetErrorProviderWithCount(txtServerAddress, "Field cannot be empty.");
                }
                else
                {
                    SetErrorProviderWithCount(txtServerAddress, "");
                }
     
                if (string.IsNullOrWhiteSpace(txtDefaultLoungeName.Text))
                {
                    SetErrorProviderWithCount(txtDefaultLoungeName, "Field cannot be empty.");
                }
                else
                {
                    SetErrorProviderWithCount(txtDefaultLoungeName, "");
                }
     
                if (string.IsNullOrWhiteSpace(txtServerInfo.Text))
                {
                    SetErrorProviderWithCount(txtServerInfo, "Field cannot be empty.");
                }
                else
                {
                    SetErrorProviderWithCount(txtServerInfo, "");
                }
     
                return !HasErrorProvider();
            }

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 749
    Points
    39 749
    Par défaut
    C'est normal en fait... L'évènement Validating empêche le contrôle de perdre le focus, donc le bouton Cancel ne peut pas prendre le focus, et donc être cliqué

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

Discussions similaires

  1. [PHP-JS] Problème évènement OnClick en PHP
    Par JmL40 dans le forum Langage
    Réponses: 7
    Dernier message: 17/07/2007, 14h22
  2. problème évènement sorting
    Par babafredo dans le forum ASP.NET
    Réponses: 5
    Dernier message: 04/07/2007, 09h32
  3. Réponses: 21
    Dernier message: 01/02/2006, 09h17
  4. [servlet]problème de variable jamais nulle
    Par omega dans le forum Servlets/JSP
    Réponses: 2
    Dernier message: 18/03/2004, 09h31
  5. Problème évènements clavier
    Par julie20 dans le forum Langage
    Réponses: 3
    Dernier message: 24/09/2003, 12h39

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