How can i show an image while my application is loading - c#

i have and application windows form .net and my form1 takes a lot of time to appear because in it's event form1_Load does a lot of operation.
My goal is to show an image while the operation are being done.
private void form1_Load(object sender, EventArgs e)
{
methode1();
}
While my methode1() is working, my form doesnt show, i want to show an image on the screen while my methode1() is working because while methode1() is working, there is nothing on the screen.

All the visual things in .net is done on form. You can do it by creating an small form which contains an image load it before module1() and after completing module1() close it. Just below..
private void form1_Load(object sender, EventArgs e)
{
Form f = new Form();
f.Size = new Size(400, 10);
f.FormBorderStyle = FormBorderStyle.None;
f.MinimizeBox = false;
f.MaximizeBox = false;
Image im = Image.FromFile(path);
PictureBox pb = new PictureBox();
pb.Dock = DockStyle.Fill;
pb.Image = im;
pb.Location = new Point(5, 5);
f.Controls.Add(pb);
f.Show();
methode1();
f.Close();
}

Create another form, just for loading, with a static image, and display it before your application starts to load, and destroy it afterwards. Always on top, and with no border is the usual setup for such things.

Try this code
using System.Reactive.Linq;
private void RealForm_Load(object sender, EventArgs e)
{
var g = new Splash();
// place in this delegate the call to your time consuming operation
var timeConsumingOperation = Observable.Start(() => Thread.Sleep(5000));
timeConsumingOperation.ObserveOn(this).Subscribe(x =>
{
g.Close();
this.Visible = true;
});
this.Visible = false;
g.ShowDialog();
}
This code uses Microsoft Rx to execute operations in background threads among other cool features
http://msdn.microsoft.com/en-us/data/gg577609.aspx
In order for this code to work you need to reference two nuget packages: Rx and Rx windows forms
https://nuget.org/packages/Rx-Main/1.0.11226
https://nuget.org/packages/Rx-WinForms/1.0.11226

(splash screen c# -- google it)
Here's what I just found:
http://msdn.microsoft.com/en-us/library/aa446493.aspx

How about using the built in SplashScreen class?
http://msdn.microsoft.com/en-us/library/system.windows.splashscreen.aspx

Related

C# Using One Panel As a Placeholder For Multiple Forms

I apologize if this has been addressed already, but I could not find a case that fit my exact situation. So here goes...
I have a MainForm that contains a toolStrip1 docked to the left that functions as a vertical navigation bar. I have a panel (pnlMain) filling up the remainder of the form. I want to use pnlMain to display different forms which are made up of win form classes. Right now, I can click on the labels/buttons on toolStrip1 to display different forms within pnlMain.
private void tsLblCustomers_Click(object sender, EventArgs e)
{
hidePanels();
CustomerReport cr = new CustomerReport();
cr.TopLevel = false;
cr.AutoScroll = true;
cr.BackColor = Color.White;
pnlMain.Controls.Add(cr);
cr.Show();
}
What I want to do now is display additional forms within pnlMain by clicking on a button on another form rather than a label/button on toolStrip1. Some of my forms are as follows: CustomerReport, AddCustomer, EmployeeReport, AddEmployee. The Report forms are linked to my tool strip buttons. The Add forms are linked to buttons on the Reports forms. I tried several things including the following:
1) On CustomerReport, I tried creating an instance of MainForm, then I'll create an instance of AddCustomer, and then add that instance to the panel on MainForm.
2) I also tried creating a method in MainForm to create the instance of AddCustomer, and then call that method from the Add button on CustomerReport. Even though the code was the same as the toolstrip buttons on MainForm, it did not work.
I tried different variations of hiding forms, showing forms, clearing the panel, setting Visible to true or false, and I can't get it to work right. In some cases, I've managed to hide the CustomerReport, but AddCustomer will not come up. At some point I think I created a NEW instance of MainForm and my code wasn't impacting the original form that is already open. I'm just lost. Should I be using a different design? Originally I set up my application to just hide one form then show the other but I read that that is a 'terrible design'.
This sounds very similar to this thread here: Creating Form Inside the Form
You'd want to look into MDI.
Although it sounds like you're aiming for one cohesive interactive window. Otherwise, if you just want separate windows to popup, you can create properties within that other form and read them after returning a DialogResult. I'm not sure why this would be bad design without knowing more about the context of the program.
//Optionally do a hide(); here.
AddCustomer customer = new AddCustomer();
DialogResult result = customer.ShowDialog();
if(result == DialogResult.OK)
{
var name = customer.Name;
//More properties or whatever here.
}
//The properties would still be accessible here, too.
I ended up keeping the toolstrip nav bar on the left side of the primary window, and I created a panel in the main part of the window. All forms are displayed in the panel. Each time one of the label options in the nav bar is clicked on, the current form is cleared off the panel and the active form is displayed.
private void tsLblCustomers_Click(object sender, EventArgs e)
{
pnlMain.Controls.Clear();
CustomerReport cr = new CustomerReport();
cr.TopLevel = false;
cr.AutoScroll = true;
cr.BackColor = Color.White;
pnlMain.Controls.Add(cr);
cr.Show();
}
private void tsLblEmployees_Click(object sender, EventArgs e)
{
pnlMain.Controls.Clear();
EmployeeReport emp = new EmployeeReport();
emp.TopLevel = false;
emp.AutoScroll = true;
emp.BackColor = Color.White;
pnlMain.Controls.Add(emp);
emp.Show();
}
private void tsLblVendors_Click(object sender, EventArgs e)
{
pnlMain.Controls.Clear();
VendorReport vend = new VendorReport();
vend.TopLevel = false;
vend.AutoScroll = true;
vend.BackColor = Color.White;
pnlMain.Controls.Add(vend);
vend.Show();
}
private void MainForm_Load(object sender, EventArgs e)
{
WelcomeForm welcome = new WelcomeForm();
welcome.TopLevel = false;
welcome.AutoScroll = true;
welcome.BackColor = Color.White;
pnlMain.Controls.Add(welcome);
welcome.Show();
}

