Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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

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



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

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



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.
Posted
Updated 14-Nov-12 18:43pm
v2

1 solution

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:
C#
if (Properties.Settings.Default.ShowSplashScreen)
    {
    // Show your splash screen
    ...
    }

When you change the Checkbox, save the new value:
C#
Properties.Settings.Default.ShowSplashScreen = splashScreenCheckBox.Checked;
Properties.Settings.Default.Save();

In your Form constructor, set the check box:
C#
public frmMain()
    {
    InitializeComponent();
    splashScreenCheckBox.Checked = Properties.Settings.Default.ShowSplashScreen;
    }


Done!
 
Share this answer
 
v2
Comments
shaikh-adil 14-Nov-12 16:46pm    
i have done this
public Form2()
{

InitializeComponent();
if (checkBox1.Checked)
{
Thread t = new Thread(new ThreadStart(splashscreen));
t.Start();
Thread.Sleep(5000);

//InitializeComponent();
t.Abort();
}
else
{
Application.Run(new Form2());
}

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

but the exception is caught i.e stackoverflow exception
how can i do that?
can you help me in this?
OriginalGriff 15-Nov-12 3:33am    
Look at your code:
public Form2()
{
...
Application.Run(new Form2();
...
}

So, each time you construct a new instance of Form2, it creates a new instance of Form2. Which calls the Form2 constructor, which creates another new instance of Form2, which calls the Form2 constructor, which...

And you are surprised you get Stack Overflow? :laugh:

Why is Application.Run in there anyway? you should never need to call that outside of the one-and-only Main method - and then only once, normally!
shaikh-adil 15-Nov-12 4:02am    
sorry sir.
I am a newbie to programming
can you guide me?
I have that threading on the main form which contains splash form.
I want a checkbox to determine the splashing form if the checkbox is checked then splash form will not be visible. And if not checked then it will be at every mainform starting.
Can you help me in this
OriginalGriff 15-Nov-12 4:17am    
I can't help but think you are going about this the wrong way.
Think about it: when is a splash screen shown? Before the main screen (and it is normally used as a "Hello! I'm Busy!" display for the user).
And when is the splash screen removed? When the Main form is ready and active.
So where is the checkbox that can stop it being displayed? If it is on the main form, then the user can't change it until after the splash screen is removed - which makes it a bit redundant... :laugh:

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?
shaikh-adil 15-Nov-12 6:22am    
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

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