How to declare an array of windows form's?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fernand
    New Member
    • Dec 2010
    • 2

    How to declare an array of windows form's?

    Hi, i am a newbie at programing so bare with me please.

    I'm making a windows form based application, i started by adding my forms and classes for each window i'm creating but i got my self with the question: How to create a function that allows me to use a "Next" and "Back" buttons to navigate through the forms without losing the information and hiding the ones that are not currently being used keeping the information already typed, i search over the net and i found the options .Hide() and .Show() or the .Visible = false/true. However for each of the "Next" or "Back"butto ns i would need to instance a new form wich would not allow me to retrive information from previously used forms and moreover would create windows until my computer just crashes(i imagine). So yeah basicly i though of making an array in a class myApplication where i declared the size as the number of windows im using but i dont have a clue as to how to do this, any suggestions and/or code would be greatly appreciated. Thanks in advance.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I think the problem you're encountering is that when you close a form, it gets disposed. Once disposed, you can't show it again, you'd need to instantiate a new one. This makes using an array rather hard... though you can do it.

    To make an array, any array, you just need to use the array syntax. Here it is in its truest form...

    Code:
    Form[] myFormArray = new Form[10];
    That creates a Form array that has a length of 10; however, it's important to note that no Forms have been created. Everything in that array is null. You'd need to add them in. It'd take a bit of work for you to link everything up and pass the current page back and forth since you're effectively hiding and showing the same form.

    Let me suggest an alternative to you...

    Why not create a UserControl for each of your pages, then have your main form just have a panel, a next button, and a back button. Now you can create an array of UserControl objects, instantiate each index as desired, and keep track of it that way. Here's an example...

    Code:
    public class Page1 : UserControl { ... }
    public class Page2 : UserControl { ... }
    public class Page3 : UserControl { ... }
    Code:
    public class MainForm : Form
    {
      private UserControl[] m_pages = new UserControl[3];
      private int m_currentPage = 0;
    
      public MainForm()
      {
        m_pages[0] = new Page1();
        m_pages[1] = new Page2();
        m_pages[2] = new Page3();
    
        UpdatePanel();
      }
    
      public void UpdatePanel()
      {
        m_pnlCurrentPage.Controls.Clear();
        m_pnlCurrentPage.Controls.Add(m_pages[m_currentPage]);
      }
    }
    Code:
    public void m_bNextButton_Clicked(...)
    {
      if (m_currentPage < m_pages.Length - 1) m_currentPage++;
    
      UpdatePanel();
    }
    
    public void m_bBackButton_Clicked(...)
    {
      if (m_currentPage > 0) m_currentPage--;
    
      UpdatePanel();
    }

    Comment

    • Fernand
      New Member
      • Dec 2010
      • 2

      #3
      First of all, thanks for the quick reply,as i read this code i understand what you mean by working with user controls and i went ahead and did a bit of research on what would it be, however i still cant figure out what is that the user control needs, i feel kind of stupid maybe is right here but i really can't figure out what goes in and what goes out, since each of my forms have a next and a back button.

      Here is what i tried, my project starts with frmWelcome, this form doesn't have a next or back button, instead i used a button that starts the chain of Forms designed for the user to input information, other that shows a formulary with a Datagrid in which a Search can be made for information already inserted, this page has as well an add function (into the DB i created in SQL) among other things, and the third button just has the Application.Exi t(); to close the program.

      as i understood the user control will display the next and back buttons in a "background " form and will display the one that has its value set as true, while the others remain hidden, but the buttons on each individual form will be useless and thus eliminated, correct? most likely i didn't understood how was it working or how to implement it in my forms :S, if it is possible to have another example specifying the User control methods to use or a link to where i can see more of this i would appreciate it a lot. Thank you again

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        The idea is that you'll have...

        MainForm - Contains Next and Back buttons, and a panel. The panel will contain the active "page".

        Pages - These are User Controls that contain the information you want each "page" to represent. They also provide public properties to this information.

        So yea, you're correct in that the buttons on each individual form will be eliminated. The forms themselves will be changed to UserControls.

        The important thing here is that you're not using separate forms anymore, you're using UserControls that represent what your forms were, and you're displaying those UserControls in a main form.

        Comment

        Working...