11 Replies - 1936 Views - Last Post: 04 November 2011 - 08:45 PM Rate Topic: -----

#1 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Draw string in a PrintDocument object

Posted 03 November 2011 - 07:55 PM

Hi there,

I threw this together for a bake sale that me and my friends do to raise money. This is a scratchy rough copy (Published as Version 1.1.0 because there was a huge logic error in 1.0.0), and I was wondering if anyone could give me pointers on how to make the code better. The heart of the program is here. I don't have the code for the SplashScreen, AboutBox, and the InstructionsForm because that is unnecessary. The main issue that I have is with the large EndSalesButton_Click event. The big block there is driving my eyes and brain crazy while trying to test the logic. So, to sum up everything, can anyone make that look nicer and easier on the mind? If you would like to run my program to better understand how it works, feel free to contact me at either of the two emails in the beginning declarations (lines 2 and 3)of the code below.

Thank You Very Much!

'Name: Douggie
'Email 1: <removed>
'Email 2: <removed>
'Date: 10/30/11
'Project: BATT Sales Tracker
'Made With: Visual Basic 2008
'
Public Class MainForm
    'setting the variables for the (....TrackerTextBox) and the End Sales

    Private a As Integer = 0
    Private b As Integer = 0
    Private c As Integer = 0
    Private d As Integer = 0
    Private f As Integer = 0
    Private g As Integer = 0
    Private h As Integer = 0

    Private Sub DoughnutsCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DoughnutsCheckBox.CheckedChanged
        'Determines Visibility based on DoughnutsCheckBox

        With DoughnutsCheckBox.Checked
            DoughnutsQTYNumericUpDown.Enabled = True
            DoughnutsCostTextBox.Enabled = True
            DoughnutsPriceTextBox.Enabled = True
        End With

        If DoughnutsCheckBox.CheckState = CheckState.Unchecked Then
            DoughnutsQTYNumericUpDown.Enabled = False
            DoughnutsCostTextBox.Enabled = False
            DoughnutsPriceTextBox.Enabled = False
        End If
    End Sub

    Private Sub CoffeeCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CoffeeCheckBox.CheckedChanged
        'Determines Visibility based on CoffeeCheckBox

        With CoffeeCheckBox.Checked
            CoffeeQTYNumericUpDown.Enabled = True
            CoffeeCostTextBox.Enabled = True
            CoffeePriceTextBox.Enabled = True
        End With

        If CoffeeCheckBox.CheckState = CheckState.Unchecked Then
            CoffeeQTYNumericUpDown.Enabled = False
            CoffeeCostTextBox.Enabled = False
            CoffeePriceTextBox.Enabled = False
        End If
    End Sub

    Private Sub FlavoringCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlavoringCheckBox.CheckedChanged
        'Determines Visibility based on FlavoringCheckBox

        With FlavoringCheckBox.Checked
            FlavorQTYNumericUpDown.Enabled = True
            FlavorCostTextBox.Enabled = True
            FlavorPriceTextBox.Enabled = True
        End With

        If FlavoringCheckBox.CheckState = CheckState.Unchecked Then
            FlavorQTYNumericUpDown.Enabled = False
            FlavorCostTextBox.Enabled = False
            FlavorPriceTextBox.Enabled = False
        End If
    End Sub

    Private Sub BananasCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BananasCheckBox.CheckedChanged
        'Determines Visibility based on the BananasCheckBox

        With BananasCheckBox.Checked
            BananasQTYNumericUpDown.Enabled = True
            BananasCostTextBox.Enabled = True
            BananasPriceTextBox.Enabled = True
        End With

        If BananasCheckBox.CheckState = CheckState.Unchecked Then
            BananasQTYNumericUpDown.Enabled = False
            BananasCostTextBox.Enabled = False
            BananasPriceTextBox.Enabled = False
        End If
    End Sub

    Private Sub OrangesCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrangesCheckBox.CheckedChanged
        'Determines Visibility based on the OrangesCheckBox

        With OrangesCheckBox.Checked
            OrangesQTYNumericUpDown.Enabled = True
            OrangesCostTextBox.Enabled = True
            OrangesPriceTextBox.Enabled = True
        End With

        If OrangesCheckBox.CheckState = CheckState.Unchecked Then
            OrangesQTYNumericUpDown.Enabled = False
            OrangesCostTextBox.Enabled = False
            OrangesPriceTextBox.Enabled = False
        End If
    End Sub

    Private Sub HotChocolateCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HotChocolateCheckBox.CheckedChanged
        'Determines Visibility based on the HotChocolateCheckBox

        With HotChocolateCheckBox.Checked
            HotChocolateQTYNumericUpDown.Enabled = True
            HotChocolateCostTextBox.Enabled = True
            HotChocolatePriceTextBox.Enabled = True
        End With

        If HotChocolateCheckBox.CheckState = CheckState.Unchecked Then
            HotChocolateQTYNumericUpDown.Enabled = False
            HotChocolateCostTextBox.Enabled = False
            HotChocolatePriceTextBox.Enabled = False
        End If
    End Sub

    Private Sub OtherItemCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OtherItemCheckBox.CheckedChanged
        'Determines Visibility based on the OtherItemCheckBox

        With OtherItemCheckBox.Checked
            OtherItemQTYNumericUpDown.Enabled = True
            OtherItemPriceTextBox.Enabled = True
            OtherItemCostTextBox.Enabled = True
            DefineOtherItemLabel.Enabled = True
            DefineOtherItemTextBox.Enabled = True
        End With

        If OtherItemCheckBox.CheckState = CheckState.Unchecked Then
            OtherItemQTYNumericUpDown.Enabled = False
            OtherItemPriceTextBox.Enabled = False
            OtherItemCostTextBox.Enabled = False
            DefineOtherItemLabel.Enabled = False
            DefineOtherItemTextBox.Enabled = False
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'Closes the program + DialogResult

        Dim toasty As DialogResult
        toasty = MessageBox.Show("Do you really want to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk)
        If toasty = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub StartSalesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartSalesButton.Click
        'Disables the InitialSettingsGroupBox and Enables both the SalesTrackerGroupBox and the
        'AddASaleGroupBox, as well as the DocumentPreviewRichTextBox with above Label8. This 
        'alos determines the enabled properties of all the buttons.

        InitialSettingsGroupBox.Enabled = False
        AddASaleGroupBox.Enabled = True
        SalesTrackerGroupBox.Enabled = True
        Label8.Enabled = True
        DocumentPreviewRichTextBox.Enabled = True

        'Is the SellADoughnutButton enabled?
        If DoughnutsCheckBox.CheckState = CheckState.Unchecked Then
            SellADoughnutButton.Enabled = False
        ElseIf DoughnutsCheckBox.CheckState = CheckState.Checked Then
            SellADoughnutButton.Enabled = True
        End If

        'Is the SellACoffeeButton enabled?
        If CoffeeCheckBox.CheckState = CheckState.Unchecked Then
            SellACoffeeButton.Enabled = False
        ElseIf CoffeeCheckBox.CheckState = CheckState.Checked Then
            SellACoffeeButton.Enabled = True
        End If

        'Is the SellAFlavoringButton enabled?
        If FlavoringCheckBox.CheckState = CheckState.Unchecked Then
            SellAFlavoringButton.Enabled = False
        ElseIf FlavoringCheckBox.CheckState = CheckState.Checked Then
            SellAFlavoringButton.Enabled = True
        End If

        'Is the SellABananaButton enabled?
        If BananasCheckBox.CheckState = CheckState.Unchecked Then
            SellABananaButton.Enabled = False
        ElseIf BananasCheckBox.CheckState = CheckState.Checked Then
            SellABananaButton.Enabled = True
        End If

        'Is the SellAOrangeButton enabled?
        If OrangesCheckBox.CheckState = CheckState.Unchecked Then
            SellAOrangeButton.Enabled = False
        ElseIf OrangesCheckBox.CheckState = CheckState.Checked Then
            SellAOrangeButton.Enabled = True
        End If

        'Is the SellHotChocolateButton enabled?
        If HotChocolateCheckBox.CheckState = CheckState.Unchecked Then
            SellHotChocolateButton.Enabled = False
        ElseIf HotChocolateCheckBox.CheckState = CheckState.Checked Then
            SellHotChocolateButton.Enabled = True
        End If

        'Is the SellOtherItemButton enabled?
        If OtherItemCheckBox.CheckState = CheckState.Unchecked Then
            SellOtherItemButton.Enabled = False
        ElseIf OtherItemCheckBox.CheckState = CheckState.Checked Then
            SellOtherItemButton.Enabled = True
        End If
    End Sub

    Private Sub SellADoughnutButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SellADoughnutButton.Click
        'Math to calculate the value in the text box

        SellADoughnutButton.Enabled = True
        a = a + 1
        DoughnutsTrackerTextBox.Text = a
    End Sub

    Private Sub SellACoffeeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SellACoffeeButton.Click
        'Math to calculate the value in the text box

        b = b + 1
        CoffeeTrackerTextBox.Text = b
    End Sub

    Private Sub SellAFlavoringButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SellAFlavoringButton.Click
        'Math to calculate the value in the text box

        c = c + 1
        FlavoringTrackerTextBox.Text = c
    End Sub

    Private Sub SellABananaButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SellABananaButton.Click
        'Math to calculate the value in the text box

        d = d + 1
        BananasTrackerTextBox.Text = d
    End Sub

    Private Sub SellAOrangeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SellAOrangeButton.Click
        'Math to calculate the value in the text box

        f = f + 1
        OrangesTrackerTextBox.Text = f
    End Sub

    Private Sub SellHotChocolateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SellHotChocolateButton.Click
        'Math to calculate the value in the text box

        g = g + 1
        HotChocolateTrackerTextBox.Text = g
    End Sub

    Private Sub SellOtherItemButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SellOtherItemButton.Click
        'Math to calculate the value in the text box

        h = h + 1
        OtherItemTrackerTextBox.Text = h
    End Sub

    Private Sub InstructionsToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstructionsToolStripMenuItem1.Click
        'Opens the instructions

        InstructionsForm.Show()
    End Sub

    Private Sub PrintToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem1.Click
        'Prints the form

        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.PrinterSettings.DefaultPageSettings.Landscape = True
        PrintForm1.Print()
    End Sub

    Private Sub SaveDocumentToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveDocumentToolStripMenuItem.Click
        'Asks the user to check the contents of the DocumentPreviewRichTextBox before continuing on.
        'Create the new file

        Dim SaveFile As New SaveFileDialog
        SaveFile.Filter = "RichTextFormat|*.rtf"
        SaveFile.ShowDialog()
        If SaveFile.FileName = "" Then
        Else
            System.IO.File.WriteAllText(SaveFile.FileName, DocumentPreviewRichTextBox.Text)
        End If
    End Sub

    Private Sub OpenToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem1.Click
        'Opens previous

        Shell("Explorer.exe")
    End Sub

    Private Sub ProgramInformationToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgramInformationToolStripMenuItem1.Click
        'Displays information on the program

        AboutMeAboutBox.Show()
    End Sub

    Private Sub DoughnutsTrackerTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DoughnutsTrackerTextBox.TextChanged
        'Disables SellADoughnut button if the Tracker number = the NumericUpDown value

        Dim doughnuttraces As Integer
        doughnuttraces = Integer.Parse(DoughnutsTrackerTextBox.Text)
        If doughnuttraces = DoughnutsQTYNumericUpDown.Value Then
            SellADoughnutButton.Enabled = False
        ElseIf doughnuttraces <> DoughnutsQTYNumericUpDown.Value Then
            SellADoughnutButton.Enabled = True
        End If
    End Sub

    Private Sub CoffeeTrackerTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CoffeeTrackerTextBox.TextChanged
        'Disables the SellACoffee button if the Tracker number  = the NumericUpDown value

        Dim coffeetraces As Integer
        coffeetraces = Integer.Parse(CoffeeTrackerTextBox.Text)
        If coffeetraces = CoffeeQTYNumericUpDown.Value Then
            SellACoffeeButton.Enabled = False
        ElseIf coffeetraces <> CoffeeQTYNumericUpDown.Value Then
            SellACoffeeButton.Enabled = True
        End If
    End Sub

    Private Sub FlavoringTrackerTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlavoringTrackerTextBox.TextChanged
        'Disables the SellAFlavoring Button if the Tracker number = the NumericUpDown value

        Dim Flavortraces As Integer
        Flavortraces = Integer.Parse(FlavoringTrackerTextBox.Text)
        If Flavortraces = FlavorQTYNumericUpDown.Value Then
            SellAFlavoringButton.Enabled = False
        ElseIf Flavortraces <> FlavorQTYNumericUpDown.Value Then
            SellAFlavoringButton.Enabled = True
        End If
    End Sub

    Private Sub BananasTrackerTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BananasTrackerTextBox.TextChanged
        'Disables the SellABananaButton if bananastraces = NumericUpDown value

        Dim bananastraces As Integer
        bananastraces = Integer.Parse(BananasTrackerTextBox.Text)
        If bananastraces = BananasQTYNumericUpDown.Value Then
            SellABananaButton.Enabled = False
        ElseIf bananastraces <> BananasQTYNumericUpDown.Value Then
            SellABananaButton.Enabled = True
        End If
    End Sub

    Private Sub OrangesTrackerTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrangesTrackerTextBox.TextChanged
        ' Disables the SellAOrangeButton if the the text = NumericUpDown value

        Dim orangestraces As Integer
        orangestraces = Integer.Parse(OrangesTrackerTextBox.Text)
        If orangestraces = OrangesQTYNumericUpDown.Value Then
            SellAOrangeButton.Enabled = False
        ElseIf orangestraces <> OrangesQTYNumericUpDown.Value Then
            SellAOrangeButton.Enabled = True
        End If
    End Sub

    Private Sub HotChocolateTrackerTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HotChocolateTrackerTextBox.TextChanged
        'Disables the SellHotChocolate button if the text = NumericUpDown.Value

        Dim hotchocolatetraces As Integer
        hotchocolatetraces = Integer.Parse(HotChocolateTrackerTextBox.Text)
        If hotchocolatetraces = HotChocolateQTYNumericUpDown.Value Then
            SellHotChocolateButton.Enabled = False
        ElseIf hotchocolatetraces <> HotChocolateQTYNumericUpDown.Value Then
            SellHotChocolateButton.Enabled = True
        End If
    End Sub

    Private Sub OtherItemTrackerTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OtherItemTrackerTextBox.TextChanged
        'Disables the SellOtherItem button if the text = NumericUpDown.Value
        Dim otheritemtraces As Integer
        otheritemtraces = Integer.Parse(OtherItemTrackerTextBox.Text)
        If otheritemtraces = OtherItemQTYNumericUpDown.Value Then
            SellOtherItemButton.Enabled = False
        ElseIf otheritemtraces <> OtherItemQTYNumericUpDown.Value Then
            SellOtherItemButton.Enabled = True
        End If
    End Sub

    Private Sub EndSalesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndSalesButton.Click
        Try
            'Enabeling the text box
            TextBox1.Enabled = True
            Label11.Enabled = True

            'Defines the DocumentPreviewRichTextBox's text and does calcualtions. THIS IS A VERY LONG
            'ONE PLEASE BEAR WITH ME!

            'Dim... ALOT and Parse ALOT
            'Doughnuts
            Dim DoughnutsPriceDecimal As Decimal
            Dim DoughnutsCostDecimal As Decimal
            Dim DoughnutsTrackerDecimal As Decimal
            DoughnutsPriceDecimal = Decimal.Parse(DoughnutsPriceTextBox.Text)
            DoughnutsCostDecimal = Decimal.Parse(DoughnutsCostTextBox.Text)
            DoughnutsTrackerDecimal = Decimal.Parse(DoughnutsTrackerTextBox.Text)

            'Coffee
            Dim CoffeePriceDecimal As Decimal
            Dim CoffeeCostDecimal As Decimal
            Dim CoffeeTrackerDecimal As Decimal
            CoffeePriceDecimal = Decimal.Parse(CoffeePriceTextBox.Text)
            CoffeeCostDecimal = Decimal.Parse(CoffeeCostTextBox.Text)
            CoffeeTrackerDecimal = Decimal.Parse(CoffeeTrackerTextBox.Text)

            'Flavoring
            Dim FlavoringPriceDecimal As Decimal
            Dim FlavoringCostDecimal As Decimal
            Dim FlavoringTrackerDecimal As Decimal
            FlavoringPriceDecimal = Decimal.Parse(FlavorPriceTextBox.Text)
            FlavoringCostDecimal = Decimal.Parse(FlavorCostTextBox.Text)
            FlavoringTrackerDecimal = Decimal.Parse(FlavoringTrackerTextBox.Text)

            'Bananas
            Dim BananasPriceDecimal As Decimal
            Dim BananasCostDecimal As Decimal
            Dim BananasTrackerDecimal As Decimal
            BananasPriceDecimal = Decimal.Parse(BananasPriceTextBox.Text)
            BananasCostDecimal = Decimal.Parse(BananasCostTextBox.Text)
            BananasTrackerDecimal = Decimal.Parse(BananasTrackerTextBox.Text)

            'Oranges
            Dim OrangesPriceDecimal As Decimal
            Dim OrangesCostDecimal As Decimal
            Dim OrangesTrackerDecimal As Decimal
            OrangesPriceDecimal = Decimal.Parse(OrangesPriceTextBox.Text)
            OrangesCostDecimal = Decimal.Parse(OrangesCostTextBox.Text)
            OrangesTrackerDecimal = Decimal.Parse(OrangesTrackerTextBox.Text)

            'Hot Chocolate
            Dim HotChocolatePriceDecimal As Decimal
            Dim HotChocolateCostDecimal As Decimal
            Dim HotChocolateTrackerDecimal As Decimal
            HotChocolatePriceDecimal = Decimal.Parse(HotChocolatePriceTextBox.Text)
            HotChocolateCostDecimal = Decimal.Parse(HotChocolateCostTextBox.Text)
            HotChocolateTrackerDecimal = Decimal.Parse(HotChocolateTrackerTextBox.Text)

            'Other Item
            Dim OtherItemPriceDecimal As Decimal
            Dim OtherItemCostDecimal As Decimal
            Dim OtherItemTrackerDecimal As Decimal
            OtherItemPriceDecimal = Decimal.Parse(OtherItemPriceTextBox.Text)
            OtherItemCostDecimal = Decimal.Parse(OtherItemCostTextBox.Text)
            OtherItemTrackerDecimal = Decimal.Parse(OtherItemTrackerTextBox.Text)

            'Others below
            Dim ExtraExpensesDecimal As Decimal
            Dim TotalTaxDecimal As Decimal
            ExtraExpensesDecimal = Decimal.Parse(ExtraExpensesTextBox.Text)
            TotalTaxDecimal = Decimal.Parse(TotalTaxTextBox.Text)

            'All of the text put into the Document preview
            DocumentPreviewRichTextBox.Text = DateTimePicker1.Text & System.Environment.NewLine & _
            System.Environment.NewLine & _
            "Other Item Name = " & DefineOtherItemTextBox.Text & System.Environment.NewLine & _
            "Doughnuts QTY = " & DoughnutsQTYNumericUpDown.Value & System.Environment.NewLine & _
            "Coffee QTY = " & CoffeeQTYNumericUpDown.Value & System.Environment.NewLine & _
            "Flavoring QTY = " & FlavorQTYNumericUpDown.Value & System.Environment.NewLine & _
            "Bananas QTY = " & BananasQTYNumericUpDown.Value & System.Environment.NewLine & _
            "Oranges QTY = " & OrangesQTYNumericUpDown.Value & System.Environment.NewLine & _
            "Hot Chocolate QTY = " & HotChocolateQTYNumericUpDown.Value & System.Environment.NewLine & _
            "Other item QTY = " & OtherItemQTYNumericUpDown.Value & System.Environment.NewLine & _
            System.Environment.NewLine & _
            "Doughnuts Cost = " & DoughnutsCostTextBox.Text & System.Environment.NewLine & _
            "Coffee Cost = " & CoffeeCostTextBox.Text & System.Environment.NewLine & _
            "Flavoring Cost = " & FlavorCostTextBox.Text & System.Environment.NewLine & _
            "Bananas Cost = " & BananasCostTextBox.Text & System.Environment.NewLine & _
            "Oranges Cost = " & OrangesCostTextBox.Text & System.Environment.NewLine & _
            "Hot Chocolate Cost = " & HotChocolateCostTextBox.Text & System.Environment.NewLine & _
            "Other Item Cost = " & OtherItemCostTextBox.Text & System.Environment.NewLine & _
            System.Environment.NewLine & _
            "Doughnuts Price = " & DoughnutsPriceTextBox.Text & System.Environment.NewLine & _
            "Coffee Price = " & CoffeePriceTextBox.Text & System.Environment.NewLine & _
            "Flavor Price = " & FlavorPriceTextBox.Text & System.Environment.NewLine & _
            "Bananas Price = " & BananasPriceTextBox.Text & System.Environment.NewLine & _
            "Oranges Price = " & OrangesPriceTextBox.Text & System.Environment.NewLine & _
            "Hot Chocolate Price = " & HotChocolatePriceTextBox.Text & System.Environment.NewLine & _
            "Other Item Price = " & OtherItemPriceTextBox.Text & System.Environment.NewLine & _
            System.Environment.NewLine & _
            "Doughnuts sold = " & DoughnutsTrackerTextBox.Text & System.Environment.NewLine & _
            "Coffee sold = " & CoffeeTrackerTextBox.Text & System.Environment.NewLine & _
            "Flavor sold = " & FlavoringTrackerTextBox.Text & System.Environment.NewLine & _
            "Bananas sold = " & BananasTrackerTextBox.Text & System.Environment.NewLine & _
            "Oranges sold = " & OrangesTrackerTextBox.Text & System.Environment.NewLine & _
            "Hot Chocolate sold = " & HotChocolateTrackerTextBox.Text & System.Environment.NewLine & _
            "Other Item sold = " & OtherItemTrackerTextBox.Text & System.Environment.NewLine & _
            System.Environment.NewLine & _
            System.Environment.NewLine & _
            "TOTALS" & System.Environment.NewLine & _
            System.Environment.NewLine & _
            "Total Taxes = " & TotalTaxTextBox.Text & System.Environment.NewLine & _
            "Total losses = " & ((DoughnutsQTYNumericUpDown.Value * DoughnutsCostDecimal) + _
            (CoffeeQTYNumericUpDown.Value * CoffeeCostDecimal) + _
            (FlavorQTYNumericUpDown.Value * FlavoringCostDecimal) + _
            (BananasQTYNumericUpDown.Value * BananasCostDecimal) + _
            (OrangesQTYNumericUpDown.Value * OrangesCostDecimal) + _
            (HotChocolateQTYNumericUpDown.Value * HotChocolateCostDecimal) + _
            (OtherItemQTYNumericUpDown.Value * OtherItemCostDecimal) + _
            (TotalTaxDecimal) + (ExtraExpensesDecimal)) & System.Environment.NewLine & _
            "Total Gains = " & ((DoughnutsPriceDecimal * DoughnutsTrackerDecimal) + _
            (CoffeePriceDecimal * CoffeeTrackerDecimal) + _
            (FlavoringPriceDecimal * FlavoringTrackerDecimal) + _
            (BananasPriceDecimal * BananasTrackerDecimal) + _
            (OrangesPriceDecimal * OrangesTrackerDecimal) + _
            (HotChocolatePriceDecimal * HotChocolateTrackerDecimal) + _
            (OtherItemPriceDecimal * OtherItemTrackerDecimal)) & System.Environment.NewLine & _
            "Final Result = " & ((DoughnutsPriceDecimal * DoughnutsTrackerDecimal) + _
            (CoffeePriceDecimal * CoffeeTrackerDecimal) + _
            (FlavoringPriceDecimal * FlavoringTrackerDecimal) + _
            (BananasPriceDecimal * BananasTrackerDecimal) + _
            (OrangesPriceDecimal * OrangesTrackerDecimal) + _
            (HotChocolatePriceDecimal * HotChocolateTrackerDecimal) + _
            (OtherItemPriceDecimal * OtherItemTrackerDecimal)) - _
            ((DoughnutsQTYNumericUpDown.Value * DoughnutsCostDecimal) + _
            (CoffeeQTYNumericUpDown.Value * CoffeeCostDecimal) + _
            (FlavorQTYNumericUpDown.Value * FlavoringCostDecimal) + _
            (BananasQTYNumericUpDown.Value * BananasCostDecimal) + _
            (OrangesQTYNumericUpDown.Value * OrangesCostDecimal) + _
            (HotChocolateQTYNumericUpDown.Value * HotChocolateCostDecimal) + _
            (OtherItemQTYNumericUpDown.Value * OtherItemCostDecimal) + _
            (TotalTaxDecimal) + (ExtraExpensesDecimal))

            TextBox1.Text = ((DoughnutsPriceDecimal * DoughnutsTrackerDecimal) + _
            (CoffeePriceDecimal * CoffeeTrackerDecimal) + _
            (FlavoringPriceDecimal * FlavoringTrackerDecimal) + _
            (BananasPriceDecimal * BananasTrackerDecimal) + _
            (OrangesPriceDecimal * OrangesTrackerDecimal) + _
            (HotChocolatePriceDecimal * HotChocolateTrackerDecimal) + _
            (OtherItemPriceDecimal * OtherItemTrackerDecimal)) - _
            ((DoughnutsQTYNumericUpDown.Value * DoughnutsCostDecimal) + _
            (CoffeeQTYNumericUpDown.Value * CoffeeCostDecimal) + _
            (FlavorQTYNumericUpDown.Value * FlavoringCostDecimal) + _
            (BananasQTYNumericUpDown.Value * BananasCostDecimal) + _
            (OrangesQTYNumericUpDown.Value * OrangesCostDecimal) + _
            (HotChocolateQTYNumericUpDown.Value * HotChocolateCostDecimal) + _
            (OtherItemQTYNumericUpDown.Value * OtherItemCostDecimal) + _
            (TotalTaxDecimal) + (ExtraExpensesDecimal))

            'sets forecolor
            If TextBox1.Text < 0 Then
                TextBox1.ForeColor = Color.Red
            ElseIf TextBox1.Text = 0 Then
                TextBox1.ForeColor = Color.Blue
            ElseIf TextBox1.Text > 0 Then
                TextBox1.ForeColor = Color.Green
            End If
        Catch Ex As Exception
        End Try
    End Sub
End Class


This post has been edited by smohd: 04 November 2011 - 12:06 AM
Reason for edit:: Title renamed and email removed


Is This A Good Question/Topic? 0
  • +

Replies To: Draw string in a PrintDocument object

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Draw string in a PrintDocument object

Posted 03 November 2011 - 08:39 PM

There are lots of things you can do to clean this up and shorten it. You may have noticed that many of the checkbox events have a similar format...

With SomeKindOfCheckBox.Checked
     SomeQTYNumericUpDown.Enabled = True
     SomeCostTextBox.Enabled = True
     SomePriceTextBox.Enabled = True
End With

If SomeKindOfCheckBox.CheckState = CheckState.Unchecked Then
     SomeQTYNumericUpDown.Enabled = False
     SomeCostTextBox.Enabled = False
     SomePriceTextBox.Enabled = False
End If



Since many of these controls are grouped together, you can put them on a panel control and disable the panel or you can use the checkbox checked property itself to set the values...

SomeQTYNumericUpDown.Enabled = SomeKindOfCheckBox.Checked
SomeCostTextBox.Enabled =  SomeKindOfCheckBox.Checked
SomePriceTextBox.Enabled =  SomeKindOfCheckBox.Checked



Now we have cut that down to three lines. But do we want these three lines for every one of the checkbox events (aka CheckChanged event)?

No, probably not. Since all the events are pretty similar in format you can create a new function that we will pass it three controls and it will enable/disable them.

Private Sub EnableDisable(ByRef numUpDown as NumericUpDown, ByRef cost as TextBox, ByRef price as TextBox)
    numUpDown.Enabled = SomeKindOfCheckBox.Checked
    cost.Enabled =  SomeKindOfCheckBox.Checked
    price.Enabled =  SomeKindOfCheckBox.Checked
End Sub



Then from each event you can simply call this new Sub and give it the three controls you want to enable. So notice with these two changes we have cut down like 6 events of 10 lines each (60 total lines) into a function of 3 lines and 6 calls (9 lines).

Refactoring at its best! We haven't even gotten to your long event yet. That event, since you are printing a lot of stuff, can probably be made much simpler using String.Format.

:)
Was This Post Helpful? 1
  • +
  • -

