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 :

Questions sur this.hide() et this.close()


Sujet :

C#

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Par défaut Questions sur this.hide() et this.close()
    Bonjour,

    Je suis débutant c# et je ne comprends pas pourquoi quand je fais un this.hide ou un this.close dans ma Form1_Load ça ne fonctionne pas, alors que en VB.Net quand je faisais un me.hide ou un me.close cela fonctionnait

    Pourriez m'expliquer ?

    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
    private void Form1_Load(object sender, EventArgs e)
            {
    
                this.hide;
    
                string parameters;
                string sourcePath = ConfigurationManager.AppSettings["source"];
                string targetPath = ConfigurationManager.AppSettings["target"];
                MessageBox.Show("Status: Program is started.");
             
                this.close();
    
    
    
    
    }
    Je voudrais lancer mon formulaire en invisible et le fermer une fois son traitement fini.

    D'avance merci pour votre aide

  2. #2
    Membre Expert
    Avatar de PixelJuice
    Homme Profil pro
    Ingénieur .NET & Game Designer
    Inscrit en
    Janvier 2014
    Messages
    661
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Ingénieur .NET & Game Designer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2014
    Messages : 661
    Par défaut
    Bonsoir ,

    ne fonctionne pas si la form est la form principale de ton programme , qui est lancé de cette manière:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Application.Run(new Form());
    dans ton Program.cs

    Il y a une solution pour parvenir a ce que tu veux faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    protected override void OnVisibleChanged(EventArgs e)
    {
        base.OnVisibleChanged(e);
        this.Visible = false;
    }
    Ce code permet de cacher directement ta Form au lancement.

    Tu peux aussi lancer ta forme comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
                Form1 form = new Form1();
                form.Show();
                form.Visible = false;
                Application.Run();
    Et dans ta fonction qui se lance au chargement , mettre

    Pour quitter ton application.

    Mais c'est un peu déconseillé car c'est un arrêt brutal.
    Après, ça dépends de l'utilisation de ton application , est-ce une application qui exécute une tache puis se ferme (dans ce cas la , autant utiliser un programme de type Console) , ou alors une application accessible uniquement par une icône de notification ? Ou un autre cas de figure.

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Par défaut
    c'est une application pour une tache planifiée qui utilise un fichier com de PDFCreator ce fichier n'est pas reconnu en console je pense

    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
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    namespace docToPDF
    {
     
        public partial class Form1 : Form
        {
            private const int maxTime = 20;
     
            private PDFCreator.clsPDFCreator _PDFCreator;
            private PDFCreator.clsPDFCreatorError pErr;
     
            private bool ReadyState;
            public Form1()
            {
     
                InitializeComponent();         
     
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
                string parameters;
                string sourcePath = ConfigurationManager.AppSettings["source"];
                string targetPath = ConfigurationManager.AppSettings["target"];
                MessageBox.Show("Status: Program is started.");
     
     
     
                pErr = new PDFCreator.clsPDFCreatorError();
     
                _PDFCreator = new PDFCreator.clsPDFCreator();
                _PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError);
                _PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady);
     
                parameters = "/NoProcessingAtStartup";
     
                if (!_PDFCreator.cStart(parameters, false))
                {
                    MessageBox.Show("Status: Error[" + pErr.Number + "]: " + pErr.Description);
                }
     
                // récupérer les fichiers du dossier source
                string[] fileNames = Directory.GetFiles(sourcePath, "*.doc*");
                //MessageBox.Show(Convert.ToString(fileNames.Count()));
     
                for (int i = 0; i < (fileNames.Count()); i++)
                {
                    string fname, DefaultPrinter;
                    FileInfo fi;
                    PDFCreator.clsPDFCreatorOptions opt;
     
                    fi = new FileInfo(fileNames[i]);
                    if (fi.Name.Length > 0)
                    {
                        if (fi.Name.IndexOf(".") > 1)
                        {
                            fname = fi.Name.Substring(0, fi.Name.IndexOf("."));
                        }
                        else
                        {
                            fname = fi.Name;
                        }
     
                        opt = _PDFCreator.cOptions;
                        opt.UseAutosave = 1;
                        opt.UseAutosaveDirectory = 1;
                        opt.AutosaveDirectory = targetPath;
                        opt.AutosaveFormat = 0;
                        opt.AutosaveFilename = fname;
                        _PDFCreator.cOptions = opt;
                        _PDFCreator.cClearCache();
                        DefaultPrinter = _PDFCreator.cDefaultPrinter;
                        _PDFCreator.cDefaultPrinter = "PDFCreator";
                        _PDFCreator.cPrintFile(fi.FullName);
                        ReadyState = false;
                        _PDFCreator.cPrinterStop = false;
                        timer1.Interval = maxTime * 1000;
                        timer1.Enabled = true;
                        while (!ReadyState && timer1.Enabled)
                        {
                            Application.DoEvents();
                        }
                        timer1.Enabled = false;
     
                        _PDFCreator.cPrinterStop = true;
                        _PDFCreator.cDefaultPrinter = DefaultPrinter;
                    }
                }
                _PDFCreator.cClose();
                _PDFCreator = null;
                pErr = null;
                Process.Start("taskkill /f /im PDFCreator.exe");
                this.Dispose();
                this.Close();
                Application.ExitThread();
                Application.Exit();
                Environment.Exit(0);
     
            }
     
     
            private void _PDFCreator_eReady()
            {
                _PDFCreator.cPrinterStop = true;
                ReadyState = true;
            }
     
            private void _PDFCreator_eError()
            {
                pErr = _PDFCreator.cError;
            }
     
            private void timer1_Tick(object sender, System.EventArgs e)
            {
                timer1.Enabled = false;
            }
        }
    }
    Ou est-ce que je place

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Form1 form = new Form1();
    form.Show();
    form.Visible = false;
    Application.Run();
    Merci de ton aide

  4. #4
    Membre Expert
    Avatar de PixelJuice
    Homme Profil pro
    Ingénieur .NET & Game Designer
    Inscrit en
    Janvier 2014
    Messages
    661
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Ingénieur .NET & Game Designer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2014
    Messages : 661
    Par défaut
    Dans ton Program.cs , au lieu des lignes qui lance l'application avec ta form directement.

    Sinon je pense qu'en console ça doit passer mais dans le doute , autant garder ton code déja fait .

    Tu peux aussi simplement ajouter cette méthode dans ton code pour rendre la Form invisible , sans besoin de l’appeller car c'est une méthode qui override :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    protected override void OnVisibleChanged(EventArgs e)
    {
        base.OnVisibleChanged(e);
        this.Visible = false;
    }

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Par défaut
    Merci, j'ai bien ma fenêtre invisible mais je n'arrive pas a fermer mon application, à la fin de ma procédure je mets Environment.Exit(0); mais sans succès ...µ

    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
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Configuration;
     
    namespace test
    {
        public partial class Form1 : Form
        {
            private const int maxTime = 20;
     
            private PDFCreator.clsPDFCreator _PDFCreator;
            private PDFCreator.clsPDFCreatorError pErr;
     
            private bool ReadyState;
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
                string sourcePath = ConfigurationManager.AppSettings["source"];
                string targetPath = ConfigurationManager.AppSettings["target"];
     
                pErr = new PDFCreator.clsPDFCreatorError();
     
                _PDFCreator = new PDFCreator.clsPDFCreator();
                _PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError);
                _PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady);
     
                // récupérer les fichiers du dossier source
                string[] fileNames = Directory.GetFiles(sourcePath, "*.doc*");
     
                for (int i = 0; i < (fileNames.Count()); i++)
                {
                    string fname, DefaultPrinter;
                    FileInfo fi;
                    PDFCreator.clsPDFCreatorOptions opt;
     
                    fi = new FileInfo(fileNames[i]);
                        if (fi.Name.Length > 0)
                        {
                            if (fi.Name.IndexOf(".") > 1)
                            {
                                fname = fi.Name.Substring(0, fi.Name.IndexOf("."));
                            }
                            else
                            {
                                fname = fi.Name;
                            }
                            if (!_PDFCreator.cIsPrintable(fi.FullName))
                            {
                                MessageBox.Show("File '" + fi.FullName + "' is not printable!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return;
                            }
                            opt = _PDFCreator.cOptions;
                            opt.UseAutosave = 1;
                            opt.UseAutosaveDirectory = 1;
                            opt.AutosaveDirectory = targetPath;
                            opt.AutosaveFormat = 0;
                            opt.AutosaveFilename = fname;
                            _PDFCreator.cOptions = opt;
                            _PDFCreator.cClearCache();
                            DefaultPrinter = _PDFCreator.cDefaultPrinter;
                            _PDFCreator.cDefaultPrinter = "PDFCreator";
                            _PDFCreator.cPrintFile(fi.FullName);
                            ReadyState = false;
                            _PDFCreator.cPrinterStop = false;
                            timer1.Interval = maxTime * 1000;
                            timer1.Enabled = true;
                            while (!ReadyState && timer1.Enabled)
                            {
                                Application.DoEvents();
                            }
                            timer1.Enabled = false;
                            if (!ReadyState)
                            {
                                MessageBox.Show("Creating printer test page as pdf.\n\r\n\r" +
                                    "An error is occured: Time is up!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            _PDFCreator.cPrinterStop = true;
                            _PDFCreator.cDefaultPrinter = DefaultPrinter;
                        }
                        Application.Exit();
                        this.Close();
                        Environment.Exit(1);
     
                }
            }
     
            private void _PDFCreator_eReady()
            {
                MessageBox.Show( "Status: \"" + _PDFCreator.cOutputFilename + "\" was created!");
                _PDFCreator.cPrinterStop = true;
                ReadyState = true;
            }
     
            private void _PDFCreator_eError()
            {
                pErr = _PDFCreator.cError;
            }
     
            private void timer1_Tick(object sender, System.EventArgs e)
            {
                timer1.Enabled = false;
            }
     
            private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                _PDFCreator.cClose();
                while (_PDFCreator.cProgramIsRunning)
                {
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(100);
                }
                _PDFCreator = null;
                pErr = null;
     
                System.Diagnostics.Process.Start("taskkill /f /im PDFCreator.exe");
     
            }
            //protected override void OnVisibleChanged(EventArgs e)
            //{
            //    base.OnVisibleChanged(e);
            //    this.Visible = false;
            //}
     
        }
    }

  6. #6
    Expert confirmé
    Avatar de StringBuilder
    Homme Profil pro
    Chef de projets
    Inscrit en
    Février 2010
    Messages
    4 197
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Chef de projets
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2010
    Messages : 4 197
    Billets dans le blog
    1
    Par défaut
    Pourquoi en mode console ça marcherait pas ?

    On peut parfaitement accéder à tout ce qu'on veut depuis le mode console, y compris ouvrir des Form...
    Car là je vois pas trop l'intérêt d'ouvrir une Form si c'est pour pas l'afficher, et la fermer dans la foulée...

    Même si l'élément que tu veux utiliser est un contrôle utilisateur, à ce moment, depuis une fenêtre console, rien n'empêche d'instancier une Form contenant le contrôle, sans pour autant l'afficher.

    On n'est certainement pas à 3 cycles CPU près, mais autant faire propre tout de même...

    -- Edit : Et c'est quoi ces DoEvents ? Et le contenu du timer ? Pfiou !!!! Franchement ça fait peur ton machin...

Discussions similaires

  1. Question sur l'utilisation de $this
    Par zerros dans le forum Langage
    Réponses: 3
    Dernier message: 13/03/2013, 11h53
  2. questions sur la POO : new et this
    Par Invité dans le forum Débuter
    Réponses: 23
    Dernier message: 06/11/2010, 01h56
  3. une ou deux question sur this ..
    Par Invité dans le forum Débuter
    Réponses: 4
    Dernier message: 24/01/2010, 10h35
  4. Réponses: 3
    Dernier message: 23/05/2007, 12h35
  5. Question sur le pointeur this
    Par Core8 dans le forum C++
    Réponses: 4
    Dernier message: 29/03/2007, 14h31

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