Navigating between multiple forms in C# - c#

I have 2 forms of which one of them has a shockwave component added to it, it plays a flash movie and on click of a button created in flash the form 1 (frmFlashIntro) unloads and form 2(frmMain) starts.
since frmFlashIntro is a form which is used only once i want to unload the form rather than hide it. i also want frmMain to have complete control once frmFlashIntro closes as if it is the main form.
Hiding the form does not seem to be a good way to go about doing this thing.
What i am currently doing is i created a property in frmFlashIntro and added get and set methods to them (both of them are blanK)
public Form FormfrmMainRef { get; set; }
I added this code to a click event.
frmMain fm = new frmMain();
this.FormfrmMainRef = fm;
FormfrmMainRef.Show();
now this effectively shows my frmMain but keeps the frmFlashIntro also running so i did this
frmFlashIntro fi=new frmFlashIntro();
fi.Close();//checked with dispose also
i know i am doing something wrong in the last step. Can anyone please tell me how do i close that form and free the resources held by it.
Also i do not want to use an MDI

It looks like frmMain has no reference to the original frmFlashIntro. Your bit of code
frmFlashIntro fi=new frmFlashIntro();
fi.Close();//checked with dispose also
will actually create a second frmFlashIntro, so when you call fi.Close() you're actually closing the second form, not the original.
Theoretically you should be able to add a property to your frmMain that's a reference to frmFlashIntro, and then call Dispose on that so something like
frmMain fm = new frmMain();
this.FormfrmMainRef = fm;
fm.FormIntroRef = this;
FormfrmMainRef.Show();
And then in the load event of frmMain, add a call to FormIntroRef.close()
Ok, but, all that aside, I think the best way to solve this problem is to update the Program.cs file and then have something like
frmFlashIntro flashIntro = new frmFlashIntro
frmFlashIntro.ShowDialog();
frmMain mainForm = new frmMain()
Application.Run(frmMain);
Oh, and a second side note, generally, types begin with capital letters, and instances of those types begin lowercase, and the are camel cased. So you'd normally have something like
MainForm frmMain = new MainForm()
That should achieve your desired result.

Did you try to dispose the form with fi.Dispose() ?

Related

Showing and Hiding forms all over the project

I want to hide and close a form from every form of the project. I mean, if I am in form2, at clicking a button, I want form1 to be shown or hidden, depends of necesities. I can't find a way, I searched everywhere I could about OOP of doing this, but I still can't figure it out. Do I have to create a new instance of every form everytime?
Hmm, The reshowing i don't know 100% sure, but hiding them should be easy with the following code:
YourFormName newYourFormName = new YourFormName();
newYourFormName.Parent = this;
newYourFormName.ShowDialog();
this.Hide(); // or to close it:
this.Close();
Add this to the button, replace YourFormName for your form that you want to close.
For re-show you could try an if statement, im not at my PC atm so cant check for working code on that. but this should work.

Transferring information from one label in FORM1 to another label in FORM2

I have a label on Form1 called oldNumber.
When I click a button which loads Form2, a label called newNumber, will be displayed when form2 loads.
My presumption is for the label to display the number from the previous form as the form loads it must be placed within the Form2's load method. Although I'm not entirely sure how to call the oldNumber in Form1 to display in the newNumber Label on form2.
There is similar questions asked to this, but none have managed to help me.
Thanks!
P.s Im new to .NET, and winForms. So question is quite simple i know.
Modify your form2 constructor to receive a string
public class Form2:Form
{
public void Form2(string textFromForm1)
{
InitializeComponent();
this.labelOnForm2.Text = textFromForm1;
}
....
}
then when you call the Form2 Show/ShowDialog in Form1 instance
Form2 frm = new Form2(this.labelOnForm1.Text);
frm.Show();
This link might help you as there are many similar principles that can be done working with multiple forms that may rely on each other. It has a few links, even a full sample showing how to work multiple forms / methods / properties, etc

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# second form closing after initialisation

