Trouble going from one form to another using buttons - c#

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);
}

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;
}

Preloading a form in WPF 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.

How to reset form values after closing?

I have a Login form that users use to log in. After logging in, they will be presented with another form, and this form contains different labels. Each label is being treated as a button. The form is divided into 2 panels, the left panel is for the labels, and the right panel is for the button action. For example, I have 3 labels on the left side one called "Home" and the other one is "Register User" and lastly "Log Out" when I press the label "Users" a TabControl will be showing on the right side and it has 2 tabs "Register user" And "Current Users".
After I click on "Log Out" and I log in with a different user I want everything to be clean just like if I was opening it from the beginning. For example, the Home page should show first and not the last visible panel. If I log in with user called "test" now and I click on "Users" and then log out and log in from a different user I will still see The "Users" panel open and every value entered in textboxes is still open. And I don't want this.
The code I used to log out is:
private void LogOutBtn_Click(object sender, EventArgs e)
{
this.Close();
Globals.LoginForm.Show();
}
The code I use to move from the login form to the welcome form is:
private void LoginBtn_Click(object sender, EventArgs e)
{
this.Hide();
Globals.WelcomeForm.ShowDialog();
}
Try declaring a new WelcomeForm each you show it. Something like this:
private void LoginBtn_Click(object sender, EventArgs e)
{
Globals.WelcomeForm NewWelcomeForm = new Globals.WelcomeForm();
this.Hide();
NewWelcomeForm.ShowDialog();
}
The new constructor will reset all the controls and their values.
Never Mind I got it working. I know my mistake :) declaring it in a different class and calling from that class was causing me this problem.
My code now is
private void LoginBtn_Click(object sender, EventArgs e)
{
Welcome NewWelcomeForm = new Welcome();
his.Hide();
NewWelcomeForm.ShowDialog();
}

How to close a form in UserControl

I created a UserControl with the buttons Save, Close and Cancel. I want to close the form without saving on the Cancel button, prompt a message to save on the Close button and Save without closing on the Save button. Normally, I would have used this.Close() on the Cancel button, but the UserControl doesn't have such an option. So I guess I have to set a property for that.
Scrolling down the "Questions that may already have your answer" section, I came across this question: How to close a ChildWindow from an UserControl button loaded inside it? I used the following C# code:
private void btnCancel_Click(object sender, EventArgs e)
{
ProjectInfo infoScreen = (ProjectInfo)this.Parent;
infoScreen.Close();
}
This does the job for one screen, but I wonder if I have to apply this code for all the screen I have? I think there should be a more efficient way. So my question is: Do I need to apply this code for every form I have, or is there another (more efficient) way?
you can use
((Form)this.TopLevelControl).Close();
you can use the FindForm method available for any control:
private void btnCancel_Click(object sender, EventArgs e)
{
Form tmp = this.FindForm();
tmp.Close();
tmp.Dispose();
}
Do not forget to Dispose the form to release resources.
Hope this helps.
You also can close one form in any part of the code using a remote thread:
MyNamespace.MyForm FormThread = (MyNamespace.MyForm)Application.OpenForms["MyForm"];
FormThread.Close();
I found the simple answer :) I all ready thought of something like that.
To close a WinForm in a ButtonClicked Event inside a UserControl use the following code:
private void btnCancel_Click(object sender, EventArgs e)
{
Form someForm = (Form)this.Parent;
someForm.Close();
}

Parent form closing when child form closes

In my C# application, I have the following method that is called when the main form closes.
private void FormMain_Closing(object sender, FormClosingEventArgs e)
{
// Show this dialog when the user tries to close the main form
Form testForm = new FormTest();
testForm.StartPosition = FormStartPosition.CenterParent;
testForm.ShowDialog();
}
This creates a dialog window that will show when the main form is closing. However, my issue is that when the user closes testForm, the main form closes immediately after. I've tried all sorts of variants of e.Cancel = true; and such, and still cannot cancel the main form closing.
Any ideas?
Edit: it looks like I'm running into an issue using two ShowModal()'s in succession. Looking into the issue...
Edit: Used this.DialogResult = DialogResult.None; and it seems to have fixed my problem. It is apparently a known issue in WinForms when opening a modal dialog from a modal dialog.
This code works fine with me. I think there is a problem in another part of your code.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Form testForm = new FormTest();
testForm.StartPosition = FormStartPosition.CenterParent;
testForm.ShowDialog();
e.Cancel = testForm.DialogResult == DialogResult.Cancel;
}
This could to be handled by the children too from the docs:
If a form has any child or owned
forms, a FormClosing event is also
raised for each one. If any one of the
forms cancels the event, none of the
forms are closed. Therefore the
corresponding FormClosed events are
not sent to any of the forms.
I know that you mention in your question that you have tried to use 'e.Cancel = true;' However, the following code works in my environment (.NET 4.0, Visual Studio 2010, Windows 7):
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// Show this dialog when the user tries to close the main form
Form testForm = new FormTest();
testForm.StartPosition = FormStartPosition.CenterParent;
testForm.ShowDialog();
e.Cancel = true;
}
If this doesn't work in your case you may have other event handlers at work. In that case try this code in a newly generated Windows Forms Application.

Categories

Resources