Creating a multi-form program linked to a database with login features in c#.
Tried moving between forms using show(), showdialog() and close(), dispose(), hide().
Once past the login form, the program will not properly close the forms.
It closes the form so the user can no longer access its controls, however, the 'closed' form remains completely visible in the windows taskbar and tab menus.
The user can even hover over the taskbar icon for said 'closed' form and see all the information there!
As the program will be handling sensitive personal information. I need help to stop this problem from happening.
Code from a back button on secondary form aimed to completely close the active form and open the main form.
"Curform" is defined outside the method as it is used in multiple buttons within the program.
Form CurForm = Form.ActiveForm;
public void Btn_Back_Click()
{
var MainForm = new MainForm();
MainForm.Show();
CurForm.Close();
}
Use directly active form instead of curForm
Form.ActiveForm.Close();
If you want to use curForm in other parts of the code update curForm when you have created the new form, like this:
Form CurForm;
public void Btn_Back_Click()
{
CurForm= new MainForm();
CurForm.Show();
// Do some things
CurForm.Close();
}
Related
so I have looked this up so much and nothing fits exactly what I want.
I have one form open called "Main" it has a button in it that opens up a form using the code
AddModification modification = new AddModification();
Modification.Show();
Now this opens the form correctly and all. But the problem is that both forms are open. So the AddModification form is a popup form that changes things in the Main Form.
So the AddModification form is opened / shown but the form itself is broken because I can't put Main main = new Main(); in the AddModification form without a stack overflow problem.
So how the heck do I access any of Main's controls in AddModification if I need to keep main and AddModification open and not disposed?
You need to pass the first form as a constructor parameter to the second form.
Have you tried something like 'this.Owner' ?
Please refer to the diagram below:
I'm looking for the best way from one form to open another form. Once the menu form is open, I want to close the login form. I have tried to do this, but I got lost when I try to call it as a child of the MDI container.
The reason your main form isn't showing is because once you close the login form, your application's message pump is shut down, which causes the entire application to exit. The Windows message loop is tied to the login form because that's the one you have set as the startup form in your project properties. Look in your "Program.cs" file, and you'll see the responsible bit of code: Application.Run(new LoginForm()). Check out the documentation for that method here on MSDN, which explains this in greater detail.
The best solution is to move the code out of your login form into the "Program.cs" file. When your program first starts, you'll create and show the login form as a modal dialog (which runs on a separate message loop and blocks execution of the rest of your code until it closes). When the login dialog closes, you'll check its DialogResult property to see if the login was successful. If it was, you can start the main form using Application.Run (thus creating the main message loop); otherwise, you can exit the application without showing any form at all. Something like this:
static void Main()
{
LoginForm fLogin = new LoginForm();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new MainForm());
}
else
{
Application.Exit();
}
}
if you develop WPF form, you can use 'http://wpfmdi.codeplex.com/'. or in Win Form you could play with form such that you want
try this
//global form
LoginForm login = new LoginForm();
public void menuOpen()
{
if(login.Visible)
login.Close();
}
I have two form classes (Form1 and Form2) in a Winforms App.
Form1 is like this:
Form2 is like this (ShowInTaskbar = false):
And this code on Form1:
Form2 someForm = new Form2();
private void btOpenAnotherWindow_Click(object sender, EventArgs e)
{
if (someForm.ShowDialog(this) == DialogResult.OK)
MessageBox.Show("OK!!!");
else
MessageBox.Show("Not OK.");
}
That is, a window with a button which opens modally another windows when clicked, and waits for the user to close the second window (by clicking the "OK" or "Cancel" button). And depending how it was closed, do alternating actions (represented here by the MessageBox.Show() calls).
I need:
The user can use only one window at a time. (Modal forms, That's why I used ShowDialog() instead of Show())
When the form closes, do something depending on how the form was closed (the "if (someForm.ShowDialog(this)...")
To be able (as a user) to minimize the WHOLE APP.
To be able to "unminimize" the app to the former state correctly.
The program to respond to WIN+M (minimize all) keys combination.
the above example fails in two ways:
(need 5) Doesn't respond to WIN+M
(need 3) The app seems to minimize when the Minimize title bar button is clicked, but it is an illusion because the main form (Form1) does not minimize and it is in fact just hidden behind the other opened windows. Only running the example with an empty desktop shows what really happens. Pics follow:
Before Minimize button is clicked:
After:
Note:
The Main form is not minimized
The Form2 is in the left botton corner of the screen.
Form2 is a full blown window (not a dialog window per se) and I need the user to interact with it only until it is closed and I also need the user to be able to miminize the whole app in case he needs it.
It is a shame I can't post here the real forms, It would be clearer than these mock-ups.
I need a solution that works with many levels of modal windows (not only two as this example shows). Any suggestions?
I may need a little more information about what you're trying to do here. I have a simple form (Form1) with a button on it, which calls this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 form2 = new Form1();
form2.ShowDialog();
}
When I click the button, I get a second instance of the same form, but it's modal. I still have the option to minimize this second modal form (I obviously can't interact with the first form), and when I do minimize the second form it does minimize the entire application (both forms). Now obviously you're asking the question, so I don't think I'm understanding you. =) What about this scenario do you wish to change?
C
There is probably some way to hack this functionality using API calls, but I would probably suggest doing some type of overlay with a control inside your main form rather than an actual window. This would allow you to make it "modal" and still have the ability to minimize/resize the main window.
i'm thinking about writing a WPF program that would require login and password at the app startup.
I thought about small form with two textboxes as a login form. User will have to fill in his details and then the main form of the application will be unlocked.
How will you solve this?
Thanks for your answers, daemonsvk
This is standard behavior for the Window.ShowDialog() call. Other windows will be disabled.
I would make the initial startup form your login page and authenticate the user, if their details are correct hide the login form and show you other form.
if(user is authed)
{
this.Hide(); //Hide the login form
mainAppForm.Show(); //or Form.ShowDialog(); //Shows the main form
}
The simplest way to do this is to first show (using ShowDialog, which blocks i.e. waits until the form is closed before continuing to the next line of code) the login form. If the login is successful, you dispose the login form and then show your main form; if the login fails, you end the application.
If, however, you want your main form to be visible underneath the login form (a not unreasonable requirement), then you need to first show the main form, and then display the login form (modally) from a method in the main form. In WinForms this requires some kind of hack, since you couldn't show the login form from either the main form's constructor or its Load event (as the main form won't yet be visible when the login form appears).
WPF may handle this better now.
When I want to Display a form (C#) by clicking a button in another form I usually create an object from the form that I want to show and use the show method :
Form2 f2 = new Form2();
f2.Show();
or I work with the "Owner" :
Form2 tempForm = new Form2();
this.AddOwnedForm(tempForm);
tempForm.Show();
the two ways generate the same results but what is the best and what are the differences between them?
The only difference apart from the naming is that in the second you call AddOwnedForm, and in the first you do not. Looking at the documentation we see:
When a form is owned by another form, it is minimized and closed with the owner form. For example, if Form2 is owned by form Form1, if Form1 is closed or minimized, Form2 is also closed or minimized. Owned forms are also never displayed behind their owner form. You can use owned forms for windows such as find and replace windows, which should not be displayed behind the owner form when the owner form is selected.
So if you want this behavior of the forms being minimized together, and one always being shown above the other, use AddOwnedForm. If you don't want this behavior, don't use it.
Microsoft uses Form f = new Form(); f.Show(); by default when creating a new Windows Forms project to show the main form, and there is probably negligible difference (in performance) between such methods. Using the Show() method, instead of just setting f.Visible = true; is also more logical.
When you use AddOwnedForm() you essentially lock the forms together in such way that when one form is minimized, the other is also. The form is also always displayed on top of the owning form, similar to a modal dialog.