Splash screen doesn't hide - using Microsoft.VisualBasic library - c#

I have 2 forms. Form1 (with code below) and Splash (just default form for test).
My problem is that after application run the Splash doesn't hide. Main form is loaded but Splash is still not closed.
The Form1 code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
namespace WindowsFormsApplication2
{
class Program : WindowsFormsApplicationBase
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
// Show Form in Single-instance mode
var prg = new Program();
prg.EnableVisualStyles = true;
prg.IsSingleInstance = true;
prg.MinimumSplashScreenDisplayTime = 1000;
prg.SplashScreen = new Splash();
prg.MainForm = new Form1();
prg.Run(args);
}
}
}
You must add reference to Microsoft.VisualBasic to work of this.
Splash form code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
}
}
}
Thank you in advance for your help.

Ah you're using the Visual Basic Appplication Framework to run the splash screen?
Try this.
This is from a quick Forms application - note that I have left all names and namespace as default, so you may need to change this for your code. The project has two forms only. Form2 is the splash screen. I embedded a background image on it in order to ensure that it popped up okay and that I could differentiate it from Form1.
I added a reference to .NET Microsoft.VisualBasic into my project.
This is from the program.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new MyApp().Run(args);
}
}
public class MyApp : WindowsFormsApplicationBase
{
protected override void OnCreateSplashScreen()
{
this.SplashScreen = new Form2();
}
protected override void OnCreateMainForm()
{
// Do your initialization here
//...
System.Threading.Thread.Sleep(5000); // Test
// Then create the main form, the splash screen will automatically close
this.MainForm = new Form1();
}
}
}
I know that is different to wht you're using but it seems to work.

Related

How to use created Instance on all forms / How to make my instance public C# windowsforms

Hello it is probably easy question for you, I'm a beginner and I'm making my own simple game and I want to use a Class:Gamer, which I want to initialize in MainWindow(Form1.cs) from a save file. From then, I want to use it on another Forms aswell, but somehow I can't make the instance go public.
Could you tell me what I'm doing wrong? Or is there another way how to solve this?
Thank you :)
Code on Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;
using System.IO;
namespace THE_GAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Gamer Player;
private void MainWindow_Load(object sender, EventArgs e)
{
//load from savefile lvl;hp;money;gun;armor,name
string allData = File.ReadAllText("../../saveFile/save.txt");
string[] dataFromSave = new string[5];
dataFromSave = allData.Split(';');
Player = new Gamer(dataFromSave[0], dataFromSave[1], dataFromSave[2], dataFromSave[3], dataFromSave[4], dataFromSave[5]);
}
}
}
Code on secondForm2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;
namespace THE_GAME
{
public partial class Statistics : Form1
{
public Statistics()
{
InitializeComponent();
}
private void Statistics_Load(object sender, EventArgs e)
{
//labels stats
labelName.Text = Form1.Player.GetName();
labelHealth.Text = Form1.Player.GetHealth().ToString();
labelMoney.Text = Form1.Player.GetMoney().ToString();
}
private void buttonBack_Click(object sender, EventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
this.Close();
}
}
}
Thank you for your time.
To get at the Gamers Player object from a different Form just do
Form1.Player;
ie
var nam = Form1.Player.Name;
Form1.Player.Die();
etc
PS As I said in a comment - its extremely odd to dereive a form of yours from another one of your forms. Like this
public partial class Statistics : Form1

C# Running program at startup

Hello I am working on c# simple project.
This is the code:
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.IO;
private void SetStartup()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (chkStartUp.Checked)
rk.SetValue(AppName, Application.ExecutablePath);
else
rk.DeleteValue(AppName,false);
}
namespace web1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// open browser
Thread.Sleep(6000);
System.Diagnostics.Process.Start("https://google.com/");
}
}
}
It shows errors "The name "AppName" does not exist in the current context" and "The namespace cannot contain members such as fields or methods directly"
I want to:
If I click on this program .exe, it should run on the startup (every startup).
How to deal with it? I've tried so many ideas, but doesn't helped.
Thanks in advance.
BTW: it's my first time here, nice to meet you all!

Can't close this form

