2016-04-09 2 views
0

Je crée un formulaire Windows, j'ai lu des fichiers texte, j'ai une liste déroulante et ce que Je veux, c'est quand je sélectionne l'option "Jeux" dans la combobox est pour le richtextbox2 à remplir avec le texte qui se trouve dans le fichier lu "game.txt". Et la même chose pour les autres options et fichiers texte. J'ai essayé cette instruction if mais j'ai une erreur indiquant que "je ne peux pas convertir la chaîne en booléen".Comment faire apparaître le texte dans la zone de texte, après avoir sélectionné l'option de la zone de liste déroulante dans Visual Studio Windows Form C#

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
       { 

       } 

       private void richTextBox2_TextChanged(object sender, EventArgs e) 
       { 

       } 

       private void Form1_Load(object sender, EventArgs e) 
       { 
        //Fill combobox with names. 
        comboBox1.Items.Add("Games"); 
        comboBox1.Items.Add("Operating"); 
        comboBox1.Items.Add("Information"); 
         try 
        { 
        //read text from text files. 
         string game = File.ReadAllText(@"game.txt"); 
         string operate = File.ReadAllText(@"operate.txt"); 
         string information = File.ReadAllText(@"info.txt"); 

//if you select Games option in combobox, fill text box with text from read file "game.txt". 
    if (comboBox1.Text = "Games") 
        { 
         richTextBox2.Text = game; 
        } 
        } 
        catch (Exception ex) 
        { 
         //display error if files not found. 
         MessageBox.Show(" " + ex.Message); 
        } 

Répondre

0

En C# pour vérifier si une valeur est égale à une autre, vous devez utiliser l'opérateur ==. Donc, votre déclaration if devrait ressembler à:

if (comboBox1.Text == "Games") 

EDIT: Au-dessus rendra votre compilation de code. Afin de rendre votre programme fonctionne comme prévu, vous devez déplacer votre bloc try/catch à la SelectedChangedIndex de votre ComboBox, il peut ressembler à ceci:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     //if you select Games option in combobox, fill text box with text from read file "game.txt". 
     if (comboBox1.Text == "Games") 
     { 
      richTextBox2.Text = File.ReadAllText(@"game.txt"); 
     } 
     else if (comboBox1.Text == "Operate") 
     { 
      richTextBox2.Text = File.ReadAllText(@"operate.txt"); 
     } 
     else if (comboBox1.Text == "Information") 
     { 
      richTextBox2.Text = File.ReadAllText(@"info.txt"); 
     } 
    } 
    catch (Exception ex) 
    { 
     //display error if files not found. 
     MessageBox.Show(" " + ex.Message); 
    } 
} 
0
//Fill the default comboBox item. 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     comboBox1.Items.Add("Games"); 
     comboBox1.Items.Add("Operating"); 
     comboBox1.Items.Add("Information"); 
    } 

    public void Write(string category) 
    { 

     switch (category) 
     { 
      //if you select Games option in combobox, do work Games() method. 
      case "Games": Games(); break; 
      case "Operating": Operating(); break; 
      case "Information": Information(); break; 
      default: 
       break; 
     } 
    } 

    //if you select Games , read file "game.txt" with together Read() method. 
    public void Games() 
    { 
     richTextBox1.Text = Read("games"); 
    } 

    //if you select Operating , read file "operating.txt" with together Read() method. 
    public void Operating() 
    { 
     richTextBox1.Text = Read("operating"); 
    } 

    //if you select Information , read file "information.txt" with together Read() method. 
    public void Information() 
    { 
     richTextBox1.Text = Read("information"); 
    } 

    //File reading process 
    public string Read(string fileName) 
    { 
     string data = File.ReadAllText(Application.StartupPath + @"\" + fileName + ".txt"); 
     return data; 
    } 

    //Selected item process 
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     Write(comboBox1.SelectedItem.ToString()); 
    } 
+0

S'il vous plaît ajouter aussi une explication de la façon dont vous avez résolu le problème et que fais tu. Le code seul peut être déroutant pour certains lecteurs –

+0

OK, merci pour les conseils .. – mvlttna