Results 1 to 15 of 15

Thread: Choose which window to open

  1. #1

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Choose which window to open

    So the MainForm.cs is the main file that opens. I've created another form Splash.cs, how do i get the Splash.cs to show before the MainForm loads. Doesn't matter if its in the background showing, just want to see the Splash.cs first.

    I thought I could choose that open in the properties, but I can't find it.

    So I presume its a piece of code probably related to window and show or something.

    Any advice please?

    Thanks

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Choose which window to open

    Quote Originally Posted by jokerfool View Post
    So the MainForm.cs is the main file that opens. I've created another form Splash.cs, how do i get the Splash.cs to show before the MainForm loads. Doesn't matter if its in the background showing, just want to see the Splash.cs first.

    I thought I could choose that open in the properties, but I can't find it.

    So I presume its a piece of code probably related to window and show or something.

    Any advice please?

    Thanks
    Normally you will find this in the Main method of your application, usually in Program.cs

  3. #3

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Choose which window to open

    What am I looking for in that file?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Choose which window to open

    Quote Originally Posted by jokerfool View Post
    What am I looking for in that file?
    Have you looked? There's only one mention of a form in that file.

    Something called Splash.cs is a code file, not a form. The form definition is in that file and that class is likely named Splash. Likewise, the MainForm.cs file likely contains a form class definition named MainForm. Have you looked in the Main method in the Program.cs file, which contains the Program class, for any reference to MainForm?

  5. #5

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Choose which window to open

    So i would assume its this piece here
    Code:
    Application.Run(new MainForm());
    but if I replace MainForm with Splash it doesn't work.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Choose which window to open

    Quote Originally Posted by jokerfool View Post
    if I replace MainForm with Splash it doesn't work.
    Would you care to explain what actually happens, so that we may determine WHY it doesn't work and thereby determine a solution to the problem? If Splash is the name of a form class and that class has a parameterless constructor then I don't see any reason that it would not work, which is why you need to provide us with ALL the relevant information. If a compilation error or run-time exception occurs, you will be provided with an error message for a reason.

    Having said all this, if you're trying to create a proper splash screen then it's not simply a case of changing the startup form. The main point of a slash screen is to display something to the user while the application main form is being loaded. If you display the splash screen on the UI thread though, you can't load the main form at the same time. That means that you need to create and display the splash screen on a secondary thread. If all you want to do is display some information for a while before the main form is displayed then you can call Application.Run to display the splash form and have it closed using a Timer, then call Application.Run a second time and pass the actual main form. If you want a proper splash screen though - one that allows a main form to load while it's displayed - then you might take a look at this:

    http://www.vbforums.com/showthread.php?628109

  7. #7

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Choose which window to open

    The splash screen is a registration screen which is the window I would like to open before the main form opens.

    The form name is Register.cs

    Code:
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System;
    using System.IO;
    using System.Net;
    using Ketarin.Forms;
    using Microsoft.VisualBasic.CompilerServices;
    using Microsoft.Win32;
    
    namespace Ketarin
    {
        public partial class Form1 : Form
        {
            // The kLibraryKey is meant to prevent unauthorized use of the library.
            // Do not share this key. Replace this key with your own from Advanced Installer
            // project > Licensing > Registration > Library Key
            private const string kLibraryKey = "xxxxxx";
    
            public Form1()
            {
    
    
                this.CenterToScreen();
    
                // We launch the trial UI on a thread because we want the trial UI to be
                // displayed after you main application UI is displayed.
                Thread thread = new Thread(InitTrial);
                thread.Start();
            }
    
            // Main trial function
            // This function does all the work
            //
            // @return
            // - 0 when licensed
            // - 2 when unlicensed but in trial
            [DllImport("Trial.dll", EntryPoint = "ReadSettingsStr", CharSet = CharSet.Ansi)]
            private static extern uint InitTrial(String aKeyCode, IntPtr aHWnd);
    
            // This function does all the work but it will NOT kill the application
            //
            // @return
            // - 0 when licensed
            // - 2 when unlicensed but in trial
            // - values grater or equal with 4 when unlicensed and trial expired
            [DllImport("Trial.dll", EntryPoint = "ReadSettingsRetStr", CharSet = CharSet.Ansi)]
            private static extern uint InitTrialReturn(String aKeyCode, IntPtr aHWnd);
    
            // Use this function to register the application using the provided serial
            [DllImport("Trial.dll", EntryPoint = "DisplayRegistrationStr", CharSet = CharSet.Ansi)]
            private static extern uint DisplayRegistration(String aKeyCode, IntPtr aHWnd);
    
            [DllImport("Trial.dll", EntryPoint = "GetPropertyValue", CharSet = CharSet.Unicode)]
            private static extern uint GetPropertyValue(String aPropName, StringBuilder aResult, ref UInt32 aResultLen);
    
            private void FileExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
         
    
    
        private static void InitTrial()
            {
                try
                {
                    // Wait 2 second for the main window to be visible.
                    Thread.Sleep(2000);
    
                    Debug.WriteLine("Debug Information-Product Starting ");
    
                    Process process = Process.GetCurrentProcess();
                    InitTrial(kLibraryKey, process.MainWindowHandle);
    
                    // To prevent your application be killed when unlicensed and trial expired
                    // ReadSettingsRetStr must be called which will return 4 or grater instead of killing the app
                    //
                    //         uint ret = InitTrialReturn(kLibraryKey, process.MainWindowHandle);
                    //
                    //         if (ret >= 4)
                    //         {
                    //           MessageBox.Show("Invalid mode to run");
                    //           // disable the application
                    //           // Process.GetCurrentProcess().Kill();
                    //         }
    
                    // Let's read some licensing configuration property
                    StringBuilder trialName = new StringBuilder();
                    UInt32 len = (UInt32)trialName.Capacity;
                    if (GetPropertyValue("TrialName", trialName, ref len) == 234)
                    {
                        trialName.EnsureCapacity((Int32)len);
                        GetPropertyValue("TrialName", trialName, ref len);
                    }
    
                    Debug.WriteLine("TrialName=" + trialName);
                }
                catch (DllNotFoundException ex)
                {
                    // Trial dll is missing close the application immediately.
                    MessageBox.Show(ex.ToString());
                    Process.GetCurrentProcess().Kill();
                }
                catch (Exception ex1)
                {
                    MessageBox.Show(ex1.ToString());
                }
            }
    
            private void registerToolStripMenuItem_Click(object sender, EventArgs e)
            {
               
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void InitializeComponent()
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 12);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(230, 116);
                this.button1.TabIndex = 0;
                this.button1.Text = "Begin";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.Button1_Click);
                // 
                // Form1
                // 
                this.ClientSize = new System.Drawing.Size(252, 141);
                this.Controls.Add(this.button1);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
                this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "Form1";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                this.ResumeLayout(false);
    
            }
    
            private Button button1;
    
            private void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                    Process process = Process.GetCurrentProcess();
                    DisplayRegistration(kLibraryKey, process.MainWindowHandle);
                }
                catch (DllNotFoundException ex)
                {
                    // Trial dll is missing close the application immediately.
                    MessageBox.Show(ex.ToString());
                    this.Close();
                }
                catch (Exception ex1)
                {
                    MessageBox.Show(ex1.ToString());
                }
              
            }
        }
    }
    Now I don't fully understand any of this and I am only learning from trial and error. Am I assuming that everything that is listed in the program.cs file is what takes into count first when the application is executed?

    So this line
    Code:
    Application.Run(new MainForm());
    refers to starting the mainform after executing the exe is this right?

    So basically I need the Register form to open and stay open until one of the 3 options are chosen, then obviously closes. Once the app has been registered the window wont open and the mainform opens instead.

    I've also been reading the link in your signature too on C# (Thank you) https://www.homeandlearn.co.uk/csharp/csharp_s1p5.html
    Last edited by jokerfool; May 22nd, 2019 at 02:35 AM.

  8. #8

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Choose which window to open

    I think I've stumbled across a bug in my form - the public partial class says public partial class Form1 : Form and not public partial class Register : Form

  9. #9

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Choose which window to open

    So this is where I am upto:

    1. I create a form called Register and in that form is the above code
    2. I went and changed the Form1 all over the form
    3. I went and made a change in program.cs -
    Code:
    Application.Run(new Register());
    4. I run the main app and that window opens for the register

    So far so good.

    5. In the Register form I did actually place a button but because the window opens then the register info of try buy or register opens that first window that opened with the button, I don't see the button.

  10. #10

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Choose which window to open

    So I renamed the Register form.

    I added this to the Program.cs

    Code:
    Application.Run(new RegExF());
                    Application.Run(new MainForm());
    Question: When I run the main application that new window RegExF opens, its blank even though I added a button. When i close it, the main application opens. Why is the form empty when i can't see the button, I don't get any errors.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Choose which window to open

    Quote Originally Posted by jokerfool View Post
    The splash screen is a registration screen which is the window I would like to open before the main form opens.
    That's not a splash screen. A splash screen is a specific thing, not just any form displayed before the main form.

    The Main method, which is generally in the Program class in the Program.cs file, is the entry point for your application. When you execute your application, it's the Main method that gets called. When that Main method completes, your application exits.

    The Application.Run method is basically what creates a context for a WinForms application to run in and receive window messages from the OS. You generally pass a form but can also pass an application context and once that form or context closes, the method returns and that message pump quits. Normally, everything you do in your application occurs with the context of that Application.Run call.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Choose which window to open

    Quote Originally Posted by jokerfool View Post
    Question: When I run the main application that new window RegExF opens, its blank even though I added a button. When i close it, the main application opens. Why is the form empty when i can't see the button, I don't get any errors.
    You did something wrong but it's impossible to say what based on the information provided. Maybe the build is failing and you're executing old output. Maybe it's something else. I don't know.

  13. #13

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Choose which window to open

    If I did something wrong then the application wouldn't build and run. The registration code above I've placed into a form, I called the form from the program.cs, so obviously the form will display, the code inside doesn't show any visible info on that form, a completely seperate registration window appears.

    Code:
        this.ClientSize = new System.Drawing.Size(630, 414);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "RegExF";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                this.ResumeLayout(false);
    Please tell me, does the code above allow me to add further code to display a text box and button and if so can you give me an example on how to do this but please don't give me the answer as I would like to try and figure it out. Thank you.

  14. #14

    Thread Starter
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Choose which window to open

    I ended up working it out. Thank you.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Choose which window to open

    It's generally a good idea to post your solution. As well as satisfying our curiosity, that may also help someone else in future. It also means that, if your solution is sub-optimal, we can potentially provide improvements.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width