473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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"buttons 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.
Dec 3 '10 #1
3 7555
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. 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...

Expand|Select|Wrap|Line Numbers
  1. public class Page1 : UserControl { ... }
  2. public class Page2 : UserControl { ... }
  3. public class Page3 : UserControl { ... }
Expand|Select|Wrap|Line Numbers
  1. public class MainForm : Form
  2. {
  3.   private UserControl[] m_pages = new UserControl[3];
  4.   private int m_currentPage = 0;
  5.  
  6.   public MainForm()
  7.   {
  8.     m_pages[0] = new Page1();
  9.     m_pages[1] = new Page2();
  10.     m_pages[2] = new Page3();
  11.  
  12.     UpdatePanel();
  13.   }
  14.  
  15.   public void UpdatePanel()
  16.   {
  17.     m_pnlCurrentPage.Controls.Clear();
  18.     m_pnlCurrentPage.Controls.Add(m_pages[m_currentPage]);
  19.   }
  20. }
Expand|Select|Wrap|Line Numbers
  1. public void m_bNextButton_Clicked(...)
  2. {
  3.   if (m_currentPage < m_pages.Length - 1) m_currentPage++;
  4.  
  5.   UpdatePanel();
  6. }
  7.  
  8. public void m_bBackButton_Clicked(...)
  9. {
  10.   if (m_currentPage > 0) m_currentPage--;
  11.  
  12.   UpdatePanel();
  13. }
Dec 3 '10 #2
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.Exit(); 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
Dec 4 '10 #3
GaryTexmo
1,501 Expert 1GB
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.
Dec 4 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Brian Keating EI9FXB | last post by:
Would I be correct in saying that the only way to get a user message into a Windows form would be to use P/Invoke with Message? Of is there some part of the .NET API that I am totally un aware...
4
by: Michael C# | last post by:
I have a Windows Form I want to add to another form similar to the way you add an OpenFileDialog to a Windows Form from the Toolbox. Thx
1
by: Martin Carolan | last post by:
Hi there, I'm having a bit of trouble figuring out how to do this, help would be appreachiated: I have a windows form inside an exe called frmWizard, it is just a wizard interface with some...
5
by: fernandez.dan | last post by:
Hi My question is how to change the view of my windows form. What I want to do is in my Windows Form is first ask the user some questions, when the user is done she pushes a button called next....
2
by: Petez | last post by:
Hi, I want to declare array of System::Data::DataRow but how? With single variable I write System::Data::DataRow^ aRow but statement System::Data::DataRow^ - not works. System::Data::DataRow* ...
8
by: Alison | last post by:
Hi, Al I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window...
3
by: Nina | last post by:
Hi there, Is there anyone knows how to make windows form datagrid that is bound to an array editable? How to catch when a cell has been edited? Thank you for your help. Nina
1
by: Melson | last post by:
Hi I've realised that whenever I declare this sentence, "dim newform as new mainform" in another form, the Public Sub New() in Windows Form Designer generated code in mainform will automatically...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
2
by: deccio | last post by:
I have create an activex Control with Visual studio 2005 and framework 2.0 in c# to add drag & drop functionality to upload multi file. When I use it in a windows form it work fine. Infact if I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.