Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have problem with loading text in richtextbox.I have the whole code built but I miss the last step. The text does not appear in the richtextbox but when I click ctrl+V the text appears. All count I would like tol oad without click.
Thank everybody for all advice

What I have tried:

Form 1:
C#
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;
        }      
    }
}

Form 2:
C#
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();
        }
    }
}
Posted
Updated 11-Oct-20 22:27pm
v2
Comments
Richard MacCutchan 12-Oct-20 4:17am    
Where is the call to richTextBox1_Load?
dejf111 12-Oct-20 4:36am    
oooo thank you I´m donkey

1 solution

First, assuming Form1 is the main form, and you hide it, how will you show it again unless Form2 has a reference to it ? Do you really need to show a second Form ?
private void richTextBox1_Load(object sender, EventArgs e)
{
    richTextBox1.Text = "Directories:" + GetDirectoryCount() + Environment.NewLine + "Files:" + GetFileCount();
}
A RichTextBox has no 'Load Event: this should be the Form2 'Load Event.

Why let the user show Form2 without making sure you have a valid Path:
public void button1_Click(object sender, EventArgs e)
{
    button2.Enabled = false;

    FolderBrowserDialog folder = new FolderBrowserDialog();

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

        button2.Enabled = true;
    }
    else
    {
        // throw an error ?
    }
}
You can use these forms to simplify your Form2 methods:

return Directory.GetFiles(folderPath).Length;

return Directory.GetDirectories(folderPath).Length;
 
Share this answer
 
Comments
dejf111 12-Oct-20 4:36am    
Thank you very much
dejf111 12-Oct-20 4:40am    
I have questions, how would I create a reference back to form1? Could this be solved without form2?
BillWoodruff 12-Oct-20 5:09am    
My preference would be to not hide Form1, and make Form1 the 'Owner of Form2: then, Form2 will always appear on top of Form1. You could also assign Form2 a 'Closing event in Form1 that would make Form1 visible again.

However, unless I know why you want to show a second Form, I can't give you better advice :)
dejf111 12-Oct-20 8:03am    
until now I've discovered that it's miscalculating :D

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