Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello after regards .

I was using this code , that I've just built. It does not work, why? cause of it's working on the load of first form in the program.

Note: the program has 2x forms. I want it to load if the cfg.ini ( line xxxxload = 1 )
it will hide the main form , Open the new form.
But, it does not work because the code get fired before the form start to be shown, so, any thing that it could be fixed?

Here's the code I use.

C#
Base.iniFile cfg = new Base.iniFile(".\\xxxx\\cfg.ini");
            try
            {

                if (cfg.readValue("Xxxx", "XxxxLoad") == "1")
                {
                    Folder.second_form f = new Folder.second_form();
                    this.Hide();
                    f.Show();
                }
                else
                {

                }
            }
            catch
            {
                MessageBox.Show("Xxxx", "Xxxxtitle", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
Posted
Updated 3-Mar-13 11:27am
v2
Comments
[no name] 3-Mar-13 14:59pm    
1. Explain what "it does not work" means.
2. Pick better names for your variables and keys.
3. Post your real code that you are having trouble with.
Mostafa Metwally 3-Mar-13 15:17pm    
It should be

Folder.second_form f = new Folder.second_form();
f.Show();
this.Hide();
Phoenix1337 3-Mar-13 15:24pm    
Does not work too. It shows the both forms.
Sergey Alexandrovich Kryukov 3-Mar-13 18:40pm    
The question does not make much sense because you complain that your code is called to early while not showing how it was called. Instead, you could simply explain what do you want to achieve and why.
—SA

1 solution

Hi,
there are few things, first of all you are doing all in a bundle, so you need to split them into tasks that does individual components such as:
1) Load First Form;
2) Start the Background worker to read the file, so that the form1 is responsive;
3) Read the file;
4) On background worker completed, invoke a delegate so that the resources are not caught in the cross thread operation;
5) process the results in the main thread;
6) initiate the second form;
7) show the new form;
8) hide the main form.

See code below:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace SecondFormOnFormLoad
{
    public partial class Form1 : Form
    {
        private delegate void BGWorkerCompletedCallback();

        private string _fileName = @"D:\Projects\VS_Test_Projects\CodeProjectTests\SecondFormOnFormLoad\bin\Debug\cfg.ini";
        private string _errorMsg;
        private List<string> _contentsOfIniFile;

        /// <summary>
        /// Basic const.
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            // give some time just to show that the form has loaded.
            System.Threading.Thread.Sleep(500);
            ReadFile();
        }

        private void ReadFile()
        {
            _errorMsg = null;
            FileInfo fi = new FileInfo(_fileName);
            if (!fi.Exists)
            {
                _errorMsg = "File doesn't exists!";
            }
            else
            {
                _contentsOfIniFile = new List<string>();
                using (StreamReader sr = new StreamReader(_fileName, System.Text.Encoding.Default))
                {
                    string str = "";
                    while ((str = sr.ReadLine()) != null)
                    {
                        _contentsOfIniFile.Add(str);
                    }
                }
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            BGWCompletedHandler();
        }


        private void BGWCompletedHandler()
        {
            new BGWorkerCompletedCallback(TaskCompleted).Invoke();
        }

        private void TaskCompleted()
        {
            if (_contentsOfIniFile.Count > 0)
            {
                CheckContentAndOpenNewForm();
            }
            else if (!string.IsNullOrEmpty(_errorMsg))
            {
                MessageBox.Show(_errorMsg);
            }
        }

        private void CheckContentAndOpenNewForm()
        {
            string[] parts = _contentsOfIniFile[0].Split(new char[] { '=' });

            if (parts.Length > 0)
            {
                if (parts[0].Contains("Hello World"))
                {
                    Form2 frm = new Form2();

                    if (parts[1].Trim().Equals("1"))
                    {
                        frm.Show(this);
                        this.Hide();
                    }
                }
            }
        }
        
    }
}


This works like a sweet.

Regards
Jegan
 
Share this answer
 

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