Click here to Skip to main content
15,886,736 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
when i press button many times its repeatedly open or create form2 how, when form2 is already open so when i press button no more from2 windows show up
C#
private void button1_Click(object sender, EventArgs e)
{
Form2 myForm;
myForm = new Form2();
myForm.Show();
}
Posted

Just hide this button after the first click, http://msdn.microsoft.com/en-us/library/system.windows.forms.control.hide.aspx[^].

—SA
 
Share this answer
 
private void button1_Click(object sender, EventArgs e)
{
Form2 myForm;
myForm = new Form2();
myForm.ShowDialog();
}
 
Share this answer
 
A better answer than all of these would be, read the answer you got to the last question, use the code you were given there, and once your form is a member variable, you can check if it's open and you can't create more than one, as you have one only that you work with, instead of creating a new one every time this method is called.

As you're obviously just playing, why not read some online articles or buy a book so you can learn this properly. There's major gaps in your knowledge that mean the fact you have two forms on the screen probably makes you think you know a lot more than you really do.
 
Share this answer
 
Comments
snorkie 13-Feb-12 14:16pm    
Christian, that's a bit harsh. Not everybody is a gifted programmer. Looking at the last post and the current post, the answers were not easy for a beginner. Using phrases like "member variable" may not mean anything to a newbie. I'm hoping that the simple solution I provided as number three will be enough to get him/her moving forward and gain some confidence. I think we get stuck on elegant solutions sometimes and need to refocus on dead simple.
Christian Graus 13-Feb-12 14:20pm    
He was given the source code to make it a member variable in the last question. What he's posted, shows he did not use what he was given, although he marked it as the answer. ShowDialog IS dead simple, and the ONLY thing to do if your form is locally created IMO. But, then it's no longer a floating dialog, which may be what the OP wanted. Your answer adds value by pointing that out, but the fact is, if this person wants help, they should do the things they've been advised to do. What's the point of giving an answer to someone whose next question shows they didn't listen to what they were told last time ?

In addition, people who write random code, ask random questions and don't listen to the answers, are not going to learn anything. You can't use C# without knowing about what happens when you create a new instance of a class, so my advice may seem harsh, but it's the only path for this person to end up having any clue about development. I didn't insult them, I just told them the only possible way for them to move to the point of being able to code.
snorkie 14-Feb-12 10:13am    
That's fair. Thanks for your response.
W Balboos, GHB 13-Feb-12 14:21pm    
Not a debate on the goodness of the solutions, but mine version (w/flag in constructor) allows the form instance to take care of itself.
Christian Graus 13-Feb-12 14:23pm    
Well, I don't want to enter in to a debate, either. Fundamentally, this user has asked less than an hour before this question, how to track the form instance so another button can close it. The fact that he's asked that, and been given the code, means that he should be using my solution, automatically. The fact he's not, means he's ignored the advice he was given, and almost certainly could not understand your reply, and perhaps not mine, either.
Hello,

I think it can help you:

C#
public partial class Form1 : Form
{
    private Form form2;

    public Form1()
    {
        InitializeComponent();

        InitializeForm2();
    }

    private void InitializeForm2()
    {
        form2 = new Form(); //You can add some details
        //I prefer to have a ready form for example: ReadyForm
        //then:
        // form2 = ReadyForm();
        // Instead of form2 = new Form();
    }

    private void MyButton_Click(object sender, EventArgs e)
    {
        if (form2.IsAccessible)
            return;

        if (form2.IsDisposed)
            InitializeForm2();

        form2.Show();
    }

   //....

}
 
Share this answer
 
v3
Declare
C#
public static bool isOpen = false;
in Form2
set
and On PageLoad_Form2
C#
isOpen = true;

Now on Button Click (that open Form2)
C#
if (Form2.isOpen==false)
            {
  Form2 bc = new Form2();
   bc.Show();
               
            }

Hope It will solve your problem...
 
Share this answer
 
majid54787,

Try the following modification

