Showing and Hiding forms all over the project - c#

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.

Related

Showing a modeless form's 'title bar' inside the parent form when it's minimized

I'm trying to implement some complement views inside my application and I would like to have a better layout control over them. I don't know how to explain in words what my desired functionality is, so I made it through with some photoshop help, hoping you could give me a hand to implement it.
My application now looks like this:
(i need reputation to post images so.. sorry for the links)
http://i59.tinypic.com/2ikv8m1.jpg
When I minimize the modeless form which is focused in the previous image, I would like to be able to see it (and handle it to maximize or close) inside my main form as I show in the image below (made it in photoshop)
http://i58.tinypic.com/1e28go.jpg
Hope someone can lead my into a solution and thanks for the support.
EDIT: I need to be able to move that form outside my main form, even to a different monitor.
If you don't want to use the MDI approach, then set TopLevel of the modeless Form to false and add it to the main Forms Controls collection before showing it:
Form frm = new Form();
frm.TopLevel = false;
this.Controls.Add(frm);
frm.Show();
*Obviously changing Form to the correct type of your modeless form.
If i understand what you are trying to do, you want to minimize a certain form but still see it within your app (im assuming like Excel or Word)
You can do something similar to what Idle_Mind said, but enclose both in a Form instead of the parent.
Form fParent = new Form();
fParent.Dock = DockMode.Fill;//i think this is the syntax. Use this if you want the form to fill to the screen
Form fChild = new Form();
fChild.TopLevel = false;
fParent.Controls.Add(fChild);
fChild.Show();
Here, it should minimize to the lower left part of the parent form. You can then size the parent to whatever you want it to be.

How to make an application which has another screen in the extended screen?

I want to make an application which has another screen in the extended screen in another monitor (or in my case projector), similar to the presenter view in power point.
I want to use C#, and I guess in windows form application or wpf template there is no dual screen application support.
My first guess is to make 2 separate project and connect them, but I'm not sure about it either.
Any idea how to do it, or at least someone has a tutorial related to it?
I've looked at google, but still not sure what the right question to the issue, since google always showed "how to set up dual screen" for users.
Please clarify anything if my question is unclear. Thanks.
The easiest method I can think of is to simply create a second form (assuming you use Window Forms for your project), so you'd have Form1 as the default, and Form2 as your second form.
Run Form1 by default, and then while loading Form1, do something like this:
if(System.Windows.Forms.Screen.AllScreens.Length > 1)
{
Form2 form2 = new Form2();
form2.Show();
}
Then within form2's shown event, set it's position to the second screen (and perhaps maximize it, depending on what you need).
private void Form2_Shown(object sender, EventArgs e)
{
this.Location = Screen.AllScreens[1].Bounds.Location;
this.WindowState = FormWindowState.Maximized;
}
From that point on (and provided you keep a reference to Form2 within Form1), you can easily exchange information between the two forms as needed. You could even expand it to support 3-4 monitors, simply by making multiple Form2's, one for each screen

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

Navigating between multiple forms in 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() ?

Categories