Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiple forms - SDI & MDI Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Multiple forms - SDI & MDI Please use speaker notes for additional information!"— Presentation transcript:

1 Multiple forms - SDI & MDI Please use speaker notes for additional information!

2 MuffinsM1.vbp

3

4 Const cDisc As Currency = 0.95 Dim wrkAmountDue As Currency Option Explicit Private Sub cmdHelp_Click() 'this brings up a form that provides help frmHelp.Show vbModal End Sub Private Sub cmdClear_Click() Dim ctclear As Integer txtQuan.Text = "" txtAmtDue.Text = "" chkDozen.Value = Unchecked Do While ctclear < 5 optMuffin(ctclear).Value = False ctclear = ctclear + 1 Loop End Sub Private Sub cmdExit_Click() End End Sub Private Sub cmdSum_Click() frmSummary.Show vbModal End Sub The summary is shown in vbModal which means the user must act before another form can be accessed. In this case, the user must click Okay. MuffinsM1.vbp This routine clears the information off the form. Notice that I am setting all options to false. I could have chosen to make the first option true to have a default. The Help form is shown in vbModal which means the user must click the Okay. I am defining cDisc as a constant equal to.95. Help defines a constant as “A named item that retains a constant value throughout the execution of a program.”

5 Private Sub cmdCalc_Click() Const ConstNum = "Please enter a number!" Dim wrkPrice As Currency Dim wrkFound As String Dim ct As Integer Dim priceArray As Variant priceArray = Array(".85", ".75", "1.00", ".90", ".90") If Not IsNumeric(txtQuan.Text) Then MsgBox ConstNum, vbOKOnly, "ERROR" Else ct = 0 wrkFound = "F" Do While ct < 5 And wrkFound = "F" If optMuffin(ct).Value = True Then wrkPrice = Val(priceArray(ct)) wrkFound = "T" Else ct = ct + 1 End If Loop If wrkFound = "F" Then MsgBox "Please select muffin", vbOKOnly, "SELECT" End If wrkAmountDue = wrkPrice * Val(txtQuan.Text) If chkDozen.Value = Checked Then wrkAmountDue = wrkAmountDue * cDisc End If txtAmtDue.Text = Format(wrkAmountDue, "Currency") If wrkAmountDue > gHighestSale Then gHighestSale = wrkAmountDue End If gTotal = gTotal + wrkAmountDue wrkAmountDue = 0 gCustomerCount = gCustomerCount + 1 End Sub This sets up an array which contains the prices. This goes through checking to see which muffin option has been checked. When a match is found the corresponding price is taken from the array and the indicator is set to T which ends the loop. If a match is not found, then an error message is displayed. This routine compares the calculated amount to the gHighestSale in the bas module. If it is greater, the current amount replaces the amount in the module. Then the totals are accumulated.

6 'Standards in naming can be helpful, for example public or global variables can start with g 'while a variable that is at the module level only can start with m - note that p can mean 'public - different companies can and will have their own internal standards for variable 'naming Public gTotal As Currency Public gHighestSale As Currency, gCustomerCount As Integer Sub Main() 'The splash screen should be loaded initially and displayed until the user clicks Okay frmSplash.Show End Sub This is the bas module. It shows the splash screen. Note that SubMain is the startup object.

7

8 This is using the Form-Activate event.

9 MuffinM2.vpb

10 Private Sub mnuAbout_Click() Call cmdHelp_Click End Sub Private Sub mnuCalculate_Click() 'You can also enable things using mnuCalculate.Enabled = True 'or make then unenabled with mnuCalculate.Enabled = False. 'Here I am using the checked feature to check calculated because 'I am currently in that routine. I do this by setting Checked = 'True - I also want to uncheck other features so I set Checked = 'False. mnuCalculate.Checked = True mnuSummary.Checked = False mnuClear.Checked = False mnuExit.Checked = False Call cmdCalc_Click End Sub Private Sub mnuClear_Click() mnuCalculate.Checked = False mnuSummary.Checked = False mnuClear.Checked = True mnuExit.Checked = False Call cmdClear_Click End Sub Private Sub mnuExit_Click() End End Sub Private Sub mnuSummary_Click() mnuCalculate.Checked = False mnuSummary.Checked = True mnuClear.Checked = False mnuExit.Checked = False Call cmdSum_Click End Sub

11 Private Sub mnuCalculate_Click() mnuCalculate.Checked = True mnuSummary.Checked = False mnuClear.Checked = False mnuExit.Checked = False Call cmdCalc_Click End Sub MuffinM2.vpb

12 MuffinM2Popup.vbp Visible is unchecked on Popup, however it is checked on all of the menu items that are under popup - calculate, summary, clear etc.