How can I write a live MetroTile custom control?

Using MetroFramework 1.3.5 and .NET Transitions, in Windows Forms, I have written the code for a live MetroTile. Here is the Timer Tick method used to update the tile:
private void updateTiles_timer_Tick(object sender, EventArgs e)
{
metroPanel1.BackColor = Color.Transparent;
Bitmap bm = new Bitmap(metroPanel1.Width, metroPanel1.Height);
metroTile_startStop.DrawToBitmap(bm, new Rectangle(metroTile_startStop.Location.X, metroTile_startStop.Location.Y, metroTile_startStop.Width, metroTile_startStop.Height));
metroPanel1.BackgroundImage = bm;
if (animationFlag)
{
metroTile_startStop.Text = Properties.Resources.startStopTile_alternateText;
animationFlag = false;
}
else
{
metroTile_startStop.Text = "Start";
animationFlag = true;
}
Transition.run(metroTile_startStop, "Top", metroTile_startStop.Height - 16, 4, new TransitionType_Linear(500));
}
private void metroPanel1_Click(object sender, EventArgs e)
{
metroTile_startStop.PerformClick();
}
The tile is placed inside a MetroPanel with the Size(tile.Width + 10, tile.Height + 8). metroPanel1_Click() is necessary if, during a transition, the user clicks the background bitmap.
I don't have sufficient experience and I need help in writing a custom control so I can easily reuse it in future projects. The control should have a property which sets the timer interval and the possibility to set the tile texts. If I have a working code, then I can extend it to set the tile image, customize animations etc.
Thank you!

Onclick Button open a new Window only with Image inside

I am new to WPF and I've read a lot of answers, but none of them worked for me.
I have a button with a Click method. When I click on it I want my program to show a new Window, only with an image inside, which URL is previously given. Can you show me the simplest way to create such onclick method ?
This code will start you up:
private void button1_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Show();
PictureBox pb = new PictureBox();
pb.ImageLocation = "https://www.google.com/images/srpr/logo11w.png";
pb.Size = form.Size;
pb.SizeMode = PictureBoxSizeMode.Zoom;
form.Controls.Add(pb);
}

How to hide WinForm after it run? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Single Form Hide on Startup
I want to hide my WinForm after it run (Not minimizing).
I used:
this.Load += new System.EventHandler(this.Form1_Load);
private void Form1_Load(object sender, EventArgs e)
{
Hide();
}
But it's not working. Can you help me do it?
In the form Load override you can use one of the following tricks:
Make the form completely transparent:
private void OnFormLoad(object sender, EventArgs e)
{
Form form = (Form)sender;
form.ShowInTaskbar = false;
form.Opacity = 0;
}
Move the form way off the screen:
private void OnFormLoad(object sender, EventArgs e)
{
Form form = (Form)sender;
form.ShowInTaskbar = false;
form.Location = new Point(-10000, -10000);
}
Try to hide the form after it has been shown and not after it has been loaded, use the Shown event instead of the Load event
You can't easily hide the form but what you can do is set the Opacity to 0, for example:
this.Opacity = 0;
If you Don't want the user to be able to see the app at all set this:
this.ShowInTaskbar = false;
Then they won't be able to see the form in the task bar and it will be invisible.
I believe this solved your "no use minimized" requirement??

Saving Location for next form

Alright so I'm doing something with next buttons that open new forms, annoying thing is that new forms pop up somewhere I don't want to on the desktop.
I'm trying to get the new form to spawn on the location of the old form with the code below, unfortunately for whatever reason it's not working at all, they still pop up the same way as before. And yes I have registered the events.
Form1:
System.Drawing.Point LocationPoint = new System.Drawing.Point(200,200);
private void Installer_template_LocationChanged(object sender, EventArgs e)
{
// Save the window location to the installer arts
LocationPoint = this.Location;
}
private void NextButton_Click(object sender, EventArgs e)
{
var NextForm = new Form2(LocationPoint);
NextForm.Show();
this.Hide();
}
Form2
public Form2(System.Drawing.Point LocationPoint)
{
InitializeComponent();
this.Location = LocationPoint;
}
The code is something along those lines
Have you tried setting the StartPosition of the new forms, i.e.
this.StartPosition = FormStartPosition.Manual;
or
this.StartPosition = FormStartPosition.CenterParent;
Alrighty I fixed it, it was a bunch of problems.
Wrong property, gotta use DesktopLocation instead of Location property
Second I had some issues with static member that couldn't be modified or whatever the error is, I just used the settings file to save my location instead
Having that done, it still didn't work because you can't just do this.DesktopLocation = something, you have to use this.SetDesktopLocation(X, Y)
Still didn't work, because it got overwritten by other code when loading the form so you had to use the Shown even of the form and run it in there..

Categories