如何将数据从第一种形式传输到第二种形式?

How to transport data from first form to the second form?

我无法获取从第一个 main form 到第二个 form 的值。我希望能够在 richtextbox 中以第二种形式显示计算出的文件夹和文件数量。我求求你帮助我。谢谢大家的建议。

表格 1:

using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;


namespace pocetadresaru
{
    public partial class Form1 : Form
    {
        private Form2 form2;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            form2 = new Form2();
            form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
            form2.Location = new Point(Location.X, Location.Y + Height + 80);
            form2.Show();
            textBox1.Focus();
        }

        private void form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Close();
        }

        private void button1_Click_1(object sender, EventArgs e)

        {
            string folderPath= String.Empty;
            var folder = new FolderBrowserDialog();

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

        public static int GetDirectoryCount(string folderPath)
        {
            return Directory.EnumerateDirectories(folderPath).Count();
            
          
        }

        public static int GetFileCount(string folderPath)
        {
          return Directory.EnumerateFiles(folderPath).Count();
            
             
        }


    }
}

表格 2:

using System;
using System.Windows.Forms;

namespace pocetadresaru
{
    public partial class Form2 : Form
    {
        public string Data
        {
            get { return richTextBox1.Text; }
            set { richTextBox1.Text = "Adresář:" +

 **the number of directories listed here** `+ Environment.NewLine + "Soubor:" +` **the number of files listed here**`; }
            }
            public Form2()
            {
                InitializeComponent();
    
            }
        }
    }

像那样为我工作。 (只做目录数的设置)

表格 1


using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private Form2 form2;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string folderPath = String.Empty;
            var folder = new FolderBrowserDialog();

            if (folder.ShowDialog() == DialogResult.OK)
            {
                folderPath = Path.GetFullPath(folder.SelectedPath);
                var count = GetDirectoryCount(folderPath);
                form2.Data = count.ToString();
            }
        }

        public static int GetDirectoryCount(string folderPath)
        {
            return Directory.EnumerateDirectories(folderPath).Count();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            form2 = new Form2();
            form2.Location = new Point(Location.X, Location.Y + Height + 80);
            form2.Show();
        }
    }
}

表格2

using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        private string _data;
        public string Data
        {
            get { return _data; }
            set
            {
                _data = value;
                textBox1.Text = _data;
            }
        }

        public Form2()
        {
            InitializeComponent();
        }
    }
}

或者,如果您真的想在单击时刷新第二个表单:

private void button1_Click(object sender, EventArgs e)
    {
        string folderPath = String.Empty;
        var folder = new FolderBrowserDialog();

        if (folder.ShowDialog() == DialogResult.OK)
        {
            folderPath = Path.GetFullPath(folder.SelectedPath);
            textBox1.Text = folderPath;
            form2.RefreshView(
                GetDirectoryCount(folderPath),
                GetFileCount(folderPath));
        }
    }

在 Form2 中:

public string Data
    {
        get { return richTextBox1.Text; }
        set
        {
            richTextBox1.Text = value ;
        }
    }

public void RefreshView(int dirCount, int filesCount)
    {
        Data = $"Adresář: {dirCount} directories, {filesCount} files";
    }