I am trying to create a simple c# application (my first attempt at c# so please be kind). I've created a form with a textbox to capture an "auth code", which is then validated and then a webclient fetches an xml file passing this auth code in to the request. The data sent back is parsed e.c.t.
What i want to do is once the xml comes back and ive done my checks to valid it is all fine. I want to close the first form and load up a second form where i will programmatically add the form components needed to display the xml data in a pretty format.
My problem is that im unable to get the second form to stay open (im no doubt invoking the second form in the wrong manner). Here's what i have:
// close current form
this.Close();
//open new form
xmlViewForm xmlView = new xmlViewForm();
xmlView.Show();
I'm sure you've spotted the mistake im making by now. but just to state the obvious for the sake of completeness, it closes the first form, opens the second, and then immediately the program exits (the second form flashes up for a second obviously).
I've tried a few things but none of them work (including using Application.Run(new xmlViewForm()); instead of instantiating the class and using the show() method. Obviously you know that doesn't work, and now i do too, although i dont understand c# even remotely enough to work out why.
Thanks for any help :)
The first thing that came to mind is that you are closing the form that you opened by calling Application.Run(new MyForm()) or something similar. This form has special significance; it is the "main form" of the application, and when closed, it signals to the application that the user wants to close the entire program, no matter how many other windows are open.
There are two possible fixes. First, and easiest, is simply to Hide() the form you don't want visible instead of calling Close() on it. Though invisible, it's still running, so the application doesn't close.
The second solution is to define a custom "application context" that should be run instead of the "default context" that is created by specifying a main form to watch. You do this by deriving a custom class from System.Windows.Forms.ApplicationContext. With this context specified, you can use it to control termination of the application based on something other than closure of the main form. Example code that launches two "main forms" and keeps track of whether both are still active can be found at the MSDN page for the class. You can do something similar by specifying Load and Close handlers for the main form, then passing them to the child form when the main form instantiates it, thus keeping a count of "open" forms, and closing out the full application when that number is zero. Just make sure the child form loads before closing the main form by calling childForm.Show() before this.Close().
You can not open the second form after closing the main form.
Do this:
//open new form
xmlViewForm xmlView = new xmlViewForm();
xmlView.Show();
// hide current form
this.Hide();
Main form can not be closed because it's the parent form. The child form will never show up if you close the main form.
Or change the xmlViewForm to main form by editing Program.cs file
Application.Run(new XmlViewForm());
Then you can easily call the other form first at the time of loading and close it as you please:
private void XmlViewForm_Load(o, s)
{
// hide current form, and this will remain hidden until the other form is done with it's work
this.Hide();
//open the other form
TheOtherForm _theOtherForm = new TheOtherForm();
_theOtherForm.Show();
}
private void TheOtherForm_Closed(o, s)
{
// show current form
this.Show;
}

.NET ShortcutKeys on ToolStripMenuItem in main Form doesn't work when other Form is active

I have an application (.NET / C#) with a main Form and two optional tool/property Forms. The main Form has Ctrl-S set as ShortcutKeys for File->Save. When the main Form is active, Ctrl-S works fine, but when one of the other Forms is active it doesn't work.
This setup in an application is so common, I'm sure there's a simple solution, but I just can't find it.
Regards,
HÃ¥kan Eriksson
I can suggest you to do sending old form reference to currently active form. For ex.
This is your old form : OldForm;
This is your active form : ActiveForm;
Make your ActiveForm opening with parameter like:
//This is global variable;
public OldForm frm;
public ActiveForm()
{
InitializeComponents();
}
public ActiveForm(OldForm myOldForm)
{
InitializeComponents();
frm = myOldForm;// at this point you got your OldForm !
}
So now, you can handle your old form from your activeform. You can reach everyControl of your OldFrom with using your frm variable. I hope this answer can you give you any idea.

Categories