If/else调用控件

本文关键字:控件 调用 else If | 更新日期: 2023-09-27 18:11:29

我是一个初学者,希望有人能告诉我,如果我使用正确的方式来使用if/else,而使用线程。

在我的表单用户有一个复选框,这将决定哪一组应用程序将被安装。如果用户选择无线启用,它将安装奥德赛客户端。

请不要生吃我,我才刚刚开始。

所以问题是:

  1. 上的if/else语句是最好的方式?

  2. 如何调用复选框控件?我知道如何通过调用来使进度条工作,但对复选框感到困惑。

      这是最好的方式来挂钩进度条考虑到我正在处理wceload>?或者有可能将它与我们的预设值连接起来。即25;35% ?

谢谢Program1

名称空间{公共部分类Form1: Form{

    private Thread myThread;
    delegate void UpdateProgressDelegate();

    private bool workerThreadDone;

    public Form1()
    {
        InitializeComponent();
    }

    public string Message = "";
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    delegate void UpdateStatusInvoker(string statusText);
    private void MyWorkerThread()
    {

        while (!workerThreadDone)
        {
            Thread.Sleep(1000);
            using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"Software'Apps'Microsoft .NET CF 3.5"))
                if ((Key != null && checkBox2.Checked))
                {
                    MessageBox.Show("Framework Exist, Wireless apps Enabled");
                    appcenter();
                    oac();

                }
                else
                    if ((Key != null && !checkBox2.Checked))
                    {
                        MessageBox.Show("Framework exist and NOT installing wireless apps.");

                        appcenter();
                    }

                    else
                        if (checkBox2.Checked)
                        {
                            MessageBox.Show("Framework not found,Installing with wireless");
                            NETCFv351();
                            NETCFv352();
                            sqlce1();
                            sqlce2();
                            sqlce3();
                            appcenter();
                            oac();

                        }
                        else
                            if (!checkBox2.Checked)
                            {
                                MessageBox.Show("Installing everything with NO wireless");
                                NETCFv351();
                                NETCFv352();
                                sqlce1();
                                sqlce2();
                                sqlce3();
                                appcenter();

                            }
        }
    }
    public void skipframework_progress()
    {
        this.statusBar1.Text = ("Framework Found!, Skipping");
        this.progressBar1.Value = 35;
    }
    // NETCFv35.Messages.EN.wm.cab
    public void NETCFv351()
    {
        this.progressBar1.Invoke(new UpdateProgressDelegate(framework_progress));
        Thread.Sleep(1000);
        string cab = "''Program Files''cabs''NETCFv351.cab";
        string parm = @"/delete 1 """ + cab + @""" /silent";
        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();
        p.StartInfo = psi;
        p.Start();
        p.WaitForExit();
    }
    public void framework_progress()
    {
        this.statusBar1.Text = ("Installing NET3.5 Framework");
        this.progressBar1.Value = 10;
    }
    //NETCFv35.Messages.EN.wm.cab
    public void NETCFv352()
    {
        string cab = "''Program Files''cabs''NETCFv352.cab";
        string parm = @"/delete 1 """ + cab + @""" /silent";
        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();
        p.StartInfo = psi;
        p.Start();
        p.WaitForExit();
    }
    //sqlce.ppc.wce5.armv4i.CAB
    public void sqlce1()
    {
        string cab = "''Program Files''cabs''sqlce1.cab";
        string parm = @"/delete 1 """ + cab + @""" /silent";
        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();
        p.StartInfo = psi;
        p.Start();
        p.WaitForExit();
    }
    //sqlce.repl.ppc.wce5.armv4i.CAB
    public void sqlce2()
    {
        string cab = "''Program Files''cabs''sqlce2.cab";
        string parm = @"/delete 1 """ + cab + @""" /silent";
        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();
        p.StartInfo = psi;
        p.Start();
        p.WaitForExit();
    }
    public void sqlce3()
    {
        //sqlce.dev.ENU.ppc.wce5.armv4i.CAB
        string cab = "''Program Files''cabs''sqlce3.cab";
        string parm = @"/delete 1 """ + cab + @""" /silent";
        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();
        p.StartInfo = psi;
        p.Start();
        p.WaitForExit();
        Thread.Sleep(1000);
        this.progressBar1.Invoke(new UpdateProgressDelegate(framework_done_progress));
    }

    public void framework_done_progress()
    {
        this.statusBar1.Text = ("NET3.5 Framework Installed");
        this.progressBar1.Value = 30;
    }


    public void appcenter()
    {
        string cab = "''Program Files''cabs''appcenter.cab";
        string parm = @"/delete 1 """ + cab + @""" /silent";

        Thread.Sleep(700);
        ProcessStartInfo psi1 = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();
        p.StartInfo = psi1;
        p.Start();
        p.WaitForExit();
        Thread.Sleep(700);
        this.progressBar1.Invoke(new UpdateProgressDelegate(appcenter_progress));
    }
    public void appcenter_progress()
    {
        this.statusBar1.Text = ("Appcenter Installed");
        this.progressBar1.Value = 35;
    }

    public void oac()
    {
        string cab = "''Program Files''cabs''oac.cab";
        string parm = @"/delete 1 """ + cab + @""" /silent";
        ProcessStartInfo psi1 = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();
        p.StartInfo = psi1;
        p.Start();
        p.WaitForExit();
        Thread.Sleep(700);
        this.progressBar1.Invoke(new UpdateProgressDelegate(oac_progress));
    }
    public void oac_progress()
    {
        this.statusBar1.Text = ("Odyssey Installed");
        this.progressBar1.Value = 65;
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        workerThreadDone = false;
        myThread = new Thread(MyWorkerThread);
        myThread.IsBackground = true;
        myThread.Start();

    }
}

}

If/else调用控件

我重新编写了代码…看看现在是否更有意义:

public partial class Form1 : Form
{
    private Thread myThread = null;
    public Form1()
    {
        InitializeComponent();
    }
    public delegate void ProgressDelegate(string message, int percent);
    public void DisplayProgess(string message, int percent)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new ProgressDelegate(DisplayProgess), new Object[] { message, percent });
        }
        else
        {
            this.statusBar1.Text = message;
            this.progressBar1.Value = percent;
        }
    }
    public delegate bool IsCheckedDelegate(CheckBox cb);
    public bool IsChecked(CheckBox cb)
    {
        if (cb.InvokeRequired)
        {
            return (bool)cb.Invoke(new IsCheckedDelegate(IsChecked), new Object[] { cb });
        }
        else
        {
            return cb.Checked;
        }
    }
    private void button1_Click_1(object sender, EventArgs e)
    {
        if (myThread == null)
        {
            button1.Enabled = false;
            myThread = new Thread(MyWorkerThread);
            myThread.IsBackground = true;
            myThread.Start();
        }
    }
    private void MyWorkerThread()
    {
        using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"Software'Apps'Microsoft .NET CF 3.5"))
        {
            if (Key != null)
            {
                if (this.IsChecked(checkBox2))
                {
                    MessageBox.Show("Framework Exist, Wireless apps Enabled");
                    appcenter();
                    oac();
                }
                else
                {
                    MessageBox.Show("Framework exist and NOT installing wireless apps.");
                    appcenter();
                }
            }
            else
            {
                if (this.IsChecked(checkBox2))
                {
                    MessageBox.Show("Framework not found,Installing with wireless");
                    NETCFv351();
                    NETCFv352();
                    sqlce1();
                    sqlce2();
                    sqlce3();
                    appcenter();
                    oac();
                }
                else
                {
                    MessageBox.Show("Installing everything with NO wireless");
                    NETCFv351();
                    NETCFv352();
                    sqlce1();
                    sqlce2();
                    sqlce3();
                    appcenter();
                }
            }
        }
    }
    public void RunCabAndWait(string CabToRun)
    {
        string cab = "''Program Files''cabs''" + CabToRun;
        string parm = @"/delete 1 """ + cab + @""" /silent";
        Process.Start(new ProcessStartInfo(@"wceload.exe", parm)).WaitForExit();
    }
    // NETCFv35.Messages.EN.wm.cab
    public void NETCFv351()
    {
        this.DisplayProgess("Installing NET3.5 Framework", 10);
        Thread.Sleep(1000);
        this.RunCabAndWait("NETCFv351.cab");
    }
    //NETCFv35.Messages.EN.wm.cab
    public void NETCFv352()
    {
        this.RunCabAndWait("NETCFv352.cab");
    }
    //sqlce.ppc.wce5.armv4i.CAB
    public void sqlce1()
    {
        this.RunCabAndWait("sqlce1.cab");
    }
    //sqlce.repl.ppc.wce5.armv4i.CAB
    public void sqlce2()
    {
        this.RunCabAndWait("sqlce2.cab");
    }
    //sqlce.dev.ENU.ppc.wce5.armv4i.CAB
    public void sqlce3()
    {
        this.RunCabAndWait("sqlce3.cab");
        Thread.Sleep(1000);
        this.DisplayProgess("NET3.5 Framework Installed", 30);
    }
    public void appcenter()
    {
        Thread.Sleep(700);
        this.RunCabAndWait("appcenter.cab");
        Thread.Sleep(700);
        this.DisplayProgess("Appcenter Installed", 35);
    }
    public void oac()
    {
        this.RunCabAndWait("oac.cab");
        Thread.Sleep(700);
        this.DisplayProgess("Odyssey Installed", 65);
    }
}