Preloading a form in WPF c# - c#

I am currently trying to make a Loading form which should stay on screen for 7 seconds before switching to the main form
I cant just simply load the main form directly after the loading bar is full because the Main form takes about 7 seconds to start
Is there i way to "pre load" the main form, but hiding it until the loading form is done?
This is the code for the loading form, simplyfied
private void FormLoading_Load(object sender, EventArgs e)
{
tick.Start();
}
private void tick_Tick(object sender, EventArgs e)
{
loadingbar.Increment(1);
if (loadingbar.Value == 700)
{
this.Close();
}
}

I would add a Splashscreen - this way you do not need to use a timer for waiting until the form is loaded. You simply display the splash screen at startup and close it inside the MainWindows Constructor.
You can add a splash screen by Visual Studio -> Right click your project -> Add new item -> Wpf -> Splashscreen.
If you really want that the form does display there for 7 seconds you can use Thread.Sleep before closing the Splashscreen.

Related

Navigation between Pages and Windows in WPF

I have an issue that I am trying to resolve. I have two WPF windows. Those windows house the pages with content. My problem is not navigating page to page but rather window to window. here is an example of the code:
LoginPortal Design
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
</Grid>
LoginPortal Code
public LoginPortalMain()
{
InitializeComponent();
MainFrame.Navigate(new Uri("/Pages/LoginPortal.xaml", UriKind.RelativeOrAbsolute));
}
This is my Login portal that houses 4 buttons. Each button opens the second window which is houses several pages.
private void BtnSales_Click(object sender, RoutedEventArgs e)
{
var w = Application.Current.Windows[0];
w.Hide();
MainWindow mn = new MainWindow();
mn.Content = new SalesPage();
mn.Show();
}
When this button is clicked it opens the second WPF Window which is where several pages are housed. There is no problem with navigation. The problem starts when I wire the Closing event from the MainWindow to go back to the LoginPortalMain
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
LoginPortalMain lgm = Application.Current.MainWindow as LoginPortalMain;
lgm.Show();
}
It then goes back to the start again. Now my problem is if I click on the same button again. It just duplicates everything. Then on closing anything it just adds new windows. If I then exit out of any window, it throws an error in picture below. My question is, is there a way to either dispose of the navigation once the MainWindow is closed. So there is no duplicate windows once you close the mainWindow a second time. I have used navigation before on a single window but not on multiple windows.
I may have to rethink this whole structure and use custom UserControls and figure out the animation sequence from Pages to UserControls. However, any help would be greatly appreciated.
enter image description here
enter image description here
enter image description here
enter image description here
enter image description here
If you only want to be able to open one instance of the new window try showing and hiding a single window instance.
When the open window button is pressed create the window if need and show it.
private OtherWindow mOtherWindow;
private void OpenWindowButtonClick(object sender, RoutedEventArgs e)
{
mOtherWindow ??= new OtherWindow(); //initialize child window on first call
mOtherWindow.Show();
}
When the other window is closed hide it and cancel the close request. You could also reset the other window state here for the next time it is shown.
private void OtherWindowClosingEvent(object sender, System.ComponentModel.CancelEventArgs e)
{
((Window)sender).Hide();
Application.Current.MainWindow.Activate();
e.Cancel = true;
}

Trouble going from one form to another using buttons

this is my frmMain image
I have been struggling with this all day. I have 7 buttons in my nav menu. I have my main form which all those buttons are placed and 7 forms that I will connect to my 7 buttons. 1 form for each button. When I go to another form the previous form I opened is still there. I tried using hide and close to exit the other forms but it was not working for me. I'm also using none border form
btnItems to frmItems
btnUsers to frmUsers
btnSuppliers to frmSuppliers
btnStocks to frmStocks (and so on with the 3 more buttons connected to different forms)
This is the code.
private void btnItems_Click(object sender, EventArgs e)
{frmItems op = new frmItems();
op.Show();
}
private void btnSuppliers_Click(object sender, EventArgs e)
{
frmSuppliers op = new frmSuppliers();
op.Show();
}
Although I don't understand EXACTLY what you are trying to accomplish, is showing the forms as a modal dialog an option? This would ensure that a window must be closed before allowing control to go back to the main form and allow another window to be opened.
private void btnSuppliers_Click(object sender, EventArgs e)
{
using (var op = new frmSuppliers())
op.ShowDialog(this);
}

Starting form to appear before the next form

1I'm making a lottery simulator and I want a new form at the beginning that asks if your are 16 or older before you are taken to the main form. I'm using visual studio 2015 and coding in c#. Can someone tell me how to do this?
If you double click the form that currently shows up first to get the form load method, then place your form you want to show up first within the method, it should display when the main form loads up and hide the first one. Just make it so No exits the environment.
private void Lottery_Sim_Load(object sender, EventArgs e)
{
AgeVerificationForm.ShowDialog();
}

Call a new form from current form in c#

I want to create a program which have the same theme and looks like new forms are loading inside the current form from c#. I can do this by using panels but I do not wish to do so since if I had to do a modification it becomes a cumbersome.Instead of using panels I used forms. when i click a button in a form same style form with different values will appear on the current form. But it's looking so untidy and when i want to close the program I have to close every form. Since my final program will contain more than 15 guis it is not a practicle solution .so tried by using following code snippet.
private void ord_Click(object sender, EventArgs e)
{
this.Close();
order frm =new order();
frm.Show();
}
From this code I suppose to close the current form and display the next form. But this code didn't work .It closed both forms.
I can use this.hide() method instead of this.close .but its only hide the current form.When I want to close the Program I have to close each and every form.
I don't wish to use MDI forms. Because it is not suit for my theme. you can get idea about my theme by looking at following image.
GUI of the system
And I want to design a professional looking GUI. I also like to have comments about my GUI. Is it looking professional?
try this
private void ord_Click(object sender, EventArgs e)
{
order frm =new order();
frm.Show();
this.Hide();
}
if its still not working ?
then probably you are using forms in parent child fashion if you close the form which is running at the very start all the forms will be closed, Solution: you should hide child form using .Hide() and for the next form to open declare the parent form as mdi parent of child form hope you understand...

Clean and reset a form without leaving it with C#

Plop,
I'm currently working on a Hangman game with C#. I use a main form to the menu, then I start another form within the first form in that way :
Form2 game = new Form2();
game.ShowDialog();
So when I finish a game, I don't want to close it form, but I want to reload the whole form. Here is the way I do :
private void Replay(object sender, EventArgs e)
{
this.Controls.Clear();
this.InitializeComponent();
Form2_Load(this, null);
}
This code removes all Controls then recreate them with InitializeComponent. I thought removing controls would clean the screen but it doesn't. Some labels and PictureBox that are removed still stay displayed.
Where am I doing this wrong ?

Categories

Resources