Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
edit: original message deleted
Edit number 284 148
So I redid it, I don't know if it was correct.Now I'm having trouble fitting my results into form2.maybe it's all wrong and I didn't get the OriginalGriff article Transferring information between two forms, Part 1: Parent to Child[^]
Thank you so much for all your help and patience OriginalGriff
Now I have trouble transmitting information to Form2

What I have tried:

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ář:" + GetDirectoryCount + Environment.NewLine + "Soubor:" + GetFileCount; }
        }
        public Form2()
        {
            InitializeComponent();

        }
    }
}
Posted
Updated 14-Oct-20 19:00pm
v4
Comments
Richard MacCutchan 14-Oct-20 7:13am    
Get rid of Form2 and do everything on Form1. It will be less confusing.

1 solution

You are doing it very wrong: your Form2 shouldn't know that Form1 even exists, but less what properties or fields it has.
As soon as I see the keywords public static it's obvious you are going wrong - you shouldn't need to be using static variables in code this simple, and you shouldn't make fields or controls public at all!

Start by reading this: Transferring information between two forms, Part 1: Parent to Child[^]
Form1 is your Parent, Form2 is your Child.
Then create the property in Form2 and set it when you create the instance. Now your forms are "decoupled" so only the Parent needs to know anything about the Child.

Then fix your null problem - and that's simple as well:
C#
FolderPath = Path.GetDirectoryName(folder.SelectedPath);

Path.GetDirectoryName expects a path to a file, not a folder - and it returns the folder that file is in. So when you pass it a folder instead of a file, it "strips out" the final folder name:
C:\docs\today becomes C:\docs
C:\docs becomes C:
C:\ becomes null
The SelectedPath property of the FolderBrowserDialog is already a folder, not a file - you don't need to get a folder name from that!
 
Share this answer
 
Comments
dejf111 14-Oct-20 2:09am    
When I read the article, can I suggest here how I solve it?
OriginalGriff 14-Oct-20 2:16am    
Help yourself!
dejf111 14-Oct-20 2:18am    
in translation into my language it means two things and I don't know which one you mean
OriginalGriff 14-Oct-20 2:21am    
And without any idea what you are saying, neither do I!

Go back to your original comment "When I read the article, can I suggest here how I solve it?" and write that again in your own language - use plenty of detail. Then use Google Translate to convert that to English and post teh translation here.
dejf111 14-Oct-20 2:35am    
I hope I didn't upset you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900