13 Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbRightButton Then PopupMenu Popup End If End Sub If the mouse is down on the form, I check to see if it is the right mouse button, if it is I bring up the PopupMenu which is named Popup. Private Sub popClear_Click() mnuCalculate.Checked = False mnuSummary.Checked = False mnuClear.Checked = True mnuExit.Checked = False Call cmdClear_Click End Sub Private Sub popExit_Click() End End Sub Private Sub mnuSummary_Click() mnuCalculate.Checked = False mnuSummary.Checked = True mnuClear.Checked = False mnuExit.Checked = False Call cmdSum_Click End Sub Private Sub popSummary_Click() mnuCalculate.Checked = False mnuSummary.Checked = True mnuClear.Checked = False mnuExit.Checked = False Call cmdSum_Click End Sub Private Sub popCalculate_Click() mnuCalculate.Checked = True mnuSummary.Checked = False mnuClear.Checked = False mnuExit.Checked = False Call cmdCalc_Click End Sub

14 PRMuffinsMDI.vbp frmHelp and frmSplash are regular forms - note the icon. frmMainMDI is a parent MDI form - note the icon. frmMuffins and frmSummary are child MDI forms - note the icon ModuleM1.bas is a bas module

15 PRMuffinsMDI.vbp

16

17

18 Note that I have both the Muffins form and the Summary form opened within the parent window (Main-MDI) - Main has the menu that we will be using.

19 PRMuffinsMDI.vbp

20

21

22 Private Sub mnuBuy_Click() frmMuffins.Show frmMuffins.SetFocus End Sub Private Sub mnuCascade_Click() frmMainMDI.Arrange vbCascade End Sub Private Sub mnuDesribe_Click() frmHelp.Show End Sub Private Sub mnuExit_Click() End End Sub Private Sub mnuSummary_Click() frmSummary.Show frmSummary.SetFocus End Sub PRMuffinsMDI.vbp Private Sub mnuTileHorizontally_Click() frmMainMDI.Arrange vbTileHorizontal End Sub Private Sub mnuTileVertically_Click() frmMainMDI.Arrange vbTileVertical End Sub

23 Const cDisc As Currency = 0.95 Dim wrkAmountDue As Currency Option Explicit Private Sub cmdCalc_Click() Const ConstNum = "Please enter a number!" Dim wrkPrice As Currency Dim wrkFound As String Dim ct As Integer Dim priceArray As Variant priceArray = Array(".85", ".75", "1.00", ".90", ".90") If Not IsNumeric(txtQuan.Text) Then MsgBox ConstNum, vbOKOnly, "ERROR" Else ct = 0 wrkFound = "F" Do While ct < 5 And wrkFound = "F" If optMuffin(ct).Value = True Then wrkPrice = Val(priceArray(ct)) wrkFound = "T" Else ct = ct + 1 End If Loop If wrkFound = "F" Then MsgBox "Please select muffin", vbOKOnly, "SELECT" End If PRMuffinsMDI.vbp

24 wrkAmountDue = wrkPrice * Val(txtQuan.Text) If chkDozen.Value = Checked Then wrkAmountDue = wrkAmountDue * cDisc End If txtAmtDue.Text = Format(wrkAmountDue, "Currency") If wrkAmountDue > gHighestSale Then gHighestSale = wrkAmountDue End If gTotal = gTotal + wrkAmountDue wrkAmountDue = 0 gCustomerCount = gCustomerCount + 1 txtQuan.SetFocus End Sub Private Sub cmdClear_Click() Dim ctclear As Integer txtQuan.Text = "" txtAmtDue.Text = "" chkDozen.Value = Unchecked Do While ctclear < 5 optMuffin(ctclear).Value = False ctclear = ctclear + 1 Loop End Sub PRMuffinsMDI.vbp

25 Private Sub Form_Activate() Dim wrkAverage As Currency Dim wrkMsgStr As String Dim fmtwrkHighestSale As String, fmtwrkAverage As String txtHighSale = Format$(gHighestSale, "Currency") If gCustomerCount > 0 Then wrkAverage = gTotal / gCustomerCount fmtwrkAverage = Format$(wrkAverage, "Currency") txtAvgSale = fmtwrkAverage Else txtAvgSale = 0 End If End Sub PRMuffinsMDI.vbp The code on the Summary Form is executed when the form is activated. Note there is no exiting of this form. If the user wants to continue the buying of muffins, they should select Buy from the menu on the parent form. Note that the user can also switch forms by clicking on the form by name in the Windows menu option.

26 PRMuffinsMDI1.vbp frmMainMDI is the parent MDI form frmMuffins, frmSummary and frmHelp are Child forms. frmSplash is not part of the MDI group, it stands alone

27 PRMuffinsMDI1.vbp

28

29 Private Sub mnuExit_Click() 'Unload frmHelp 'Unload frmMainMDI 'Unload frmMuffins 'Unload frmSummary Dim AForm As Form For Each AForm In Forms Debug.Print Aform.name Unload AForm Next End End Sub PRMuffinsMDI1.vbp This loop unloads each form which has been named Aform that is in the collection Forms by looping through and unloading them one at a time. When I look at the Intermediate Window, I see the following: frmMainMDI frmMuffins frmSummary frmHelp


Download ppt "Multiple forms - SDI & MDI Please use speaker notes for additional information!"

Similar presentations


Ads by Google