【问题标题】:How to transport data from first form to the second form?如何将数据从第一种形式传输到第二种形式?
【发布时间】:2020-10-15 05:27:55
【问题描述】:

我无法获得从第一个 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();
    
            }
        }
    }

【问题讨论】:

  • form2.Data = "what ever you want"; form2.Show();
  • @TheGeneral 当我想查看GetDirectoryCount() 它不起作用

标签: c# forms return-value


【解决方案1】:

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

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";
    }

【讨论】:

  • 感谢您的代码,但Data = $"Adresář: {dirCount} directories, {filesCount} files"; 得到错误:FormatException 未处理
  • 您能否更明确地发布异常详细信息?我刚刚测试了这段代码,没有看到任何错误发生。也尝试用String.Format("Adresář: {0} directories, {1} files", dirCount, filesCount) 替换字符串插值,如果有帮助请告诉我们
  • 是的,它现在可以工作,但是如果我更详细地打开文件夹会出现一个问题,例如:C:\Users\mrazekd\Downloads 它会显示错误消息跨度>
  • 道歉只是抄录现在它工作完美,但我认为它计算错误
【解决方案2】:

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

表格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();
        }
    }
}

Form2

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();
        }
    }
}

【讨论】:

  • textBox1.Text = _data; 无法正常工作“无法将类型 'string' 隐式转换为 'System.Windows.Forms.RichTextBox'”
  • 你是在设置richTextBox的Text属性吗?对我来说,它也适用于richTextBox
  • oooooo 我太笨了,我没写richTextbox1.Text:D
猜你喜欢
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
相关资源
最近更新 更多