Multiple windows forms being called.. why?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrFlabulous
    New Member
    • Feb 2012
    • 9

    Multiple windows forms being called.. why?

    Hi, I'd appreciate someone taking a look at this as I can't see what's going on. I have a parent form populated with buttons, when a button is clicked a new form opens via ShowDialog. When I close the child form the parent form updates, and when I click a button the second time the child form spawns twice, but the second one is only opened when the first is closed. Every time the parent is refreshed a duplicate is "primed" to appear the next time the button is pressed. I only want the one child form to appear, yet after e.g. 10 parent refreshes 10 child forms will be called - consecutively, not concurrently, I should add; each child will appear when the previous one is closed.

    Code below:

    Code:
    public void LoadButtons()
            {
                int ButtonCount = 0; // Counter for the database, but no checksum
                Routines.BuildCurrentDatabase(); // Calls a string array
                foreach (Control bttn in T7_Layout.Controls)             {
                    if (bttn is Button)
                    {
                        string textline = Routines.LinesOfText[ButtonCount]; //takes one line from string array
                        bttn.Text = textline;
                        ButtonCount ++;
                        bttn.Click += new EventHandler(CallButtonView);
                    }
                }
    public void CallButtonView(object sender, EventArgs e)
                Button btn = (Button)sender; // The sender must == col1 in the text files
                string out_text = btn.Name;
                string number_out = out_text.Remove(0, 6);
                int Button_Select = int.Parse(number_out);
                vars.ButtonSelector = Button_Select - 1; // storing the number in a public variable
                Button_View showbuton = new Button_View();
                showbutton.ShowDialog(); // Bring up child form
                LoadButtons(); // called when the child is closed.
                } // Abridged - the variables are reset to zero or ""
    So when LoadButtons() is called it appears to be asking for duplicate forms to be called next time a button is clicked. Is it something to do with the bttn.Click event handler?

    I'm sure the solution is obvious, but I just can't see it. Hope you can help,

    Many thanks

    Howard Parker
  • michaeldebruin
    New Member
    • Feb 2011
    • 134

    #2
    I have no idea of what you are trying to do. But for calling on another form I think it is better to use something similar to the following code
    Code:
    using(<your from><creating variable to get form> = new <your from>)
    {
    hide();//telling your current form to hide
    <the variable you've created>.ShowDialog();//opening the other form
    <here choose what you want to do with the form you told to hide>
    }

    Comment

    • MrFlabulous
      New Member
      • Feb 2012
      • 9

      #3
      OK, I think what I'm trying to do is remove the EventHandler once the child form has been called.

      Having run a foreach loop and assigning each *bttn* individually, the bttn.Click += new EventHandler(Ca llButtonView); statement adds a new event when a button is clicked. How do I go about clearing that event? Do I have a bttn.Clicked -= new EventHandler(Ca llButtonView); immediately afterwards? Would that work?
      Last edited by MrFlabulous; Feb 23 '12, 02:36 PM. Reason: Clarity

      Comment

      • michaeldebruin
        New Member
        • Feb 2011
        • 134

        #4
        First of all can you tell me what you are trying to do? Because I think that you are trying to do something, which can be done on different and much easier way.

        Comment

        • MrFlabulous
          New Member
          • Feb 2012
          • 9

          #5
          OK, the parent form displays several buttons, all of which are populated with text during the LoadButton() routine, using a foreach loop. The idea is that by clicking a button, a child window is displayed with all the information that the button links to. The child form is editable, and when it's closed the parent form updates so that the button has new information on it.

          Essentially the parent form is supposed to look like a bunch of pigeonholes that are clickable. The child form displays the contents. And when you leave the child form the parent should be updated automatically. It all works, except for the multiple child forms that are springing up.

          The foreach loop has the bttn.Click += new EventHandler(Ca llButtonView) command. CallButtonView is a method that brings up the child form via a ShowDialog command. When the child form is closed the buttons get updated but the EventHandler is not cleared, meaning the next time a button is pressed there will be TWO calls to the child form. And the next time will add yet another event.

          I tried using bttn.Click -= new EventHandler as well but of course the child failed to load at all. And...

          foreach (Control bttn in T7_Layout.Contr ols)

          ...seems to internalise all the bttn controls, so I can't simply clear the bttn Events.
          Last edited by MrFlabulous; Feb 24 '12, 08:05 AM. Reason: clarity

          Comment

          • michaeldebruin
            New Member
            • Feb 2011
            • 134

            #6
            First of all you don't need that event, because like I said before the "using" thing does the same trick but on a much easier way. But in your situation you should use the following code:
            Code:
            using (<the form><the variable> = new <the form>
            {
            hide();//will hide your parent form
            <call the variable again>.ShowDialog();//telling the variable to show it's dialog
            show();//you can choose many methods to implement, but in your case you need show because you want your parent form to show up again.
            }
            And then do something with a counter in a load event of your child form to choose which text it must show. You can also use a if statement (or in the case of many options a switch is maybe easier) if you have only one child form. On this way you can choose which text it shows and which step of it.

            Comment

            Working...