How to hide a form from the main form - c#

I have been tweaking my program all day and I am having a problem hiding a form which will pop up saying "Please wait"
For example:
private void button12_Click(object sender, EventArgs e)
{
form2 wait = new form2();
pw.Show();
}
private void button13_Click(object sender, EventArgs e)
{
form2 wait = new form2();
pw.Hide();
}
This will not work, although I am sure this isn't news to the casual C# programmer. Is there a simple way to do what I am attempting? I have tried searching online and I did find something although I wasn't 100% sure what they were trying to do. I was going to find an example to show you but I closed page - Typical. However I think they were trying to overide the show and give you control over the .show with a bool?

The code isn't working as you expect it to because the form2 inside of button12_Click is different from the form2 inside of button13_click. Notice that you are using the new keyword twice. So in button13_click, you are creating a new form2, and then hiding it, even though you haven't even shown it yet!
Instead you can create a single form2 instance to share between your two methods:
//define this code outside both of the methods below
form2 _waitForm = new form2();
private void button12_Click(object sender, EventArgs e)
{
_waitForm.Show();
}
private void button13_Click(object sender, EventArgs e)
{
//this will hide the same form2 that was shown in button12_Click
_waitForm.Hide();
}

Related

Hiding Form 1 and Show form 2 on Windows Form 1 Load

Am a newbie when it come to C# Programming. Here's my trouble:
I want to Show a new Form 2 and Hide Form 1 on the Windows Form 1 Load.
Here's my current codes;
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Hide();
}
My en-counted Error with the current Code:
When Form 1 load its loading Form 2 but it's not hiding itself. this.Hide Statement not working, I've try this.Close but this will Close the entire software as it's closing the main form.
Can anyone kindly help me with this error.
The Error i thought is that you are showing the form2 before hiding the previous form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
form1.Visible=false;
Form2.Show();
}
You can also make form2 as modal by using show dailog method() by which focus is given to the form2 and form1 becomes inactive though it will be shown.
modal and modeless forms https://msdn.microsoft.com/en-IN/library/aa984358%28v=vs.71%29.aspx
Here's how I manage to make this work.
New Codes:
Form 1
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
}
private void Timer_Tick(object sender, EventArgs e)
{
if (Properties.Settings.Default.Status == "Form2Visible")
{
this.Hide();
}
}
Explanation:
On the Windows Form 1 Load am showing the the Form 2, then using a timer to verify a Properties.Settings Value. If the Properties.Settings = Form2Visible, Form 1 will Hide. Once am done on Form 2 I simply need to change the Properties.Settings to something else and stop the Timer on Form 1.
If their is a simplest way let me know.
You can use Visible property to hide your form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Visible=false;
}

Windows form opening other forms

What I want
I am creating an application which has two functionalities. Both functionalities have their own form (called FactuurForm and VerhuurForm). I have another Form called Home, which has, among others, two buttons. Depending on which button is clicked, I wish to open one of the two forms, and complete close the Home-form.
What I have
Currently, I have the following code:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Home home = new Home();
home.ShowDialog();
if (home.kiesFactuur)
{
FactuurForm factuur = new FactuurForm();
home.Close();
factuur.ShowDialog();
}
else if (home.kiesVerhuur)
{
VerhuurForm verhuur = new VerhuurForm();
home.Close();
verhuur.ShowDialog();
}
}
}
kiesFactuur and kiesVerhuur are booleans which in my Home class, initialized as false. As soon as I click on of the buttons, the corresponding boolean will flip to true, triggering the if-statements to close the home-form and open the new form.
My question
Altough my current codes works, it seems a bit much for such a simple functionality. I feel like I wouldn't need the booleans and this go all be done easier. So is there an easier/better way to do this?
I've also thought about creating multiple Main functions. Clicking a button would activate the corresponding new Main function and terminate the current Main. Is this even possible and if so, is it a good solution?
I don't exactly understand the need to completely close the home form. I'd just place 2 eventhandlers for each of the buttons and call the following code on them. The first form will be hidden and closed when you close your subform.
private void ShowSubDialog(Form form)
{
this.Hide(); //makes your main form invisible before showing the subform
form.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
ShowSubDialog(new FactuurForm());
Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
ShowSubDialog(new VerhuurForm());
Dispose();
}
private void Factuur_Click(object sender, EventArgs e) {
LoadForm(new FactuurForm());
}
private void Verhuur_Click(object sender, EventArgs e) {
LoadForm(new VerhuurForm());
}
private void LoadForm(Form f) {
this.Hide();
f.ShowDialog();
this.Show();
}
Add this to your Home form, remove everything after home.ShowDialog() from Main, and make Facturr_Click and Verhurr_Click handle their respective button's click events. This will allow Home to hide/show automatically.
You should replace your code like this :
if (home.kiesFactuur)
{
FactuurForm factuur = new FactuurForm();
factuur.Show();
this.Hide();
}
else if (home.kiesVerhuur)
{
VerhuurForm verhuur = new VerhuurForm();
verhuur .Show();
this.Hide();
}
In the VerhuurForm and FactuurForm you may ovveride the event of closure like this :
public VerhuurForm()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(VerhuurForm_FormClosed);
}
void FormClosedEventHandler(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
To be sure that your application is closed if you close the form because the Home still active but hidden.

How to open recently closed or hide form

I've researched thoroughly but still i cant find the best solution for this..
I have a 3 buttons, BACK - HOME - FORWARD ..
This is just like the buttons on the upper left of browser .. and im trying to do this on a form..
what i have is this ..
the back button code is
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
}
// simply hiding the form .. so that the previous form will be shown..
the home button code is this..
private void button2_Click(object sender, EventArgs e)
{
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "HomePage")
Application.OpenForms[i].Close();
}
}
// this will show the HomePage form and close other forms whos name is not "HomePage"
the Problem is when i press the Back Button , im hiding it .. how can a button will re open a previously closed or hided form ?
I hope you can Help me! Thanks!
private void button3_Click(object sender, EventArgs e)
{
???????
}
You would need to store a reference to the form that you wish to open again.
There are a couple of options to do this, but if you simply wanted to allow the user to go "Forward" once, you could just store a reference to the form like so:
internal class MyHistory {
internal static Form LastForm;
}
// ........
private void button1_Click(object sender, EventArgs e)
{
MyHistory.LastForm = this;
this.Hide();
}
// ........
private void button3_Click(object sender, EventArgs e)
{
MyHistory.LastForm.Show();
}
Of course, you could maintain a full stack of history items and traverse back/forwards through them if you wanted to be more comprehensive than this.
Note that, if you .Close() your form, you won't be able to reopen it as the reference will be disposed of once it is closed. This method would only work if you were to .Hide() it, which keeps the form instance valid, just hides the form from the user's view.
you could use a form list which holds all initialized forms. that way you can hide, show, add and remove forms dynamically.
List<Form> lstForms = new List<Form>();
then when you add a form:
Form newForm = new Form();
lstForms.Add(newForm);
Hiding a Form:
lstForms(x).Hide(); //x = index of Form you want to hide
Showing a Form
lstForms(x).Show(); //x = index of Form you want to hide
Removing a Form (when closing it for example)
lstForms.RemoveAt(x);
that way you can dynamically work with forms and it is much easier to keep an overview if you have many forms...

