Question
Menu Multiform - This needs to be done in Visual Basic
***I have the forms built I just need help with the codes. I've been working on it for days and can't get it to work. Thanks in Advanced for the help. *******
Create a menu type form that provides the user the capability to select one of the following two subforms. Means of selection is programmers choice.
The second form titled Month shall provide a checked list box that displays the contents of a read in months.txt file containing the names of the 12 calendar months. The user must select at least 3 of the listed months. A menu bar shall be provided that contains a File item. Under the File menu bar item shall be the selections to Save, Clear, and Exit. Selecting Save shall result in display of the selected months in a multiline list box on the form and the writing of the selected months to a text file named selected_months.txt. Selecting Clear clears the selections and provides for new entries. Selecting Exit returns the user to the top menu.
Explanation / Answer
It is very common to have a Windows application with mulitple Forms. Additional Forms are used for various reasons. For example, a splash form may be displayed while the main form is being loaded into the memory. A Form may also be used to display the instructions about a program, or to display the program's output. Each additional Form has its own Graphical User Interface (GUI) as well as its own Code Window. The Form object has methods that facilitate moving from one Form to another during program execution. It is easy to add additional Forms to a VB.Net project. In the following sections we explain how to add additional forms to a project, and how to navigate between them. It is always a good idea to save your work every few minutes. Selecting the option to Save All will save all Forms, Code and properties of the current open project to disk. To Save all the work you have done so far select: File ==> Save All from the Visual Studio File Menu tool bar as indicated in the following diagram: File Save All Figure MFP-1: Visual Basic File Save All When the program is running, we can display the different forms by using the Visual Basice Form Methods(). These methods are called out at the time we want to display a different Form. The next section describes some of these methods and how to use them. Visual Basic Form Object Methods The Visual Basic Form is created in Visual Studio as a class. A class is a programming construct that contains both data (properties such as height and width of form) as well as methods (actions such as hide or display). When we double-click on a blank area of a Visual Basic Form, the code window associated with that form is displayed in our Visual Studio IDE. If you double-click on a blank area of the Form2 object that we just created, Visual studio brings up the following Code Window: Form Code Window Figure MFP-2: Visual Basic Form Code Window This is all of the code associated with our Form object before we have added any controls, events or procedures to the form: Public Class Form2: Beginning of code. Public: The Public Keyword means the form itself is accessible to all other forms. Class: The Form is a type of a programming structure called a Class. Form2: The name property of this form. Private Sub Form2_Load: Sub called out when the Form2 is called from another part of the program. Private: Variables and Data in this Sub are only accessible from this code window. Sub: A series of actions that returns no data or information to the calling routine. Form2_Load: This Sub is called by the Load Event in another Form. End Sub: Last line of Sub. Control is returned to Visual Basic. End Class: Last Line of Code associated with this form. Visual Basic Show( ) Method The Show() method is used to display a Form on the screen as a Modeless Form. A modeless Form can be displayed and accessed at the same time other Forms are displayed. A modeless form does not have to be closed before working on another Form in the program. Example: Form2.Show( ) Visual Basic ShowDialog( ) Method The ShowDialog() method is used to display a Form on the screen as a Modal Form. A Modal Form can only be accessed when it is the only active Form on the screen. In order to access another Form, the Modal Form must be closed. Example: Form2.ShowDialog( ) Visual Basic Form Close( ) Method The Visual Basic Close method is used to remove a From from the computer display, as well as removing the Form from memory, where all data on the Form will be lost. Example: Me.Close( ) Visual Basic Form Accept Button( ) Method The Visual Basic Accept Button method is used to program a button as a response for when the user presses the key. This feature helps makes forms more user friendly by providing an action when the user presses the key, such as when they are finished entering data on the form. Example: Form2.AcceptButton( )=btnAcceptButton Visual Basic Multiform Project Example Have you ever had a pet named by a previous owner that just didn't seem to fit? Well, now that you are seasoned VB.Net programmers with Multiform Project knowledge and experience, you now possess the tools to rectify that situation, and choose the best name for your little munchkin. Let's see how to do just that. Create a new project in Visual Studio. Change the following properties on the default form: Name: MainForm Text: Main Form As indicated in the diagram below: Form Properties Figure MFP-3: Visual Basic Multiform Project Main Form Properties From the Common Controls section of the Visual Studio ToolBox. Add the following controls and make the necessary changes to each of the control's properties as indicated below: Common Controls: Button: Name: btnChangeName Text: Change Name Label: Name: lblPetName Text: Name: Howie Picture Box: Name: MyDogPicture And as indicated in the diagram below: Form 1 Controls Figure MFP-4: Visual Basic Multiform Project Main Form Controls When you are finished placing your controls on MainForm the result could look something like the diagram below: Main Form Design View Figure MFP-5: Visual Basic Mulitform Project Main Form Design View The next step in this multiform project example is to add the second form to our project. The following steps detail how to perform the necessary tasks: Adding a Form to a VB.NET Project To add a second form to your project. Select Project ==> Add Windows Form... from the Visual Studio File Menu as indicated in the diagram below: alt='New Form'> Figure MFP-6: Visual Basic Select a new project Form Selecting the Add Windows Form option will bring up the following dialog window: Add New Item Figure MFP-7: Visual Basic Add New Form The following 5 steps are required to add a Form to your project: Add New Item. Select Windows Forms Category: This is found under the heading Categories. Select Windows Form: This is one of the Visual Studio Installed Template Options. Give the Form a Name: This would be any name that you (the programemer) find descriptive. Select Add Button: This will put the form with the selected characteristics into your project. Change the Form properties as indicated in the diagram below: Form 2 Properties Figure MFP-8: Visual Basic frmChangeName New Properties The properties requiring changes on the newly added Form are: (Name): frmChangeName Text: Change Name Add the following controls to frmChangeName as indicated in the diagram below: frmChangeName Controls Figure MFP-9: Visual Basic frmChangeName Controls added from ToolBox Change the properties of the controls as indicated below: Button: (Name): btnAccept Text: Accept New Name Label: Text: Your dog's new name is: TextBox: (Name): txtNewName After changing the appropriate properties and arranging the controls on the form, it may look similar to: Form 2 Design View Figure MFP-10: Visual Basic frmChangeName Design View Select the Form1.vb Screen tab located at the top of the design window, or double-click in a blank area of Form 1 to bring up the Form1 Code View Window. Enter the following code: Public Class MainForm Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load MyDogPicture.Height = 150 MyDogPicture.Width = 138 MyDogPicture.ImageLocation = "http://centurion2.com/HTML/Tutorial/Dog3.JPG" End Sub Private Sub btnChangeName_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnChangeName.Click frmChangeName.Show() End Sub End Class Figure MFP-11: Visual Basic Source Code for frmMainForm Select the Form2.vb Screen tab located at the top of the design window, or double-click in a blank area of Form 2 to bring up the Form2 Code View Window. Enter the following code: Public Class frmChangeName Private Sub Form2_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.Left = MainForm.Left + MainForm.Width + 50 Me.Top = MainForm.Top Me.Height = MainForm.Height Me.AcceptButton = btnAccept Me.ActiveControl = txtNewName End Sub Private Sub btnAccept_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnAccept.Click Dim NewName As String NewName = "Name: " + txtNewName.Text MainForm.lblPetName.Text = NewName txtNewName.Clear() txtNewName.Focus() End Sub End Class Figure MFP-12: Visual Basic Source Code for frmChangeName Although the code on these 2 forms isn't very long, there are some very handy tools, that will be beneficial to incorporate in future programs. We'll go through this code right now, and determine what to expect: Note: Since following examples download content off the Internet, Centurion is not responsible for any malicious software downloaded by users of this site. The user needs to have the proper security protocols in place before attempting any downloads from the Internet. Private Sub Form1_Load: Since this is the main form, the following events will be executed at program startup. MyDogPicture.Height = 150: This is the height of the picture we are going to download off the site. MyDogPicture.Width = 138: This is the width of the picture we are going to download off the site. MyDogPicture.ImageLocation = "http://centurion2.com/HTML/Tutorial/Dog3.JPG": MyDogPicture: This is the name of the Visual Basic Picture Control that will receive the image file. .ImageLocation: This is where Visual Basic needs to look for the file. Note - This can be a web address or a local file on your own computer system. "http://centurion2.com/HTML/Tutorial/Dog3.JPG": This is the URL on the Centurion Web Site that contains the image. End Sub: End of actions performed when program first executes. Private Sub btnChangeName_Click: Sub executed when Change Name button is pressed. frmChangeName.Show(): Show the ChangeName Form with modeless attributes. End Sub: No further action is response to Change Name button. The code associated with Form Change Name is described as follows: Public Class frmChangeName: The code in this window deals with the Change Name form. Private Sub Form2_Load: The following actions will occur immediately after the Change Name Form is displayed: Me.Left=MainForm.Left+MainForm.Width+50: This is to line up the Change Name Form so that both forms are visible. Me.Left: The Left side of the Change Name Form will be located as follows: MainForm.Left+MainForm.Width: Calculation for MainForm.Right. +50: Some arbitrary amount so there is a gap between the two forms. Me.Top=MainForm.Top: Makes the 2 forms parallel with each other. Me.Height=MainForm.Height: Makes the 2 forms about the same size. Me.AcceptButton=btnAccept: Makes the Accept Button respond to the key just like a Mouse Click Me.ActiveControl=txtNewName: Places the cursor in the NewName TextBox. End Sub: End of actions performed when ChangeName Form is displayed. Private Sub btnAccept_Click: Procedure performed when Accept Button is pressed or whenever key is pressed. Dim NewName As String: Container for New Name. NewName="Name: " + txtNewName.Text: Creating New Name from data in TextBox. MainForm.lblPetName.Text=NewName: Transfer NewName to label on MainForm. MainForm: Indicates the recipient of the data transfer is on a different Form. .lblPetName:Control to receive data. .Text: Attribute to be modified. =NewName: The data that is transferred to the Label Control on the MainForm. txtNewName.Clear(): Clear the previous User Input. txtNewName.Focus(): Keep the Focus on the TextBox. Well let's give it a whirl, and see if it performs up to expectations. Press the and see if we get some correlation: Ex1 @ Runtime 1 Figure MFP-13: Visual Basic Multiform Project - Example 1 @ Runtime Press the Change Name button, to bring up the second form: Ex1 @ Runtime 2 Figure MFP-14: Visual Basic Multiform Project - Example 1 @ Runtime - #2 Enter the new name you deem appropriate, I chose 'Fang', in the TextBox and press instead of the Accept Button. You should see the corresponding name change on the MainForm as indicated in the diagram below: Ex1 @ Runtime 3 Figure MFP-15: Visual Basic Multiform Project - Example 1 @ Runtime - #3