处理checbox和线程时出错 [英] error dealing with checbox and threading

查看:83
本文介绍了处理checbox和线程时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的复选框有问题.
我正在使用启动画面等作品制作启动表格,
主要形式首先运行,包含飞溅代码
我的主要形式代码是这样的

hi i have problem with checkbox.
i am making a splash form with works like a splash screen,
the main form is runned first with contains coding of splashing
my main form code is like this

public partial class Form2 : Form
    {
        
        public mainsoftware()
        {

                Thread t = new Thread(new ThreadStart(splashscreen));
                t.Start();
                Thread.Sleep(5000);

                InitializeComponent();
                t.Abort();


        }
        public void splashscreen()
        {
            Application.Run(new splash_form());
        }




但是我想要一个复选框来确定是否必须使用启动屏幕,因此我将代码放在这样的checkstate中




but i want a checkbox to determine that splash screen must be used or not so i put that code in checkstate like this

if(CHECKBOX1.Checked)
{
{

                Thread t = new Thread(new ThreadStart(splashscreen));
                t.Start();
                Thread.Sleep(5000);

                InitializeComponent();
                t.Abort();


        }
        public void splashscreen()
        {
            Application.Run(new splash_form());
        }
}
ELSE
{
Application.Run(new mainsoftware());
}




但是出现了一些我不知道要解决的错误
错误是在if语句(对象引用未设置为对象的实例)上.




but there is some error is coming which i dont know to solve
error is at the if statement which is Object reference not set to an instance of an object.

推荐答案

您必须先访问复选框或任何其他控件称为InitializeComponent -它设置控件实例,因此在此之前,您始终会收到对象未设置为对象的实例"错误.



您正在尝试做的是有一个可选的启动屏幕,该屏幕在下次启动应用程序时是否显示或不显示?

是的,我正在尝试这样做
我的英语不太好,所以有一个你还不了解的问题.
兄弟我该怎么办?
首先显示一个初始屏幕,但是当用户登录并选中启动时不显示初始屏幕的复选框时,初始屏幕将不会显示给他.
现在您已经了解了.你能帮我这个忙吗?
预先感谢先生"


好的.首先要做的是将Checkbox状态保存在某个地方.最简单的方法是使用.NET和VS中内置的设置.
1)在项目下的解决方案资源管理器"窗格中查看.
2)找到属性"并将其展开.
3)在属性下,您应该找到"Settings.Settings"-双击它.
4)在出现的窗口中,通过以下方式添加新设置:
4.1)将名称"下的设置"更改为"ShowSplashScreen".
4.2)将类型"更改为布尔".
4.3)将值"设置为真"
关闭窗口-如果需要,保存更改.

在代码中,当您要决定是否显示启动画面时:
You can''t access checkboxes or any other controls before you have called InitializeComponent - it sets up the control instances, so until then you will always get "Object not set to an instance of an object" errors.



"Is what you are trying to do have an optional splash screen that does or doesn''t show up *next* time the application is started?

yesss i am trying to do that
my english is not so good so there is problem that you havent understood.
so bro. how can i do that?
firstly a splash screen is shown but when user login and checked the checkbox that do not show the splash screen at startup then splash screen will not been shown to him.
now you have understood. can you help me out in this.
thanks in advance sir"


OK. The first thing to do is to save the Checkbox status somewhere. The easiest way to do this is to use the Settings built into .NET and VS.
1) Look in the Solution Explorer pane, under you project.
2) Find "Properties" and expand it.
3) Under properties you should find "Settings.Settings" - double click it.
4) In the window that appears, add a new setting, by:
4.1) Change "Setting" under "Name" to "ShowSplashScreen".
4.2) Change "Type" to "Bool".
4.3) Set "Value" to "True"
Close the window - save changes if it asks.

In your code, when you want to decide if you should show a splashscreen:
if (Properties.Settings.Default.ShowSplashScreen)
    {
    // Show your splash screen
    ...
    }


更改复选框时,保存新值:


When you change the Checkbox, save the new value:

Properties.Settings.Default.ShowSplashScreen = splashScreenCheckBox.Checked;
Properties.Settings.Default.Save();


在您的Form构造函数中,设置复选框:


In your Form constructor, set the check box:

public frmMain()
    {
    InitializeComponent();
    splashScreenCheckBox.Checked = Properties.Settings.Default.ShowSplashScreen;
    }



完成!



Done!


这篇关于处理checbox和线程时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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