this.Hide(); Form1加载的事件 [英] this.Hide(); Event with Form1 load

查看:130
本文介绍了this.Hide(); Form1加载的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候后你好。



我正在使用这段代码,我刚刚建成。它不起作用,为什么?因为它正在处理程序中第一个表单的负载。



注意:该程序有2x表格。我希望它加载如果cfg.ini(行xxxxload = 1)

它将隐藏主窗体,打开新窗体。

但是,它不起作用因为代码在表单开始显示之前被触发,所以,任何可以修复的东西?



这是我使用的代码。



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.

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

推荐答案



有几件事情,首先你要做的事情所有都在捆绑中,因此您需要将它们拆分为执行单个组件的任务,例如:

1)加载第一个表单;

2)启动后台工作程序以读取该文件,以便form1响应;

3)读取文件;

4)在后台工作程序完成后,调用委托,以便资源不被捕获交叉线程操作;

5)处理主线程中的结果;

6)启动第二个表单;

7)显示新表格;

8)隐藏主表格。



参见下面的代码:

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





这很像甜蜜。



问候

Jegan



This works like a sweet.

Regards
Jegan


这篇关于this.Hide(); Form1加载的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