#3 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Re: Draw string in a PrintDocument object

Posted 03 November 2011 - 08:47 PM

View PostMartyr2, on 03 November 2011 - 08:39 PM, said:

There are lots of things you can do to clean this up and shorten it. You may have noticed that many of the checkbox events have a similar format...

With SomeKindOfCheckBox.Checked
     SomeQTYNumericUpDown.Enabled = True
     SomeCostTextBox.Enabled = True
     SomePriceTextBox.Enabled = True
End With

If SomeKindOfCheckBox.CheckState = CheckState.Unchecked Then
     SomeQTYNumericUpDown.Enabled = False
     SomeCostTextBox.Enabled = False
     SomePriceTextBox.Enabled = False
End If



Since many of these controls are grouped together, you can put them on a panel control and disable the panel or you can use the checkbox checked property itself to set the values...

SomeQTYNumericUpDown.Enabled = SomeKindOfCheckBox.Checked
SomeCostTextBox.Enabled =  SomeKindOfCheckBox.Checked
SomePriceTextBox.Enabled =  SomeKindOfCheckBox.Checked



Now we have cut that down to three lines. But do we want these three lines for every one of the checkbox events (aka CheckChanged event)?

No, probably not. Since all the events are pretty similar in format you can create a new function that we will pass it three controls and it will enable/disable them.

