Home > other >  Expander Control - Read txt-Files in C# (WindowsForms)
Expander Control - Read txt-Files in C# (WindowsForms)

Time:10-02

I‘m trying to add the second .txt file (book2) into Text Expander control, unfortunately without success.

        public Form1()
        {
            InitializeComponent();
            CreateAccordion();
        }
        private void CreateAccordion(string book1)
        {
            Accordion accordion = new Accordion();
            accordion.Size = new Size(400, 350);
            accordion.Left = 10;

            Expander expander1 = new Expander();
            expander1.BorderStyle = BorderStyle.FixedSingle;
            ExpanderHelper.CreateLabelHeader(expander1, "Book1", SystemColors.ActiveBorder);
            CreateContentLabel(expander1, book1, 140);
            accordion.Add(expander1);

            Expander expander2 = new Expander();
            expander2.BorderStyle = BorderStyle.FixedSingle;
            ExpanderHelper.CreateLabelHeader(expander2, "Book2", SystemColors.ActiveBorder);
            CreateContentLabel(expander2, "book2", 120);
            accordion.Add(expander2);
            this.Controls.Add(accordion);
        }

The following code reads only a single txt.file. Can anyone help me please?

        private void CreateAccordion()
        {
            ReadTxtFiles();
        }

        private void ReadTxtFiles()
        {
            string path = @"C:\..\..\READBOOKS";
            string[] files = new string[] { "book1.txt", "book2.txt" };

            foreach (string file in files)
            {
                string fullPath = Path.Combine(path, file);
                string booktxt = File.ReadAllText(fullPath);
                string book1 = booktxt;

                CreateAccordion(book1);
            }
        }


CodePudding user response:

I‘m trying to add the second .txt file (book2) into Text Expander control, unfortunately without success.

The main reason behind this is because you're only passing the string from your first book, you'll need to pass all of the text.

private void ReadTxtFiles()
        {
            string path = @"C:\..\..\READBOOKS";
            string[] files = new string[] { "book1.txt", "book2.txt" };
            List<string> books = new List<string>();

            foreach (string file in files)
            {
                string fullPath = Path.Combine(path, file);
                string booktxt = File.ReadAllText(fullPath);
                books.Add(booktxt);                                        
            }

            CreateAccordion(books);
        }

Change the signature of CreateAccordion:

private void CreateAccordion(List<string) books)
        {
            Accordion accordion = new Accordion();
            accordion.Size = new Size(400, 350);
            accordion.Left = 10;

            Expander expander1 = new Expander();
            expander1.BorderStyle = BorderStyle.FixedSingle;
            ExpanderHelper.CreateLabelHeader(expander1, "Book1", SystemColors.ActiveBorder);
            CreateContentLabel(expander1, books[0], 140);
            accordion.Add(expander1);

            Expander expander2 = new Expander();
            expander2.BorderStyle = BorderStyle.FixedSingle;
            ExpanderHelper.CreateLabelHeader(expander2, "Book2", SystemColors.ActiveBorder);
            CreateContentLabel(expander2, books[1], 120);
            accordion.Add(expander2);
            this.Controls.Add(accordion);
        }

Please note that I'm accessing index's here, you may want to check that the index exist before trying to access it. Also there's more way's than this, but should give you a better understanding to accomplish this.

  •  Tags:  
  • c#
  • Related