C#
private void button1_Click(object sender, EventArgs e)
{
Form2 myForm;
myForm = new Form2();
myForm.ShowDialog();
}


The .ShowDialog() will force the other form in front. Until that form is closed, there is nothing you can do on the parent form.

Hogan
 
Share this answer
 
What you need is a static boolean flag, like ifForm2Open, which you set to true in the constructor for Fform2 and set to false in the destructor.

Now, again in the instructor, you add code Form2.close() if ifForm2Open is true.


You can also make the flag publicly visible and use it to control the enable/disable from the parent window/class.



 
Share this answer
 
Comments
Shahin Khorshidnia 13-Feb-12 14:50pm    
Static? why?
W Balboos, GHB 13-Feb-12 14:59pm    
I think you took static literally, in the programming sense (bad choice of words).

If the poor choice of a key word isn't your problem, I'll explain the concept:

You wish to have a value that will not be modified, and (my C++ background is showing) would be visible in all (potential) instances of the form. For C#, declaring the value at class scope would do the trick. The real declaration of public static, as one could do in C++, would make the value visible to the calling routine (even if you had many instances). This allows you to control not only instantiation of the class (Form) but perhaps meddle with other things that use the Form (even in other classes as the static declaration makes them visible in all instances).

You could, in further, use a static int and have allow a limited number of instances of the window to be instantiated. With increment/decrement to keep track, or just to limit usage while a particular application is still running. Just declaring with a scope at class level' doesn't give you that.

Either way works.
Shahin Khorshidnia 14-Feb-12 6:22am    
Hello,
I didn't told that Static was always a bad choice. But in this situation, Majid has a parent form and a child one, ok? He wants to open the child form on presseing a button "once at time". He could plan to have a private boolean flag behind the parent form.


private bool form2IsFlagged;



Or a better idea is:



private System.Windows.Forms.Form form2


Why static and why an untimely use of stack?
The static declaration exists because it is useful. Have you never used a static functions (usable without declaring the class?). They exist throughout the .NET namespaces. You probably use them without knowing it.
    Eg.   Convert:ToString(142);

A static member is visible through all instances of a class - and it's values is the same in ALL instances. That allows setting the flag when you open the first instance to be detectable when trying to open the second instance: you can then the second instance to not open. Similarly, when you finally close the first (and only) instance, it clears the flag.

RESTATED:
A static variable has a fixed memory location (in the heap rather then the stack). It persists so long as the program that contains the class is running. It therefore allows the transmission of information amongst the different instances of the class. As I noted, you could use an int (instead of a bool), and then keep track of how many instances are currently open.

Your
private bool form2isFlagged - will be useful how ? ? ? Withing the form it's only visible in the form: useless! If it's outside your class, you need to maintain it - what will be its scope?

private System.Windows.Forms.Form form2 will help you how? The form can be open or closed without being null, depending upon scope. When a form is closed, its pointer doesn't need to null - it wasn't null before the form was opened, either, once you declared an instance. Do you want to be sure if the window is open or ALMOST sure ? Hell of a program that would be.

Why a static symbol? Part of the form's class - and set up as I noted in my original post, take care of itself when it comes to instance count limits. If private, can't be monkeyed with outside the form class. Why wouldn't you want the class to do the work for you.

FURTHERMORE - if you are clever, clicking on the button that created Form2 could be used to move the existing instance to the top of the Z-order, giving the user the perception that the program is cooperating.

A lot of bang for the buck if you use static.
 
Share this answer
 
Try This

C#
private void mnuYourForm(object sender, EventArgs e)
{
    if (IsFormAlreadyLoaded("YourFormName") == true)
    {
        return;
    }
}

Call This routine Before you open your form as shown above
C#
private bool IsFormAlreadyLoaded(string formToLoadName)
 {
    foreach (Form frmChild in this.MdiChildren)
    {
        if (frmChild.Name == formToLoadName)
        { frmChild.Activate(); return true; }
    }
    return false;
 }


This Will Definitely work
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900