Private Sub EnableDisable(ByRef numUpDown as NumericUpDown, ByRef cost as TextBox, ByRef price as TextBox)
    numUpDown.Enabled = SomeKindOfCheckBox.Checked
    cost.Enabled =  SomeKindOfCheckBox.Checked
    price.Enabled =  SomeKindOfCheckBox.Checked
End Sub



Then from each event you can simply call this new Sub and give it the three controls you want to enable. So notice with these two changes we have cut down like 6 events of 10 lines each (60 total lines) into a function of 3 lines and 6 calls (9 lines).

Refactoring at its best! We haven't even gotten to your long event yet. That event, since you are printing a lot of stuff, can probably be made much simpler using String.Format.

:)



Thank you very much!
I think that can trim down my code a lot. I have another question too. With VB 08, how do you print the contents of a RichTextBox to PrintPreview, and Which would I use, PrintDocument or PrintDialog?

:)
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Draw string in a PrintDocument object

Posted 03 November 2011 - 09:17 PM

You create an instance of the PrintDocument class, which you draw on for printing. You can draw a string to this document using a line like e.Graphics.DrawString from within the PrintPage event. It is then passed to the PrintPreviewDialog.Document property to then preview it.

The PrintDialog is a separate dialog used for selecting a printer (it is the dialog you see in an application when you hit the print menu option) and it asks you to choose a printer, how many copies etc.

