如何在richtextbox中加载文本?

How to load text in richtextbox?

我在加载文本时遇到问题 richtextbox.I 构建了整个代码,但我错过了最后一步。文本没有出现在 richtextbox 中,但是当我单击 ctrl+V 时,文本出现了。所有计数我都想在没有点击的情况下加载。如果这个问题已经被问过,我很抱歉。 也许我的问题是有道理的。 谢谢大家的建议 表格 1:

using System;
using System.IO;
using System.Windows.Forms;


namespace projektadresar
{
public partial class Form1 : Form
{

    public static string folderPath = string.Empty;
    Form2 form2;

    public Form1()
    {
        InitializeComponent();
        form2 = new Form2();
    }

    public void button1_Click(object sender, EventArgs e)
    {

        FolderBrowserDialog folder = new FolderBrowserDialog();

        if (folder.ShowDialog() == DialogResult.OK)
        {
            folderPath = Path.GetDirectoryName(folder.SelectedPath);
            textBox1.Text = folderPath;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {

        form2.Show();
        Visible = false;
    }      
}
}

表格 2:

 using System;
 using System.IO;
 using System.Windows.Forms;


namespace projektadresar
{
public partial class Form2 : Form
{
    
    
    public Form2()

    {
        InitializeComponent();
    }

    public static int GetFileCount()
    {
        var fileCount = 0;
        var fileIter = Directory.EnumerateFiles(Form1.folderPath);
        foreach (var file in fileIter)
            fileCount++;
        return fileCount;
    }
    public static int GetDirectoryCount()
    {
        var directoryCount = 0;
        var directoryIter = Directory.EnumerateDirectories(Form1.folderPath);
        foreach (var directory in directoryIter)
            directoryCount++;
        return directoryCount;
    }

    private void richTextBox1_Load(object sender, EventArgs e)
    {

        richTextBox1.Text = "Directories:" + GetDirectoryCount() + Environment.NewLine + "Files:" + GetFileCount();
    }
}
}

您不应该将 form 1 和 form 2 混为一谈。Form1 应该知道的关于 form2 的所有信息都是它完成工作所需的信息。

Form 2 应该不知道是谁创建的。它应该相信它的创建者它为您提供了足够的信息。

显然表格 2 需要一个文件夹路径,其中应该显示一些信息。可能是在显示 form 2 时,文件夹路径发生了变化。因此,只要更改文件夹路径,文本就会自动更新。

class Form2 : Form
{
    private DirectoryInfo folder;

    public Form2()
    {
        InitializeComponent();
    }

    // set by the creator of this form before the form is shown.
    // May be updated while the form is being shown:
    public DirectoryInfo Folder
    {
        get => this.folder.GetFullName();
        set
        {
           if (this.Folder.FullName != value.FullName)
           {
                this.folder = value;
                this.UpdateRichTextbox();
            }           
        }
    }

    private int FileCount => this.Folder.EnumerateFiles().Count();
    private int DirectoryCount => this.Folder.EnumerateDirectories().Count();

    // called when the form is has been loaded (subscribe to event!)
    private void OnFormLoaded(object sender, ...)
    {
        // this.folderPath should have been set, by the owner.
        // update the RichTextBox for the first time
        this.UpdateRichTextbox();
    }

    private void UpdateRichTextBox()
    {
        richTextBox1.Text = "Directories: " + DirectoryCount + Environment.NewLine
                          + "Files: " + FileCount;
    }
}

表格 1:

class Form1 : Form
{
    private Form2 form2 = null;

    private string FolderPath {get; set;}

    private void ShowForm2()
    {
        if (this.form2 != null)
        {
            // decide what to do if form2 already shown
        }
        else
        {
            this.form2 = new Form2
            {
                Folder = new DirectoryInfo(this.FolderPath),
            }
            this.form2 = Show(this); // I am the owner!
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.ShowForm2();
    }
}

关闭 Form1 时不要忘记关闭 Form2(事件 FormClosing)。

在处理 Form1 时不要忘记处理 Form2