dejf111
dejf111

Reputation: 39

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

I can't get the value from the first main form to the second form. I would like to be able to show the calculated number of folders and files in the second form in the richtextbox. I beg you to help me. Thank you to everyone for their advice.

Form1:

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


    }
}

Form2:

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

Upvotes: 0

Views: 69

Answers (2)

Cem.S
Cem.S

Reputation: 1111

Or if you really want to refresh the second form on click :

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

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

Upvotes: 2

eistzitrone
eistzitrone

Reputation: 51

Worked for me like that. (Only doing the setting of the directory count)

Form1


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

Upvotes: 1

Related Questions