Hopefully that answered the question.

This post has been edited by Martyr2: 03 November 2011 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Re: Draw string in a PrintDocument object

Posted 03 November 2011 - 09:20 PM

View PostMartyr2, on 03 November 2011 - 09:17 PM, said:

You create an instance of the PrintDocument class, which you draw on for printing. You can draw a string to this document using a line like e.Graphics.DrawString from within the PrintPage event. It is then passed to the PrintPreviewDialog.Document property to then preview it.

The PrintDialog is a separate dialog used for selecting a printer (it is the dialog you see in an application when you hit the print menu option) and it asks you to choose a printer, how many copies etc.

Hopefully that answered the question.


Yes, That did answer my question. Thank you very much for your assistance!
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Draw string in a PrintDocument object

Posted 03 November 2011 - 11:10 PM

Cant you also do this:
Private a,b,c,d,e,f,g,h As Integer

? If so it would trim your code.
Was This Post Helpful? 0
  • +
  • -

#7 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Re: Draw string in a PrintDocument object

Posted 04 November 2011 - 09:58 AM

View PostjimmyBo, on 03 November 2011 - 11:10 PM, said:

Cant you also do this:
Private a,b,c,d,e,f,g,h As Integer

? If so it would trim your code.


Using that would not work here because It needs to include the "=0" included.

