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++/CLI Discussion :

Visual C++ Express 2010. Les Chaines de caractéres. Que faut il utiliser ?


Sujet :

C++/CLI

  1. #1
    Membre du Club Avatar de Gilles57-H-G
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 88
    Points : 62
    Points
    62
    Par défaut Visual C++ Express 2010. Les Chaines de caractéres. Que faut il utiliser ?
    La classe CString de Microsoft n'étant pas présente sur la version Express de Visual C++.

    Je me suis tourné vers la classe string.
    J'ai fait
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #include <string>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include "stdlib.h"
    #include <stdio.h>
    #include <sstream>
     
     
    using namespace std;
     
    string chaine;
     
    chaine = "Apparition";
    Dans le Form1.h.
    Fonctionne, ne crée pas d'erreur.


    Je voudrais remplir une ListBox.

    L'exemple de code suivant illustre la création d'un contrôle ListBox qui affiche plusieurs éléments.
    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
     string chaine;
     chaine = "Apparition";
     
     // Create an instance of the ListBox.
       ListBox^ listBox1 = gcnew ListBox;
     
       // Set the size and location of the ListBox.
       listBox1->Size = System::Drawing::Size( 200, 100 );
       listBox1->Location = System::Drawing::Point( 10, 10 );
     
       // Add the ListBox to the form.
       this->Controls->Add( listBox1 );
     
       // Set the ListBox to display items in multiple columns.
       listBox1->MultiColumn = true;
     
       // Set the selection mode to multiple and extended.
       listBox1->SelectionMode = SelectionMode::MultiExtended;
     
       // Shutdown the painting of the ListBox as items are added.
       listBox1->BeginUpdate();
     
       // Loop through and add 50 items to the ListBox.
       for ( int x = 1; x <= 50; x++ )
       {
     
    ///////////////////////////////////////
     
    listBox1->Items->Add( String::Format( "Item {0}", x ) );
     
     
       //  Je voudrais faire apparaitre chaine qui est de type string
               //     dans la fonction Add() de la listBox1
     
                                                                          }
       listBox1->EndUpdate();
     
       // Select three items from the ListBox.
       listBox1->SetSelected( 1, true );
       listBox1->SetSelected( 3, true );
       listBox1->SetSelected( 5, true );
     
       // Display the second selected item in the ListBox to the console.
       System::Diagnostics::Debug::WriteLine( listBox1->SelectedItems[ 1 ] );
     
       // Display the index of the first selected item in the ListBox.
       System::Diagnostics::Debug::WriteLine( listBox1->SelectedIndices[ 0 ] );
    J'ai mis en commentaire l'endroit ou je voudrais faire apparaitre le string, si c'est possible.

    J'ai tout essayé, je n'ai jammais réussi à faire rentrer un string dans cette listBox.


    Je chercherai aussi a remplir un TreeNode.

    Merci.

  2. #2
    Rédacteur

    Avatar de ram-0000
    Homme Profil pro
    Consultant en sécurité
    Inscrit en
    Mai 2007
    Messages
    11 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultant en sécurité
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mai 2007
    Messages : 11 517
    Points : 50 367
    Points
    50 367
    Par défaut
    Une idée comme cela, est ce que ton projet ne serait pas en Unicode et dans ce cas, une string ASCII ne va pas.

    Il faudrait utiliser une string unicode (qui s'utilise excatement de la même manière qu'une std:string)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    std:wstring = L"Application";
    Il est probable que cela soit exactement le même problème pour le tree node.

    Au fait, la prochaine fois, poste le message d'erreur, cela évitera de jouer aux devinettes.

  3. #3
    Membre du Club Avatar de Gilles57-H-G
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 88
    Points : 62
    Points
    62
    Par défaut
    Non cela ne fonctionne pas
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    std:wstring toto = L"Apparition";
     
     listBox1->Items->Add(  toto);

    Donne :
    c:\documents and settings\administrateur\mes documents\visual studio 2010\projects\ty\ty\Form1.h(139): error C2664: 'System::Windows::Forms::ListBox::ObjectCollection::Add'*: impossible de convertir le paramètre 1 de 'std::wstring' en 'System::Object ^'


    Si je met
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    string c;
    c = "chaine";
     
      listBox1->Items->Add( String::Format( c ));
    Donne :
    c:\documents and settings\administrateur\mes documents\visual studio 2010\projects\ty\ty\Form1.h(140): error C2665: 'System::String::Format'*: aucune des 5 surcharges n'a pu convertir tous les types d'arguments


    Si je met
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     string c;
     c = "chaine";
     
     
      listBox1->Items->Add (c);
    J'ai :
    c:\documents and settings\administrateur\mes documents\visual studio 2010\projects\ty\ty\Form1.h(140): error C2664: 'System::Windows::Forms::ListBox::ObjectCollection::Add'*: impossible de convertir le paramètre 1 de 'std::string' en 'System::Object ^'



    Je met ce qui fonctionne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    for ( int x = 1; x <= 2; x++ )
     
       {   listBox1->Items->Add( String::Format( "Item {0}", x) );   }
    Affiche :

    Item 1
    Item 2

  4. #4
    Rédacteur

    Avatar de ram-0000
    Homme Profil pro
    Consultant en sécurité
    Inscrit en
    Mai 2007
    Messages
    11 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultant en sécurité
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mai 2007
    Messages : 11 517
    Points : 50 367
    Points
    50 367
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    listBox1->Items->Add(  toto.c_str());
    ?

  5. #5
    Rédacteur/Modérateur
    Avatar de JolyLoic
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2004
    Messages
    5 463
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Yvelines (Île de France)

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

    Informations forums :
    Inscription : Août 2004
    Messages : 5 463
    Points : 16 213
    Points
    16 213
    Par défaut
    En fait, tu ne programmes pas en C++, mais en C++/CLI, un langage utilisé avant tout pour faire le pont entre le monde C++ et le monde microsoft.NET (c#,...).

    Dans ce langage coexistent des objets C++ (comme std::string) et des objets .NET (comme System::String). Une solution court terme pour ton problème est de convertir l'une en l'autre. Regarde la fonction marshal_as, qui permet de convertir les types entre les deux mondes.

  6. #6
    Membre éclairé

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    426
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations forums :
    Inscription : Octobre 2008
    Messages : 426
    Points : 827
    Points
    827
    Par défaut
    Utilises la classe String au lieu de string : http://msdn.microsoft.com/fr-fr/libr...em.string.aspx

    Regarde ma réponse à http://www.developpez.net/forums/d10...faut-utiliser/

  7. #7
    Membre du Club Avatar de Gilles57-H-G
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 88
    Points : 62
    Points
    62
    Par défaut
    Résolu.

    Exemple pour faire apparaitre une chaine de caractères avec Visual C++ Express.

    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
    String^ chaine;
    chaine = "Apparition";
     
     // Create an instance of the ListBox.
       ListBox^ listBox1 = gcnew ListBox;
     
       // Set the size and location of the ListBox.
       listBox1->Size = System::Drawing::Size( 200, 100 );
       listBox1->Location = System::Drawing::Point( 10, 10 );
     
       // Add the ListBox to the form.
       this->Controls->Add( listBox1 );
     
       // Set the ListBox to display items in multiple columns.
       listBox1->MultiColumn = true;
     
       // Set the selection mode to multiple and extended.
       listBox1->SelectionMode = SelectionMode::MultiExtended;
     
       // Shutdown the painting of the ListBox as items are added.
       listBox1->BeginUpdate();
     
       // Loop through and add 50 items to the ListBox.
       for ( int x = 1; x <= 50; x++ )
       {
     
    ///////////////////////////////////////
     
    listBox1->Items->Add( chaine );
     
     
       //  Je voudrais faire apparaitre chaine qui est de type string
               //                  dans la fonction Add()
     
                                                                          }
       listBox1->EndUpdate();
     
       // Select three items from the ListBox.
       listBox1->SetSelected( 1, true );
       listBox1->SetSelected( 3, true );
       listBox1->SetSelected( 5, true );
     
       // Display the second selected item in the ListBox to the console.
       System::Diagnostics::Debug::WriteLine( listBox1->SelectedItems[ 1 ] );
     
       // Display the index of the first selected item in the ListBox.
       System::Diagnostics::Debug::WriteLine( listBox1->SelectedIndices[ 0 ] );

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 08/12/2010, 18h57
  2. Réponses: 8
    Dernier message: 03/12/2010, 01h32
  3. question sur les chaines de caractères
    Par pierrOPSG dans le forum C
    Réponses: 5
    Dernier message: 13/04/2006, 18h55
  4. les chaines de caractères
    Par mrtatou dans le forum C
    Réponses: 4
    Dernier message: 25/01/2006, 14h18
  5. xsl : test sur les chaine de caractère
    Par yos dans le forum XSL/XSLT/XPATH
    Réponses: 1
    Dernier message: 13/07/2005, 15h43

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