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 accès Textbox depuis une classe enfant


Sujet :

C#

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 3
    Points : 2
    Points
    2
    Par défaut Problème accès Textbox depuis une classe enfant
    Bonjour,

    Je viens de me mettre au C#/.Net pour un stage dans une entreprise qui arrive dans 2 mois. Je me suis donc lancé dans un projet relativement simple, faire une sorte de telnet graphique pour manier les Sockets, bref, rien de bien compliqué en soit même si j'en suis pas encore là pour le moment.



    Client.cs
    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
    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;
     
    namespace WindowsFormsApplication1
    {
        public partial class Client : Form
        {
            public Client()
            {
                InitializeComponent();
     
            }
     
            private void m_envoyer_Click(object sender, EventArgs e)
            {
                ecrireTexte(m_message.Text);
            }
     
     
            private void m_message_TextChanged(object sender, EventArgs e)
            {
     
            }
     
            private void m_message_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    ecrireTexte(m_message.Text);
                }
            }
     
            private void label1_Click(object sender, EventArgs e)
            {
     
            }
     
            private void m_serveur_TextChanged(object sender, EventArgs e)
            {
     
            }
     
            private void m_port_TextChanged(object sender, EventArgs e)
            {
     
            }
     
            private void label2_Click(object sender, EventArgs e)
            {
     
            }
     
            private void Client_Load(object sender, EventArgs e)
            {
     
            }            
     
            private void m_connexion_Click(object sender, EventArgs e)
            {
              //  ecrireTexte("test");
     
                if ((m_serveur.Text == "") && (m_port.Text == ""))
                {
                    Connexion plop = new Connexion();
                }
     
                else
                {
                    try
                    {
                        int m_portInt = System.Convert.ToInt32(m_port.Text, 10);
                        Connexion plop = new Connexion(m_serveur.Text, m_portInt);
                    }
     
                    catch (FormatException error)
                    {
                        MessageBox.Show("Le port doit être un nombre valide");
                    }        
     
                }
     
            }
     
            public void ecrireTexte(String texte)
            {
                MessageBox.Show(texte); // La variable texte s'affiche bien 
                m_texte.AppendText(texte + Environment.NewLine);
            }
     
     
        }
    }
    Connexion.cs
    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
     
    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.Net.Sockets;
     
    namespace WindowsFormsApplication1
    {
        public class Connexion : Client
        {
     
            private String m_serveur;
            private int m_port;
     
            public Connexion()
            {
                this.m_serveur = "irc.quakenet.org";
                this.m_port = 6667;
                connec();
            }
     
            public Connexion(string serveur, int port)
            {
                this.m_serveur = serveur;
                this.m_port = port;
                connec();
            }
     
            public void connec()
            {
               ecrireTexte("salut");
     
               // suite du code, mais qui sert à rien pour le moment 
     
            }           
        }
    }
    Client.Designer.cs
    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
     
    namespace WindowsFormsApplication1
    {
        partial class Client
        {
            /// <summary>
            /// Variable nécessaire au concepteur.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
     
            /// <summary>
            /// Nettoyage des ressources utilisées.
            /// </summary>
            /// <param name="disposing">true si les ressources managées doivent être supprimées*; sinon, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            #region Code généré par le Concepteur Windows Form
     
            /// <summary>
            /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
            /// le contenu de cette méthode avec l'éditeur de code.
            /// </summary>
            private void InitializeComponent()
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Client));
                this.m_message = new System.Windows.Forms.TextBox();
                this.m_envoyer = new System.Windows.Forms.Button();
                this.m_texte = new System.Windows.Forms.TextBox();
                this.m_connexion = new System.Windows.Forms.Button();
                this.m_serveur = new System.Windows.Forms.TextBox();
                this.m_port = new System.Windows.Forms.TextBox();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // m_message
                // 
                this.m_message.Location = new System.Drawing.Point(100, 500);
                this.m_message.Name = "m_message";
                this.m_message.ShortcutsEnabled = false;
                this.m_message.Size = new System.Drawing.Size(600, 20);
                this.m_message.TabIndex = 0;
                this.m_message.TextChanged += new System.EventHandler(this.m_message_TextChanged);
                this.m_message.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.m_message_PreviewKeyDown);
                // 
                // m_envoyer
                // 
                this.m_envoyer.Location = new System.Drawing.Point(350, 546);
                this.m_envoyer.Name = "m_envoyer";
                this.m_envoyer.Size = new System.Drawing.Size(100, 30);
                this.m_envoyer.TabIndex = 1;
                this.m_envoyer.Text = "Envoyer";
                this.m_envoyer.UseVisualStyleBackColor = true;
                this.m_envoyer.Click += new System.EventHandler(this.m_envoyer_Click);
                // 
                // m_texte
                // 
                this.m_texte.Location = new System.Drawing.Point(100, 86);
                this.m_texte.Multiline = true;
                this.m_texte.Name = "m_texte";
                this.m_texte.ReadOnly = true;
                this.m_texte.Size = new System.Drawing.Size(600, 397);
                this.m_texte.TabIndex = 2;
                // 
                // m_connexion
                // 
                this.m_connexion.Location = new System.Drawing.Point(40, 32);
                this.m_connexion.Name = "m_connexion";
                this.m_connexion.Size = new System.Drawing.Size(75, 23);
                this.m_connexion.TabIndex = 3;
                this.m_connexion.Text = "Connexion";
                this.m_connexion.UseVisualStyleBackColor = true;
                this.m_connexion.Click += new System.EventHandler(this.m_connexion_Click);
                // 
                // m_serveur
                // 
                this.m_serveur.Location = new System.Drawing.Point(198, 35);
                this.m_serveur.Name = "m_serveur";
                this.m_serveur.Size = new System.Drawing.Size(101, 20);
                this.m_serveur.TabIndex = 4;
                this.m_serveur.TextChanged += new System.EventHandler(this.m_serveur_TextChanged);
                // 
                // m_port
                // 
                this.m_port.Location = new System.Drawing.Point(420, 38);
                this.m_port.Name = "m_port";
                this.m_port.Size = new System.Drawing.Size(100, 20);
                this.m_port.TabIndex = 5;
                this.m_port.TextChanged += new System.EventHandler(this.m_port_TextChanged);
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(145, 35);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(47, 13);
                this.label1.TabIndex = 6;
                this.label1.Text = "Serveur:";
                this.label1.Click += new System.EventHandler(this.label1_Click);
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(385, 38);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(29, 13);
                this.label2.TabIndex = 7;
                this.label2.Text = "Port:";
                this.label2.Click += new System.EventHandler(this.label2_Click);
                // 
                // Client
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(784, 602);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.m_port);
                this.Controls.Add(this.m_serveur);
                this.Controls.Add(this.m_connexion);
                this.Controls.Add(this.m_texte);
                this.Controls.Add(this.m_envoyer);
                this.Controls.Add(this.m_message);
                this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                this.Name = "Client";
                this.Text = "Kikoo";
                this.Load += new System.EventHandler(this.Client_Load);
                this.ResumeLayout(false);
                this.PerformLayout();
     
            }
     
            #endregion
     
            private System.Windows.Forms.TextBox m_message;
            private System.Windows.Forms.Button m_envoyer;
            private System.Windows.Forms.TextBox m_texte;
            private System.Windows.Forms.Button m_connexion;
            private System.Windows.Forms.TextBox m_serveur;
            private System.Windows.Forms.TextBox m_port;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
        }
    }
    Donc mon problème est assez simple :

    J'arrive à afficher du Texte dans la TextBox (m_texte) lorsque je rentre un message et que j'appuie sur entrer/ou clique sur envoyer. Mais lorsque j'essaye d'envoyer une chaine depuis ma classe enfant avec l'instance connec(), la fonction parente reçoit bien le message (J'ai la chaine qui apparait dans une MessageBox) mais je n'arrive pas à ajouter la chaine à ma TextBox m_texte.

    Bref, j'ai du mal à comprendre

    Merci d'avance !

    edit: Bon en fait j'ai identifié le problème je pense, lorsque je fais
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
            public void ecrireTexte(String texte)
            {
     
                m_texte.AppendText(texte + Environment.NewLine);
                //  m_texte.Text += (texte + Environment.NewLine) ; // Fait la même chose
                MessageBox.Show(m_texte.Text);
     
     
            }
    La MessageBox affiche bien la valeur qu'elle est sencé recevoir sauf qu'elle s'ajoute pas avec le contenu de m_texte et ne s'affiche pas non plus graphiquement. :/

  2. #2
    Membre habitué Avatar de bibbi
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    113
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 113
    Points : 137
    Points
    137
    Par défaut
    Salut,

    juste une question logique: pourquoi tu dérives ta classe de connexion de ta classe "client" qui est une form?

    Pour moi ces deux classes ne sont pas liées. Il faut que tu instancies ta classe connexion depuis ta classe client.

Discussions similaires

  1. [PHP 5.3] Attribut privé accessible depuis une classe enfant
    Par slydemusli dans le forum Langage
    Réponses: 3
    Dernier message: 04/07/2012, 14h14
  2. [Débutant] Modifier une TextBox depuis une class [erreur CS0038]
    Par Allan007 dans le forum C#
    Réponses: 4
    Dernier message: 27/05/2012, 10h23
  3. accéder a une textbox depuis une class
    Par Alex35 dans le forum ASP.NET
    Réponses: 10
    Dernier message: 30/07/2008, 16h32
  4. [Débutant] - Accès à GTK depuis une classe
    Par Leobaillard dans le forum GTK+ avec C & C++
    Réponses: 1
    Dernier message: 03/06/2007, 13h09
  5. acces a uneressource bitmap depuis une classe
    Par firejocker dans le forum MFC
    Réponses: 9
    Dernier message: 03/02/2006, 21h48

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