Therefore I cannot shorten that one:

Private a as Integer = 0


... is the necessary format.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Draw string in a PrintDocument object

Posted 04 November 2011 - 10:47 AM

The default value of the Type integer is 0.
Was This Post Helpful? 0
  • +
  • -

#9 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Draw string in a PrintDocument object

Posted 04 November 2011 - 05:00 PM

I think it's a mental thing, whatever makes you feel better right? haha. I do it sometimes too.
Was This Post Helpful? 0
  • +
  • -

#10 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Re: Draw string in a PrintDocument object

Posted 04 November 2011 - 05:06 PM

View Posttrevster344, on 04 November 2011 - 05:00 PM, said:

I think it's a mental thing, whatever makes you feel better right? haha. I do it sometimes too.


I was taught to add the =0. I guess it gets hard to change after you do it for years! :)
Was This Post Helpful? 0
  • +
  • -

#11 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: Draw string in a PrintDocument object

Posted 04 November 2011 - 06:49 PM

Older languages would end up using whatever value was in the memory reserved for that variable if it wasn't initialized, so you could end up with junk characters or ints.
Was This Post Helpful? 0
  • +
  • -

#12 Douggie0001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 11

Re: Draw string in a PrintDocument object

Posted 04 November 2011 - 08:45 PM

For the long End Sales, should I separate the math from the richtextbox entry?, or should I just leave it as it is?

Can we start a vote?, yes or no???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1