简体   繁体   English

Expander 控件 - 读取 C# 中的 txt 文件 (WindowsForms)

[英]Expander Control - Read txt-Files in C# (WindowsForms)

I'm trying to add the second.txt file (book2) into Text Expander control, unfortunately without success.我正在尝试将 second.txt 文件 (book2) 添加到 Text Expander 控件中,但不幸的是没有成功。

        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.以下代码只读取一个 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);
            }
        }


I'm trying to add the second.txt file (book2) into Text Expander control, unfortunately without success.我正在尝试将 second.txt 文件 (book2) 添加到 Text Expander 控件中,但不幸的是没有成功。

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 :更改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.还有比这更多的方法,但应该让您更好地理解如何实现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM