2016-04-09 35 views
0

Windows form yapıyorum, içinde bazı metin dosyaları okudum, bir combobox ve ne var Ben istiyorum combobox içinde "Oyunlar" seçeneğini seçtiğimde, richtextbox2 için "game.txt" okuma dosyasında bulunan metnin doldurulmasıdır. Ve diğer seçenekler ve metin dosyaları için de aynı. Bu ifadeyi denedim ama "dizeyi boole dönüştüremiyorum" şeklinde bir hata alıyorum. bir değer == operatörünü kullanmak gerekir başka eşitse C#Metin Kutusu, Visual Studio'da Windows Bileşeninden Seçimi Seçtikten Sonra Nasıl Yapılır Kutu Penceresi 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); 
        } 

cevap

0

kontrol etmek. Yani if deyimi aşağıdaki gibi görünmelidir:

if (comboBox1.Text == "Games") 

DÜZENLEME: Üstü kodunuzu derleme yapacak.

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

da Sorunu düzelttikten nasıl bir açıklama ekleyiniz ve: program çalışmalarını yapmak için bunu böyle bakabilir, senin ComboBox ait SelectedChangedIndex için try/catch blok taşımak gerekir beklendiği gibi ne yapıyorsun. Kod sadece bazı okuyucular için kafa karıştırıcı olabilir –

+0

Tamam, tavsiye için teşekkürler .. – mvlttna

İlgili konular