0

I'm using Visual Studio 2013 to create a Visual C# Windows Forms Application and I'm not using the Designer to setup the form.

I have what I think is a simple timer that should pause my application for a few seconds to allow a splash screen to be shown before the user can see and access the menu. But when I run debug the application doesn't appear on the screen until the timer is done and then it has jumped past the splash screen. I've looked at different ways to do timers and tried to search for help on this for hours now but cannot find a way to make it work. I'm guessing that I'm missing something very obvious but as a newbie I can't spot it.

Any help would be greatly appreciated.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Timers;

namespace SimpleForm
{

    public class TheForm : Form
    {

        private MenuStrip menuStrip;
        private MainMenu menuMain;
        private MenuItem menuBlockFile;
        private MenuItem menuBlockOthers;
        private MenuItem menuItemExit;
        private MenuItem menuItemHints;

        static Bitmap imgIntroBg = null;
        static Bitmap imgMenuBg = null;

        private Panel pnlIntro;
        private Panel pnlMenu;

        static int counter = 1;
        static System.Timers.Timer timer;

        public TheForm()
        {

            FormInitialize();

            MenuInitialize();

            introDisplay();

            MenuDisplay(true);

        }

        private void FormInitialize()
        {

            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Game));

            this.SuspendLayout();

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(714, 462);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.Name = "SimpleForm";
            this.Text = "Simple Form";
            ControlBox = false;
            BackColor = System.Drawing.Color.Black;
            StartPosition = FormStartPosition.CenterScreen;

            this.ResumeLayout(false);

        }


        private void MenuInitialize()
        {

            this.SuspendLayout();

            this.menuStrip = new MenuStrip();
            this.menuMain = new MainMenu();
            this.menuBlockFile = new MenuItem();
            this.menuBlockOthers = new MenuItem();

            this.menuItemExit = new MenuItem();
            this.menuItemHints = new MenuItem();

            this.menuMain.MenuItems.AddRange(new MenuItem[] {
                this.menuBlockFile,
                this.menuBlockOthers});

            this.menuBlockFile.Index = 0;
            this.menuBlockFile.MenuItems.AddRange(new MenuItem[] {
                this.menuItemExit
            } );
            this.menuBlockFile.Text = "File";

            this.menuBlockOthers.Index = 1;
            this.menuBlockOthers.MenuItems.AddRange(new MenuItem[] {
                this.menuItemHints
            });
            this.menuBlockOthers.Text = "Others";

            this.menuItemExit.Index = 0;
            this.menuItemExit.Text = "Exit";
            this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);

            this.menuItemHints.Checked = true;
            this.menuItemHints.Index = 0;
            this.menuItemHints.Text = "Temp";
            this.menuItemHints.Click += new System.EventHandler(this.menuItemExit_Click);

            this.Menu = this.menuMain;

            imgMenuBg = new Bitmap("graphics/layout/menubg.png");
            pnlMenu = new Panel();
            pnlMenu.Name = "pnlMenu";
            pnlMenu.Location = new Point(0, 0);
            pnlMenu.Width = 714;
            pnlMenu.Height = 462;
            pnlMenu.BackgroundImage = imgMenuBg;

            MenuDisplay(false);

            this.ResumeLayout(false);

        }

        private void MenuDisplay(bool display)
        {

            this.menuBlockFile.Visible = display;
            this.menuBlockOthers.Visible = display;
            if (display == true) {
                Controls.Add(pnlMenu);
            } else {
                Controls.Remove(pnlMenu);
            }

        }

        private void introDisplay()
        {

            timer = new System.Timers.Timer();
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Interval = 5*1000;
            timer.Enabled = true;

            imgIntroBg = new Bitmap("graphics/layout/Introbg.png");
            pnlIntro = new Panel();
            pnlIntro.Name = "pnlIntro";
            pnlIntro.Location = new Point(0, 0);
            pnlIntro.Width = 714;
            pnlIntro.Height = 482;
            pnlIntro.BackgroundImage = imgIntroBg;

            timer.Start();

            Controls.Add(pnlIntro);

            while (counter != 0)
            {

            }

            Controls.Remove(pnlIntro);

        }

        private static void timer_Elapsed(object source, ElapsedEventArgs e)
        {
            counter = 0;
            timer.Stop();
        }

        private void menuItemExit_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

    }

}

1 Answer 1

4

The while(counter != 0) part is not the right thing to do. The timer_Elapsed callback will be fired when the timer is done, so you add the image (Controls.Add(plnIntro);) and then wait for the callback of the timer to fire. That's when its done. At that point, just remove the plnIntro and it should do as intended.

You don't have to lock the thread with a while loop for it to work.

Try to Remove:

while (counter != 0)
{

}

Controls.Remove(pnlIntro);

And put the Controls.Remove(pnlIntro); in the timer callback:

private void timer_Elapsed(object source, ElapsedEventArgs e)
{
    Controls.Remove(pnlIntro);
    timer.Stop();
}
Sign up to request clarification or add additional context in comments.

10 Comments

For me thing brings up a couple of errors on the line "Controls.Remove(pnlIntro);". One is An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Controls.get' and the second is An object reference is required for the non-static field, method, or property 'SimpleForm.TheForm.pnlIntro'.
Sorry, did not see that the timer_Elapsed callback was static, will fix in answer! :)
Also, the timer object do not have to be static, I would even suggest that it should not be static.
It runs but then throws an error on "Controls.Remove(pnlIntro);". Of "InvalidOperationException was unhandled by user code" - "An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code Additional information: Cross-thread operation not valid: Control 'pnlIntro' accessed from a thread other than the thread it was created on."
Its possible that the System.Timers.Timer creates a new thread, I'm not sure about that specific class dealings. Try use a System.Windows.Forms.Timer instead and see if that helps.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.