Results 1 to 11 of 11

Thread: Noob: checkbox, function, module assistance

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2013
    Location
    SF Bay Area
    Posts
    50

    Noob: checkbox, function, module assistance

    I'm in the chapter in my Visual Basic book on creating multiple forms, modules, and menus.

    The first problem, is to create a problem that calculates a total for going to conference.

    it gives MainForm and OptionsForm design. Below is what I recreated
    Name:  MainForm..jpg
Views: 1701
Size:  33.9 KBName:  OptionsForm.jpg
Views: 1628
Size:  38.2 KB

    The assignment
    The Conference Options form allows the user to select the regular conference registration, the optional opening night dinner, and an optional preconference workshop; user cannot select optional events without selecting registration. When the Close button is clicked, this form should be removed from the screen and the total registration fee should appear on the main form
    It also gives a tip about using a global variable to hold the total cost, so both form will have access to the variable

    I haven't finished the program and most I can do. I have module setup in addition to the two forms.

    this is button on the MainForm to get conference info and total
    Code:
     Private Sub btnConferenceOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConferenceOptions.Click
            '==================================
            'declare variables
            '=================================
            Dim frmOptionsForm As New frmOptionsForm
    
            frmOptionsForm.ShowDialog()
    
            '==================================
            'display total
            '=================================
    
            lblTotal.Text = g_decTotalCost.ToString("c")
        End Sub
    this is the module
    Code:
    Module CalucateModule
        '==================================
        'declare global constants and variables
        '==================================
        Public Const g_decRegistration As Decimal = 895
        Public Const g_decDinner As Decimal = 30
        Public Const g_decECommerce As Decimal = 295
        Public Const g_decWeb As Decimal = 295
        Public Const g_decVisualBasic As Decimal = 395
        Public Const g_decNetworkSecurity As Decimal = 395
        Public g_decTotalCost As Decimal
    
        Public Function ConferenceTotal1() As Decimal
    
            If frmOptionsForm.chkRegistration.Checked = True Then
                g_decTotalCost += g_decRegistration
                MessageBox.Show("test if IfThen statement")
            End If
            MessageBox.Show("test outside IfThen statement")
    
            Return g_decTotalCost
        End Function
    End Module
    this is the OptionsForm code
    Code:
    Public Class frmOptionsForm
    
        Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Call ConferenceTotal()
    
            Me.Close()
        End Sub
    
        Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
            chkDinner.Checked = False
            chkRegistration.Checked = False
            lstOptions.SelectedIndex = -1
        End Sub
    
        Public Function ConferenceTotal() As Decimal
    
            If chkRegistration.Checked = True Then
                g_decTotalCost += g_decRegistration
            End If
    
            Return g_decTotalCost
        End Function 
    End Class
    What I can't figure out, ConferenceTotal function works on the OptionsForm, but not on the CalculateModule. The I added the 1 to ConferenceTotal so I didn't get some error, and didn't want to delete and retype later. Somehow I have something off when referencing the chkRegistration on the OptionsForm. The outside IfThen statement messagebox pops up when I call CalcuateTotal1 function.

    It's possible I'm not fully understand module and what can be used in it. The books says they're meant to contain general purpose procedures, functions, and declarations that are available to all forms in a project. So, maybe I'm not allowed to use radio buttons, checkboxes, lists and so forth in specific forms.

    Once I understand this, I can do the rest.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Noob: checkbox, function, module assistance

    So, maybe I'm not allowed to use radio buttons, checkboxes, lists and so forth in specific forms.
    Correct. However you can pass their values. So you could, for example declare ...

    Public Function ConferenceTotal1(ByVal RChecked As Boolean) As Decimal
    If RChecked Then

    etc.

    ... and when you call the function ..

    d = ConferenceTotal1(frmOptionsForm.chkRegistration.Checked) or more likely d = ConferenceTotal1(Me.chkRegistration.Checked)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Noob: checkbox, function, module assistance

    Although using default instances in VB.net is not really a good idea, it's bad practice.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2013
    Location
    SF Bay Area
    Posts
    50

    Re: Noob: more assistance in learning modules

    Apparently I'm still missing something in regards to modules. It's different programming challenging in the book. Basically, there are three forms, MainForm, IndividualForm, FamilyForm. The main form just have three buttons--Family, Individual, and Exit. On the Family and Individual, you pick radio buttons options and check boxes, hit calculate and it comes up with totals and display them. The Individual and Family look the same except one has a text box for entering number of phones, where the other does not

    I have all the calculations being done, and correctly. Have the Module calculating as must of the calculations since it's the same. My issue is the output. I've created procedure to display the output, but it's not displaying.

    this the IndividualForm ( I left out some code)
    Code:
    decPhoneCharge = PhoneCharge(decNumberPhones, decPhonePrice)
            decTax = TaxCalc(decPhoneCharge)
            decPhoneTotal = PhoneTotalCalc(decPhoneCharge, decTax)
            decMonthlyTotal = TotalCalc(decPhoneTotal, decOptionsTotal, decMonthlyRate)
    
            Call DisplayOutput(decPhoneCharge, decTax, decPhoneTotal, decOptionsTotal, decMonthlyRate, decMonthlyTotal)
    this is my display procedure
    Code:
        Public Sub DisplayOutput(ByVal decPhoneCharge As Decimal, ByVal decTax As Decimal, ByVal decPhoneTotal As Decimal, ByVal decOptionsTotal As Decimal, ByVal decMonthlyRate As Decimal, ByVal decMonthlyTotal As Decimal)
            With frmIndividaulForm
                .lblSubtotal.Text = decPhoneCharge.ToString("c")
                .lblTax.Text = decTax.ToString("c")
                .lblPhoneTotal.Text = decPhoneTotal.ToString("c")
                .lblOptions.Text = decOptionsTotal.ToString("c")
                .lblPhoneCharge.Text = decMonthlyRate.ToString("c")
                .lblTotalCharge.Text = decMonthlyTotal.ToString("c")
            End With
        End Sub
    If have the display code on the IndividualForm, the display in the labels is changed/shown. I far as I can figure, the values for the variables is being passed to through the procedure.


    On a different problem, I was able to use display procedure and it worked

    Code:
            End Select
    
            Call Display()
        End Sub
    Code:
        Public Sub Display()
            Dim decShippingCost As Decimal
            Dim decTax As Decimal
            Dim decTotal As Decimal
    
            decTax = TaxCalc(g_decSubtotal)
            decShippingCost = ShippingCost(frmMainForm.lstSelectedProducts.Items.Count)
            decTotal = TotalCalc(g_decSubtotal, decTax, decShippingCost)
    
            frmMainForm.lblSubtotal.Text = g_decSubtotal.ToString("c")
            frmMainForm.lblTax.Text = decTax.ToString("c")
            frmMainForm.lblShipping.Text = decShippingCost.ToString("c")
            frmMainForm.lblTotal.Text = decTotal.ToString("c")
        End Sub
    On a side question, but related, is there some kind of wildcard I use for items that are named the same, but just on different forms, like [wildcard].lblSubtotal.Text = SomeVariable.ToString() ? In both the problems discussed in this point, each had two forms, with the exact same layout and controls and control names.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2013
    Location
    SF Bay Area
    Posts
    50

    Noob: referring to a different window

    I need some more clarification. I think this may be similar to the issue I was having above with the module.

    This is different programming challenge. Basically below, need to calculate tax on the skateboard being ordered. There are separate forms for decks, trucks, wheels, and misc. I have global variable for subtotal (decSubtotal). There is only one item that is not taxable (Assembly). My thinking, if the user has selected the assembly item (which is selected by a CheckBox), to just subtract it from the a variable, in this case, decTaxableAmount, which is made to equal decSubtotal before checking if Assembly is selected.

    The TaxCalc function is on the MainForm. In my function, I'm asking it to check the MiscForm to see if Assembly has been selected. From that I can cell, it's not checking it. Are you not allowed to check to see if a CheckBox, RadioButton, ComboBox item, and so forth have been selected in a different form? I know I can add items to a ListBox or the change the text property to a label that are on different forms.

    Code:
    Public Class frmMainForm
    code removed
    
        Function TaxCalc() As Decimal
            Dim decTaxAmount As Decimal
            Dim decTaxableAmount As Decimal
    
            decTaxableAmount = decSubtotal
    
            If frmMiscForm.chkAssembly.Checked = True Then
                decTaxableAmount -= decASSEMBLY
    
            End If
    
            decTaxAmount = decTaxableAmount * decTAX_RATE
    
            Return decTaxAmount
        End Function
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim decTaxAmount As Decimal = 0
            Dim decTotal As Decimal = 0
    
            decTaxAmount = TaxCalc()
            decTotal = decSubtotal + decTaxAmount
    
    code removed
    
        End Sub
    
    code removed
    
        Private Sub btnMiscChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMiscChoose.Click
            Dim frmMiscForm As New frmMiscForm
    
            frmMiscForm.ShowDialog()
    
        End Sub
    [code removed]
    End Class

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Noob: checkbox, function, module assistance

    There are separate forms for decks, trucks, wheels, and misc.
    This seems a bit unwieldy to me. Does it really take a completely new form for each of these? Multiform applications are a rather old-fashioned way of programming anyway but it's become something of a minefield since MS dulled the distinction between default and instanced forms. I would strongly advise you to consider how it might be possible to reduce your application to a single form (with dialogs if necessary) whenevr the opportunity arises.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2013
    Location
    SF Bay Area
    Posts
    50

    Re: Noob: checkbox, function, module assistance

    Not that I don't agree. I'm just doing the programming challenging that was in the book I'm learning from. The assignment
    Quote Originally Posted by Starting Out With Visual Basic 2010
    The Skate Shop sells the following skateboard products
    ...

    Create an application that allows the user to select one deck from a form, one truck assembly from a form, and one wheel set from a form. The application should also have a form that allows the user to select any miscellaneous product, using check boxes. The application should display the subtotal, the amount of sales tax (at 6%), and the total of the order. Do not apply sales to to assembly
    The way I read it, needs that many forms. Personally, this could have all been done on one form. This particular chapter is on creating multiple forms, modules and menus. Thus, the authors were probably trying to keep the applications simple, but still have the read practice the ideas. It's a lot easier to learn with simple applications than large and complicated.

    Back to my question, is it possible to reference the CheckBox in another form, like I'm trying with my TaxCalc function?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Noob: checkbox, function, module assistance

    Quote Originally Posted by dunfiddlin View Post
    This seems a bit unwieldy to me. Does it really take a completely new form for each of these? Multiform applications are a rather old-fashioned way of programming anyway but it's become something of a minefield since MS dulled the distinction between default and instanced forms. I would strongly advise you to consider how it might be possible to reduce your application to a single form (with dialogs if necessary) whenevr the opportunity arises.
    Dialogues are forms anyway. The original code shows that ShowDialog is being called so that form at least is being displayed as a modal dialogue. It's still a form though. Anyway, learning examples are almost always contrived because they want to highlight a specific area of interest.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2013
    Location
    SF Bay Area
    Posts
    50

    Re: Noob: checkbox, function, module assistance

    this post is just a holding spot for the application. Still haven't figured out why or how to get the DisplayOutput Function to work with the FamilyForm or IndividualForm.
    Attached Files Attached Files

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Noob: checkbox, function, module assistance

    As I suspected, the problem is that you are feeding the values to an instance of a form that doesn't actually exist in your application at the time. To do what you want in the module requires not only that you pass values to the Sub for the .. er .. values but also for the controls which are to receive the values. That makes for one very ugly declaration to say the least so you really should take the easy option and delete the Display Output Sub from the module and append a simpler version of it to each of the two dialog forms' code.

    vb.net Code:
    1. Public Sub DisplayOutput(ByVal decPhoneCharge As Decimal, ByVal decTax As Decimal, ByVal decPhoneTotal As Decimal, ByVal decOptionsTotal As Decimal, ByVal decMonthlyRate As Decimal, ByVal decMonthlyTotal As Decimal)
    2.         lblSubtotal.Text = decPhoneCharge.ToString("c")
    3.         lblTax.Text = decTax.ToString("c")
    4.         lblPhoneTotal.Text = decPhoneTotal.ToString("c")
    5.         lblOptions.Text = decOptionsTotal.ToString("c")
    6.         lblPhoneCharge.Text = decMonthlyRate.ToString("c")
    7.         lblTotalCharge.Text = decMonthlyTotal.ToString("c")
    8.     End Sub

    Works a treat!

    On a stylistic note, I would arrange for the dialogs to cover the launch form (or simply hide the launch form) when they are visible, and on a personal note, I really hate 'confirm exit' dialogs!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2013
    Location
    SF Bay Area
    Posts
    50

    Re: Noob: checkbox, function, module assistance

    Thanks for the reply. I was really expecting one. I just needed a spot to upload the program because Gmail won't let one send or receive a zip file with a executable file in it.

    I knew I could get the values shown in the label if I just moved the display to the appropriate Form. Thus, I knew my values were calculated. It was more trying to expand and challenge myself, and hopefully understand more on using multiple forms and modules.

    From what you describe, I think I do the same mistake in regards to modules and forms. So, if I'm understanding you correctly, to do what I'm trying would have to repass the values for labels back to the appropriate form? I assuming that the labels for the calculations are created and displayed on the form with empty strings creates the instance. Although it's possible I'm not fully understanding what's creating an instance.


    As for the side note, I don't disagree at all. It was just trying to reinforce how to do ClosingForm and CloseForm events. I'm still really green, so not remembering how to do something happens more often than not. I have look up ControlChars.CrLf to add mutliple lines in a message (I always forget the ControlChars part) or how set colors to system default. I just don't use them enough.

    On a side note, I'll try experimenting with hiding forms. I simply don't know how to set locations of windows on the screen very well. And when I tried it, I didn't care for results. It worked, just looked clumsy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width