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

VC++ .NET Discussion :

String to Label


Sujet :

VC++ .NET

  1. #1
    Membre confirmé
    Inscrit en
    Juillet 2007
    Messages
    124
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 124
    Par défaut String to Label
    Bonjours je veux convertir un string en Label

    je veux faire ceci parce que je dois faire la même chose 216 fois

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    e->Graphics->DrawString(label3->Text,label3->Font,Brushes::Black,label3->Location.X,label3->Location.Y);
    			e->Graphics->DrawString(label4->Text,label4->Font,Brushes::Black,label4->Location.X,label4->Location.Y);
    			e->Graphics->DrawString(label5->Text,label5->Font,Brushes::Black,label5->Location.X,label5->Location.Y);
    			e->Graphics->DrawString(label6->Text,label6->Font,Brushes::Black,label6->Location.X,label6->Location.Y);
    			e->Graphics->DrawString(label7->Text,label7->Font,Brushes::Black,label7->Location.X,label7->Location.Y);
    			e->Graphics->DrawString(label8->Text,label8->Font,Brushes::Black,label8->Location.X,label8->Location.Y);
    			e->Graphics->DrawString(label9->Text,label9->Font,Brushes::Black,label9->Location.X,label9->Location.Y);
    			e->Graphics->DrawString(label10->Text,label10->Font,Brushes::Black,label10->Location.X,label10->Location.Y);
    			e->Graphics->DrawString(label11->Text,label11->Font,Brushes::Black,label11->Location.X,label11->Location.Y);
    j'avais pensé faire ca comme ca mais ca fonctionne pas

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    for (int i=0 ; i<216 ; i++) 
    			{
    			   String ^NomLabel = "label"+i;
    			   Label^ Lb = safe_cast< Label^ >(NomLabel);
    			   e->Graphics->DrawString(Lb->Text,Lb->Font,Brushes::Black,Lb->Location.X,Lb->Location.Y);
    			}
    Est-ce quelqu'un pourrais m'aider S.V.P Merci

    TrollTop c++.net

  2. #2
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    ce n'est pas du tout comme ca qu'il faut faire
    si tu veux créer un label dynamiquement, il faudrait plutot faire un truc du genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Label ^monLabel = gcnew Label();
    monLabel->Text = "ma chaine";
    // ajouter le label à la form (ou à un panel)
    this->Controls->Add(monLabel); // this étant la form

  3. #3
    Membre confirmé
    Inscrit en
    Juillet 2007
    Messages
    124
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 124
    Par défaut
    ca peut pas fonctionner ca c'est pas ca que je veux faire

    je veux que monLabel = label0 , label1, label2 , label3, label4 ect...jusqu'à label 216 avec tout ce qu'ils contiennent le Text , Font , Location tout quoi une copie conforme. un après l'autre dans un for

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    e->Graphics->DrawString(label1->Text,label1->Font,Brushes::Black,label1->Location.X+200,label1->Location.Y);
    e->Graphics->DrawString(label2->Text,label2->Font,Brushes::Black,label2->Location.X+300,label2->Location.Y);
    e->Graphics->DrawString(label3->Text,label3->Font,Brushes::Black,label3->Location.X,label3->Location.Y);
    e->Graphics->DrawString(label4->Text,label4->Font,Brushes::Black,label4->Location.X,label4->Location.Y);
    e->Graphics->DrawString(label5->Text,label5->Font,Brushes::Black,label5->Location.X,label5->Location.Y);
    e->Graphics->DrawString(label6->Text,label6->Font,Brushes::Black,label6->Location.X,label6->Location.Y);
    e->Graphics->DrawString(label7->Text,label7->Font,Brushes::Black,label7->Location.X,label7->Location.Y);
    e->Graphics->DrawString(label8->Text,label8->Font,Brushes::Black,label8->Location.X,label8->Location.Y);
    e->Graphics->DrawString(label9->Text,label9->Font,Brushes::Black,label9->Location.X,label9->Location.Y);
    e->Graphics->DrawString(label10->Text,label10->Font,Brushes::Black,label10->Location.X,label10->Location.Y);
    e->Graphics->DrawString(label11->Text,label11->Font,Brushes::Black,label11->Location.X,label11->Location.Y);
    j'espère que tu comprend

    TrollTop c++.net

  4. #4
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    non, j'ai rien compris

  5. #5
    Membre confirmé
    Inscrit en
    Juillet 2007
    Messages
    124
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 124
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    e->Graphics->DrawString(label1->Text,label1->Font,Brushes::Black,label1->Location.X,label1->Location.Y);
    e->Graphics->DrawString(label2->Text,label2->Font,Brushes::Black,label2->Location.X,label2->Location.Y);
    e->Graphics->DrawString(label3->Text,label3->Font,Brushes::Black,label3->Location.X,label3->Location.Y);
    e->Graphics->DrawString(label4->Text,label4->Font,Brushes::Black,label4->Location.X,label4->Location.Y);
    e->Graphics->DrawString(label5->Text,label5->Font,Brushes::Black,label5->Location.X,label5->Location.Y);
    e->Graphics->DrawString(label6->Text,label6->Font,Brushes::Black,label6->Location.X,label6->Location.Y);
    e->Graphics->DrawString(label7->Text,label7->Font,Brushes::Black,label7->Location.X,label7->Location.Y);
    e->Graphics->DrawString(label8->Text,label8->Font,Brushes::Black,label8->Location.X,label8->Location.Y);
    e->Graphics->DrawString(label9->Text,label9->Font,Brushes::Black,label9->Location.X,label9->Location.Y);
    e->Graphics->DrawString(label10->Text,label10->Font,Brushes::Black,label10->Location.X,label10->Location.Y);
    e->Graphics->DrawString(label11->Text,label11->Font,Brushes::Black,label11->Location.X,label11->Location.Y);
    Je veux pas à avoir à faire ces lignes 216 fois

    je veux le faire dans un for i

    for (int i=0 ; i<216 ; i++)
    {
    e->Graphics->DrawString(label+i->Text,label+i->Font,Brushes::Black,label+i->Location.X,label+i->Location.Y);
    }

    je veux le faire une fois . comme ca je vais avoir 3 lignes de programme au lieu de 216 lignes

    si tu comprend pas . Je sais pas comment te l'expliquer autrement.

    TrollTop c++.net

  6. #6
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    ca peut pas marcher comme ca ...

    je vois deux possibilités assez simple à mettre en oeuvre pour trouver ton textBox.

    Avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this->Controls->Find(nomDuTextBox, true);
    sachant que ca va te renvoyer un tableau de textBox contenant 1 seul textbox, donc il faudra acceder à l'élément 0

    soit en bouclant sur tous les controles et en testant si le controle est de type textBox, dans le meme genre que dans la faq C++/CLI http://dotnet.developpez.com/faq/cpp...ypeidentity#is

    (et au pire, si jamais tu as des textbox que tu ne veux pas traiter, tu pourras faire un filtre sur le nom ...)

  7. #7
    Membre confirmé
    Inscrit en
    Juillet 2007
    Messages
    124
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 124
    Par défaut
    té sur que tu parle de la même affaire que moi Parce que moi c'est des Label et non des TextBox . pis je veux pas les trouver. Je sais que je veux les labels de 0 à 215 .

    TrollTop c++.net

  8. #8
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    au temps pour moi, mais c'est pareil, le principe est le meme pour un textbox ou pour un label

    il ne s'agit de "les retrouver" que pour pouvoir agir dessus.

    tu ne peux pas utiliser une variable dans un nom de variable

  9. #9
    Membre confirmé
    Inscrit en
    Juillet 2007
    Messages
    124
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 124
    Par défaut
    Si je Comprend bien . Je fais ça comme ça

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    array<Label ^> ^ MonLabel = gcnew array<Label ^>(1);
     
    		for (int i=0 ; i<216 ; i++) 
    			{
    			   String ^NomLabel = "label"+i;
    			   MonLabel = this->Controls->Find(NomLabel, true);
    			   e->Graphics->DrawString(MonLabel[0]->Text,MonLabel[0]->Font,Brushes::Black,MonLabel[0]->Location.X,MonLabel[0]->Location.Y);
    			}
    ca me fait 2 erreurs les voici

    error C2440: '=' : impossible de convertir de 'cli::array<Type,dimension> ^' en 'cli::array<Type,dimension> ^'
    1> with
    1> [
    1> Type=System::Windows::Forms::Control ^,
    1> dimension=1
    1> ]
    1> and
    1> [
    1> Type=System::Windows::Forms::Label ^,
    1> dimension=1
    1> ]

  10. #10
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    for (int i = 0 ; i < 256 ; i++)
    {
     array<Control ^> ^labelATrouver = Controls->Find(String::Format("label{0}", i), true);
     if (labelATrouver != nullptr && labelATrouver->Length > 0)
     {
    	 // faire qqchose avec le label 
    	 labelATrouver[0]->Text = .......;
     }
    }

  11. #11
    Membre confirmé
    Inscrit en
    Juillet 2007
    Messages
    124
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 124
    Par défaut
    Merci Beaucoup , Tu est un As du C++.Net

    TrollTop c++.net

  12. #12
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

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

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 395
    Par défaut
    Ne dis pas C++.Net, ça sonne trop Managed C++...
    Là, tu bosses en C++/CLI...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

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

Discussions similaires

  1. Affichage d'un melange d'entier et string dans un label
    Par zodiak66 dans le forum Débuter
    Réponses: 4
    Dernier message: 03/06/2011, 22h28
  2. Comparaison de label et de string
    Par SonnyFab dans le forum ASP.NET
    Réponses: 6
    Dernier message: 22/06/2010, 14h59
  3. Adresser un Label à partir d'une chaine string
    Par Fenixecu dans le forum Windows Forms
    Réponses: 4
    Dernier message: 23/02/2009, 09h29
  4. Réponses: 1
    Dernier message: 13/09/2007, 11h56
  5. Utilisation de string et dates comme labels d'axes
    Par _moebius_ dans le forum MATLAB
    Réponses: 3
    Dernier message: 15/06/2007, 11h52

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