Click here to Skip to main content
12,360,037 members (59,443 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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());
        }


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


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 14-Nov-12 8:26am
Updated 14-Nov-12 18:43pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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();
In your Form constructor, set the check box:
public frmMain()
    {
    InitializeComponent();
    splashScreenCheckBox.Checked = Properties.Settings.Default.ShowSplashScreen;
    }

Done!
  Permalink  
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
OriginalGriff 15-Nov-12 6:44am
   
Answer updated
shaikh-adil 15-Nov-12 6:55am
   
""When you change the Checkbox, save the new value:

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

where is have to write this code sir?
OriginalGriff 15-Nov-12 7:04am
   
Ooo! Well, there are so many possible answers here...:laugh:
At your computer? :)

The best place to do it is to handle the Form.Closing event and do it in there - that way it is only updated when the user is sure about it. You can do it it the CheckBox.CheckChanged event handler, but that can give problems with the initial value as set by the InitializeComponent method.
shaikh-adil 15-Nov-12 7:16am
   
Ooo! Well, there are so many possible answers here...:laugh: At your computer? :)
what does this mean?
OriginalGriff 15-Nov-12 7:21am
   
It means "jokes don't translate well" - ignore it.
shaikh-adil 15-Nov-12 7:26am
   
i dont worry. if you have laughed at me. but i have gained some knowledge and camed to know about new thing i.e setting in visual studio.
even if you laughed it dosent hurt me well.
thanks for giving answers and solving my doubt.
really thank you sir
OriginalGriff 15-Nov-12 7:37am
   
It was never intended to hurt you - if I have offended in any way I am truly sorry, and apologise profusely.
It was a joke, not at you, but with you - or intended that way.
shaikh-adil 15-Nov-12 8:20am
   
dont say sorry sir. you are elder than me
thanks for replying to my post as i have asked many questions, peoples must have thought the this guy is always disturbing coders. i dont know about others but you have always replied my post.
thanks you
:)
OriginalGriff 15-Nov-12 8:24am
   
I learnt a long time ago - if you make a mistake, always own up, always say sorry. Age has nothing to do with it!

You are welcome - ask questions all you want, answering them is what this site is all about. :)
sariqkhan 15-Nov-12 7:31am
   
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100