Windows Form Application, need assistance - c#

I know I'm a bit underqualified to be on here, but seeing as my teacher is no good, was wondering if you could help with this extremely obscure problem that i have.
In this task, we have been asked to create a quiz through windows form application on c#. To be honest, I actually have no idea what I'm doing and am just trying to follow a booklet my teacher has given me. However, it appears this booklet isn't working, and the problem to obscure to be answered with a youtube video.
The problem I have is that I am running the form, and recieving problems when I click a button to access a different form within the program, and instead of just opening the linked windows form, it appears both forms remain open, regardless of the this.Close(); that I have coded. When both forms are open, I am unable to access the sought after form, as when i attempt to click on it it cuts back to the form where I initially clicked the link label. I realise this is very badly written, and I have no idea what I'm doing compared to you guys, but any input whatsoever would be greatly appreciated.
I have linked screenshots below. Ideally I'd like for the Program to run frmSplashScreen, the timer running out, then frmPlayerLogin, where I would click the link label to access frmPlayerSelectionNew.
Screenshot link: http://imgur.com/a/K6RqE
Edit
I've been asked for the key code to be written here
The code on the frmPlayerLogin Screen relating to the link label is this.
private void lblClickToRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form myNextScreen = new frmPlayerSelectionNew();
myNextScreen.Show();
this.Close();
}`
The code on Program.cs is as follows
`static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmSplashScreen());
Form myNextScreen = new frmSplashScreen();
myNextScreen.Show();
Application.Run(new frmPlayerLogin());`

You have two problems.
ShowDialog() is modal. That means when you use it, instead of Show(), you are halting further execution until you enter a response and return a DialogResult
myNextScreen.Show();
We can't see the rest of your code from that image, and there's a way to post code on here. If it's more than what I just pointed out, we'll need that to help.

Related

C# WPF Caliburn.micro handling opening and closing windows

1153/5000
I spent the last two days searching for a solution. Unfortunately, I found nothing, or at least nothing that would explain to me how to manage opening and closing windows.
I am new to programming, maybe that's the main problem.
But, my problem is:
I am doing a small WPF program using caliburn.micro and mwwm. The problem is opening and closing windows.
I would like to do a window management class or something like that, but I don't know how to aproach.
Could you explain to me step by step how to do it? I read the caliburn.micro documentation, but it doesn't explain how to do it.
In general, the situation looks like this:
I have a main window from which you can open several others. When i'm opening a window, the main window disappears. After closing that window, the main menu appears again. I did it with:
'''App.Current.MainWindow.Show();'''
I would like to manage all windows. Opening them and closing. I came to the point that I should do it with Iwindowmanager ... but how? What? Where? I have no idea. Hence the request for a step-by-step explanation.
ps. Forgive me my English.
edit:
757/5000
There's a problem with that, I don't have much code because I don't know how to go about it. And wherever I was looking, I couldn't find a hint.
But of course I will show what I have:
To open one window from the main menu I have this:
IWindowManager manager = new WindowManager();
public MenuView()
{
InitializeComponent();
}
private void ActionButton_OnClick(object sender, RoutedEventArgs e)
{
manager.ShowDialog( new FirstViewModel(), null, null);
this.Hide();
}
To close this window I'm using
App.Current.MainWindow.Show();
this.Close();
but if I use the first part of the code in a window other than the main menu, I don't know how to go back.

How to handle form show order on startup

I'll try to make this question as uncomplicated as possible. I have three forms frm_Splash which checks for updates; frm_Wizard which completes first-run setup; and frm_Main which is the main program. These forms' relationships are diagrammed below:
Right now in Program.cs I have this code:
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frm_Main());
}
which sets frm_Main as my main form. What is best practice to handle frm_Wizard and frm_Main? I do not want them to load before I have finished updates (if there are any).
After this, if the wizard needs to be shown, the splash screen should disappear and the wizard should appear.
Finally, once all the updates and first-time setup is done, the main form shows (but not before). How do I accomplish all this?
Some things I know:
I know I can set frm_Splash to be the startup form, but then when I close it, the whole program closes.
I could also hide forms, but doesn't that waste memory with forms
sitting in the background?
And finally, I'm looking for some general code or concepts of how to do this. I already am familiar with c# coding, so you don't need to code everything. I'm merely looking for a best practice to handle this. i.e. Should I be coding in the Program.cs, or each form's load event? If there's anything unclear about my question, please let me know before you downvote! Thank you.
One thing to note is that you do not need to use Application.Run to launch a form. So you can just create a 3 step process in your Main function:
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Show splash form (which checks for updates)
FormSplash splash = new FormSplash();
splash.ShowDialog();//will wait until splash closed
//check if first run and show if needed
if(IsFirstRun())
{
FormWizard wizard = new FormWizard();
wizard.ShowDialog();//will wait until wizard is closed
}
//Finally, run your application as normal
Application.Run(new frm_Main());
}
NOTE: I am not aware of the benefits of using Application.Run to launch you main form, I assume one of them is allowing access to Application state functions (such as Exit), but I can say I have used this technique for a splash screen in the past, and have not had any issues with doing it this way

How to create navigation inside program Form

Warning! This is noob question probably! Sorry in advance.
I'm learning C# (using MS Studio 2013) and I'm having hard time creating some kind of decent navigation in simple desktop program.
Basically what I want is this: MenuStrip with options like "calculate something", "Calculate somethingelse"... and other (that I can easily add later - like dynamic menu on a webpage). If you click first option inside the Form connected with the StripMenu you will get some controls that allows you to do something(like inputs on a webpage). If you click the second all these options will disappear and you will get a fresh set of controls where you can do somethingelse (simply another webpage to play with).
What is the best way to do it (I find it amazing hard to find out :) ). Only way I figured out (more from experience in js then tutorials) is to use show/hide like in javascript/html.
ExamplePanel.Visible = false;
ExampleOtherPanel.Visible = true;
But this doesn't seem right - I think it would be impossible to manage in bigger program (not only in code, but visual designer too - you can only fit that much Panels inside Form).
Any advice? Or at least a link to material where I can find out?
EDIT:
Finaly I gave up and used multiple Forms as sugested in answer.
private void MenuStripExample_Click_1(object sender, EventArgs e)
{
SomeForm SomeForm = new SomeForm();
this.Hide(); //Hide the main form before showing the secondary
SomeForm.ShowDialog(); //Show secondary form, code execution stop until SomeForm is closed
//this.Show(); //You may uncomment this if you want to have the previous Form to get back after you close new one
}
You normaly don't hide and show panels with different layouts. This is not a good design.
If you have complete different navigations/control sets, then create a new Form which is responsible for the control set.
If you don't want to use new Forms take a look at the TabControl.
You may also want to take a look at MDI-Container. You can use a Form as a MDI-Container and display various other Forms as child-elements inside of this container.

C#: Close and Open two forms from a separate form

I have 3 forms. How can I make it so that one form is shown with .Show() and the other is hidden with .Hide() from a separate form?
This is part of my code
private void buttonYes_Click(object sender, EventArgs e)
{
LoggedIn loggedinform = new LoggedIn();
loggedinform.Hide(); // Hides another form that is in the background
MainForm mainform = new MainForm();
mainform.Show(); // Show first form
this.Hide(); // Hides current form
}
One problem, the LoggedIn form does not hide itself. From the looks of it it skips it and just goes for the mainform.Show();
Is this a bug or do I need to do something else?
The line LoggedIn loggedinform = new LoggedIn() is going to create a new instance of that login window. That might be useful if, say, you intended to show 5 "Login" windows onscreen all at once. I think what you want to do is retrieve a reference to the login window that is already showing, and hide that; so, avoid creating a new one.
Properly passing references to existing objects around the program is kind of a structural problem, and one that I ran into quite a bit in my early programming days. The quick, unclean, and generally not-recommended way is to declare instances of those singular objects (like, maybe, your login window) as static, so they can be retrieved anywhere. However, to fully answer your question in the best way, maybe you could describe the structure of your program a bit more (full code isn't necessary, just generally-speaking, what the flow is between classes)
Ok I figured it out. I can use
Application.OpenForms[1].Hide();
[1] is the form I'm trying to hide. And it worked.
I also realized thanks to Katana that it makes sense why it wasn't working because it was basically making a new instance of the form instead of finding the current one. Sorry that my code is a mess.

C# WinForms Wait on form to proceed

I'm tired and hungry, so I might of missed it, but from what I can see no existing post covers this...
I'm writing a plugin for an application. My plugin loads a form to get some data specifically, it uses the webcam to scan for a barcode. Once it's found a barcode, the form hides itself (incase it's needed again later). This is how I currently call the form that does the barcode work:
string readData = null;
if (eye == null)
{
System.Windows.Forms.Application.EnableVisualStyles();
eye = new CamView();
}
eye.Show();
if (eye.found)
{
readData = eye.readData;
}
return readData;
So, my problem is that eye.show() doesn't block. It makes the form appear and carries right on before there's a chance for the barcode to appear. I imagine I need to use some form of threading or locking, but my crude attempts to do so have just frozen the interface completely.
The "eye" form is basically just a viewfinder for the webcam, and relies on the camera_OnImageCapture event to make it do it's image checks for the barcode.
Is there an elegant way to make the application calling the plugin wait for the form to finish? Or do I just need to add an accept button to the "eye form?"
Cheers. And humble apologies if this is in anyway a repost.
.ShowDialog();
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
"You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed."
You are on the right track. You change the code to show CamView as a modal dialog but do no add an Accept button. Instead change camera_OnImageCapture to close the dialog.

Categories