2017-02-04 3 views
0

Auf einem Formular habe ich viele Richtextboxen. Wenn das Formular geladen wird, möchte ich die RichTextBoxes mit Text aus TXT-Dateien füllen. Für jede richTextBox habe ich eine Textdatei und der Name der Datei ist derselbe wie der Name der RichTextBox. Ich habe das versucht, aber es funktioniert nicht, weil eine Komponente keine Loadfile-Funktion hat?Erhalte alle richTextBoxes und lade Textdateien mit dem gleichen Namen

Wie kann ich viele Dateien in verschiedene richtextBoxen laden, wenn ein Formular geladen wird?

private void getAllRichTextboxen(string aFileName) 
     { 
      foreach(Control item in this.Controls) 
      { 
       if (item is GroupBox) //first get the groupboxes, all the richTextBoxes are within a groupBox 
       { 
        foreach (Control aControl in item.Controls) 
        { 
         if (aControl is RichTextBox) //check if it is a richTextBox 
         { 
          if (aControl.Name == aFileName) //if the controle name = the same as the file name then load the file into the RichTextBox 
          { 
           aControl.LoadFile(Settings.applicatiePad + Form_Main.QueryMap + "\\" + aFileName + ".agq", RichTextBoxStreamType.PlainText)); 

           //richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText); 

          } 
         } 
        } 
       } 
      } 
     } 

Antwort

1

Einfach nur wählen alle Steuerelemente mit Typ RichTextBox und setzen Sie den Text-Eigenschaft:

foreach(RichTextBox r in this.Controls.OfType<RichTextBox>()) 
    r.Text = File.ReadAllText(Settings.applicatiePad + Form_Main.QueryMap + "\\" + aFileName + ".agq"); 
0

Ich verstehe es nicht. Die Foreach findet keine Richtextbox. Der Speicherort der Richtextboxen ist tabcontrol-> tabcontrol-> groupbox-> richtextbox. Die foreach findet nur die erste tabcontrol.

Gibt es eine Möglichkeit, ALLE Komponenten in einem Formular zu finden?

Verwandte Themen