Prevent form from showing multiple times

In windows form (c#), i am showing a form when user click on button, it is working fine form is visible to user, but if user click again on the same button the same form is opening again two forms are displaying. Is there any way to prevent this, please give me any reference for this thank you. This is my code....
private void button1_Click(object sender, EventArgs e)
{
Form2 obj = new Form2();
obj.Show();
}
You are most likely doing something like this:
void button1_OnClick(object sender, EventArgs e) {
var newForm = new MyForm();
newForm.Show();
}
So you are showing a new instance of the form every time it is clicked. You want to do something like this:
MyForm _form = new MyForm();
void button1_OnClick(object sender, EventArgs e) {
_form.Show();
}
Here you have just one instance of the form you wish to show, and just Show() it.
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(MyFormType))
{
form.Activate();
return;
}
}
Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();
i tried more than a ways to compare which one is better.
but i think this solution must be better than the answer.
You can try something like
private Form f;
private void button2_Click(object sender, EventArgs e)
{
if (f == null)
{
f = new Form();
f.Closed += f_Closed;
f.Show();
}
}
void f_Closed(object sender, EventArgs e)
{
f = null;
}
You are most probably creating a new instance of the form every time in the Click handler of the Button.
So you ill need to move the Form object creation outside the Button_Click.
Here's a good example of a proven solution
This will open the form if it is not already open.
If it is already open, it will place it in the foreground.
namespace MainProgram
{
public partial class Form1 : Form
{
private Form formNew = new FormToShowSomething();
private void button1_Click(object sender, EventArgs e)
{
formNew.Show();
formNew.Activate();
}
}
}
the easiest solution to your problem is replacing the Show command with ShowDialog, that way you won't any problem when it comes to preventing a form to show up twice
Form2 obj = new Form2();
obj.ShowDialog();
the code: .ShowDialog(); is what we are currently looking for that will solve the issue
10 years after , like the band :p
Thought to share the code that works for me. Nothing fancy, just checking if the form instance exists. Also, I don't prefer the ShowDialog, because the user is 'trapped' in that form and I find it annoying. The user might want to check other info from another source, for example when filling an online form and needs to copy paste a field info.
private void button1_Click(object sender, EventArgs e)
{
var obj = Application.OpenForms.OfType<Form2>().Select(t => t).FirstOrDefault();
if (obj != null)
{
obj.BringToFront();
}
else
{
obj = new Form2();
obj.Show();
}
}

How to safely close form soon after it's shown?

From my main form, I open a couple of other forms on mouseclick, like so:
Main Form:
...
private void btn_Click(object sender, EventArgs e){
frmNewForm newForm = frmNewForm();
newForm.Show();
}
In the new form, I'd like to check if there's any data to show, and if not immediately close the form.
New Form:
...
public frmNewForm(){
InitializeComponent();
// check if opening this form makes sense
if(noData){
Close();
}
}
However, I get an exception thrown at frmNewForm.Show(): The object can't be accessed.
I apologize if the translation isn't exactly the same as Visual Studio's: I'm working with another language version.
Anyway, what can I do to safely close frmNewForm?
You don't want to do it in the constructor for the new form. Rather, you need to do it on the Load event so that it finishes loading before you close it.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx
private void Form1_Load(object sender, EventArgs e)
{
if (noData) this.Close();
}
Alternatively, if you know you won't need to open the form, check before showing it!
If noData is a public Boolean property of your frmNewForm class, you can do this:
if( !newForm.noData )
{
newForm.Show();
}
Make sense?

Categories