I run a new form
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
Then I call into FormMain():
Application.Run(applicationContext);
How can I close FormMain by code?
Here is FormMain:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyTabs;
namespace CefSharp
{
public partial class FormMain : Form
{
public static AppContainer Container = new AppContainer();
public FormMain()
{
InitializeComponent();
Container.Tabs.Add(new EasyTabs.TitleBarTab(Container)
{
Content = new frmTab
{
Text = "New Tab"
}
});
Container.SelectedTabIndex = 0;
TitleBarTabsApplicationContext applicationContext = new TitleBarTabsApplicationContext();
applicationContext.Start(Container);
Application.Run(applicationContext);
this.Hide();
if(Container.ExitOnLastTabClose)
{
this.Close();
}
}
}
}
Ok guys, it's not that simple as i wrote without checking. I figured out solve with threads use. First we need delegate on form:
public delegate void closer();
partial class FormMain {
public closer Closer;
(...)
}
Inside class constructor or in InitializeComponents add Close method to it:
this.Closer += Close;
Create public static FormMain object:
static class Program {
public static FormMain form1;
(...)
}
Then you just run threads with running window and simply (in this case after 5 seconds) close the window:
Program.form1 = new FormMain();
Thread fo = new Thread(() => { Application.Run(Program.form1); });
Thread th = new Thread(() => { Thread.Sleep(5000); Program.form1.Invoke(form1.Closer); });
fo.Start();
th.Start();
You can use two different approaches:
As previously mentionded you can use formMain.Close()
And Application.Exit() to totally close the application.
If you want something more special use Bartek solution. But still, the information you gave us are not enough

How to modify label text in C# forms

(I am very new to C#) I am creating a forms application, and the purpose is to get a string from a Web API, then put that text onto a label. I have successfully gotten the data from the Web, but when I try to update the label, I have no luck.
I have debugged and found that my method inside my class Is executing, but just not setting the label's text. As you can see below, I tried to use this.resultLabel.text = str;. Here's the classes:
Program.cs (not the form cs file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Net;
using System.IO;
namespace WebsiteAPITest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
class PostManager
{
public void setupClient()
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://yakovliam.com/phpApi/csTest.php"));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
string respStr;
using (Stream stream = WebResp.GetResponseStream()) //modified from your code since the using statement disposes the stream automatically when done
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
respStr = reader.ReadToEnd();
}
MessageBox.Show(respStr);
Form1 form = new Form1();
form.SetResultLabel(respStr);
}
}
}
Actual form class (Form1.cs)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WebsiteAPITest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void GetButton_Click(object sender, EventArgs e)
{
PostManager postManager = new PostManager();
postManager.setupClient();
}
public void SetResultLabel(string str)
{
this.resultLabel.Text = str;
this.resultLabel.Refresh();
}
}
proof of label name:
Inside setupClient you call Form1 form = new Form1(); that creates a second Form1 which you never display, then you call SetResultLabel(respStr) inside this second form you never display, then you leave the method and discard it.
If you want to call SetResultLabel of your calling form, you have to pass the calling form to setupClient:
public void setupClient(Form1 callingForm)
{
...
callingForm.SetResultLabel(respStr);
Then inside your Form1:
postManager.setupClient(this);
It's quite dangerous to pass forms to other methods; a better design is to have the other method return data to your form:
public string setupClient()
{
...
return respStr;
}
And inside Form1:
SetResultLabel(postManager.setupClient());

Not Able to Close form

Good morning,
I have several questions but I am not sure which is the important one to ask so I'll first state my overall problem. I can't close my Winform App. I have searched and found many answers but they either don't work, I don't understand or both.
If I do all my work and then call Application.Exit the form never closes. Same results if I put this.Close. However if I place a button on the form and call Application.Exit it closes the form.
I obviously do not understand the flow, I hope it is clear to someone what I am trying to do. As a non-programmer I have been piecing this project together for a few months and this is my last step - Close the form after work is complete if it was run from command line with arguments. I would try longer to work it out but my Visual Studio trial runs out this week so I turn to the experts :)
Thank you,
Todd
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgramCSToormTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(String[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//add if
if (args.Length == 0)
{
Application.Run(new Form1("Form"));
}
else
{
Application.Run(new Form1(args[0]));
}
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgramCSToormTest
{
public partial class Form1 : Form
{
string CLArg1;
string ReturnText;
public Form1(string Arg1)
{
InitializeComponent();
if (Arg1 != null)
{
CLArg1 = Arg1;
textBox1.Text = Display(CLArg1);
//button1.Enabled = false;
}
else
{
textBox1.Text = "click button to start";
}
Application.Exit(); //This seems to be ignored
}
public void button1_Click(object sender, EventArgs e)
{
CLArg1 = null;
textBox1.Text = Display("Hello World");
Application.Exit();
}
public string Display(string DisplayText)
{
if (CLArg1 != null)
{
ReturnText = CLArg1;
return(ReturnText);
}
else
{
ReturnText = DisplayText;
return(ReturnText);
}
}
}
}
See this question. Application.Close() only works if an application has been created. This is done by calling Application.Run(). Now. in your code you call Application.Exit() from the constructor of your form. Which is executed before the Application.Run() that is needed to create the application.
To fix this, either wait until after Application.Run(). Or, if you want to quit the application in the constructor use Environment.Exit(int statusCode). When using Environment.Exit(int statusCode) keep this in mind though.
You can't close the Application when the Form is Loading from the Program class. Try calling the Exit method after Loading the Form:
private void Form1_Load(object sender, EventArgs e)
{
if (CLArg1 != String.Empty)
Application.Exit();
}

Categories