Listing 1

Imports Microsoft.Office.Tools.Ribbon

Public Class Ribbon

    Private Sub Ribbon_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load

    End Sub

    Private Sub chkChinook_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _
        Handles chkChinook.Click
        Call UpdatePeriodDropDown()
    End Sub

    Private Sub chkNorthwind_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _
        Handles chkNorthwind.Click
        Call UpdatePeriodDropDown()
    End Sub

    Private Sub UpdatePeriodDropDown()
        Dim periods As List(Of String) =
            Utility.UpdateAccountingPeriods(chkChinook.Checked, chkNorthwind.Checked)
        Me.ddlPeriod.Items.Clear()
        If (Not IsNothing(periods) AndAlso periods.Count > 0) Then
            For index = 0 To periods.Count - 1
                Dim entry = Factory.CreateRibbonDropDownItem
                entry.Label = periods(index)
                Me.ddlPeriod.Items.Add(entry)
            Next
        End If
    End Sub

    Private Sub btnLoadData_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _
        Handles btnLoadData.Click

        Dim AccountingPeriod = ddlPeriod.SelectedItem.Label
        WriteConsolidatedSheet(AccountingPeriod)
    End Sub

    Private Sub btnTestAddWorkSheet_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnTestAddWorkSheet.Click
        Dim wsUtility As New Utility
        Dim wsNextSheetNumber = Globals.ThisWorkbook.Worksheets.Count + 1
        wsUtility.AddWorksheet(String.Format("AfterSheet{0}", wsNextSheetNumber), True)
    End Sub

    Private Sub btnTestWorksheetNoName_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnTestWorksheetNoName.Click
        Dim wsUtility As New Utility
        Dim wsNextSheetNumber = Globals.ThisWorkbook.Worksheets.Count + 1
        wsUtility.AddWorksheet(String.Format("BeforeSheet{0}", wsNextSheetNumber), False)
    End Sub

    ' FillWorkSheetWithTable("Ref_Rows", Properties.Settings.Default.Ref_Rows, "", "");

    Private Sub btnChinookDetailForPeriod_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnChinookDetailForPeriod.Click
        Dim AccountingPeriod = txtPeriod.Text
        Dim wsUtility As New Utility
        Dim wsName = String.Format("Chinook Invoices {0}", AccountingPeriod)
        wsUtility.AddWorksheet(wsName, True)
        wsUtility.AddChinookInvoicesForPeriod(wsName, AccountingPeriod)
    End Sub

    Private Sub ddlTestLocateLabel_SelectionChanged(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles ddlTestLocateLabel.SelectionChanged
        Dim LabelText = ddlTestLocateLabel.SelectedItem.Label
        Dim wsUtility As New Utility
        Dim row = wsUtility.FindFirstOccuranceRow(1, LabelText)
        MessageBox.Show(String.Format("Text '{0}' found in row {1}", LabelText, row))
    End Sub

    Public Sub WriteConsolidatedSheet(AccountingPeriod As String)
        Dim wsUtility As New Utility

        wsUtility.WriteValueToSheet("Period", 3, AccountingPeriod)
        'MessageBox.Show(String.Format("Load Data Clicked for period {0}", AccountingPeriod))

        Dim ChinookTotalSales = Data.Chinook.Data.TotalSalesForPeriod(AccountingPeriod)
        wsUtility.WriteValueToSheet("ChinookSales", 3, ChinookTotalSales.ToString)
        'MessageBox.Show("Total Sales Retrieved For Chinook")

        Dim NorthwindTotalSales = Data.Northwind.Data.TotalSalesForPeriod(AccountingPeriod)
        wsUtility.WriteValueToSheet("NorthwindSales", 3, NorthwindTotalSales.ToString)
        'MessageBox.Show("Total Sales Retrieved For Northwind")

        Dim TotalSales = ChinookTotalSales + NorthwindTotalSales
        wsUtility.WriteValueToSheet("CombinedSales", 3, TotalSales.ToString)

        Dim LargestSale = "Chinook"
        If NorthwindTotalSales > ChinookTotalSales Then LargestSale = "Northwind"

        Dim ConsolidatedDetail = wsUtility.ConsolidateDetail(AccountingPeriod)
        Dim BoldRow = 0

        For index = 0 To ConsolidatedDetail.Count - 1
            Dim row As Integer = 0
            If index = 0 Then
                row = wsUtility.FindFirstOccuranceRow(1, "FormatRow")
            ElseIf index = ConsolidatedDetail.Count - 1 Then
                row = wsUtility.FindFirstOccuranceRow(1, "DataRow")
            Else
                row = wsUtility.FindFirstOccuranceRow(1, "DataRow")
                wsUtility.InsertRowActiveSheet(row)
                'MessageBox.Show("Inserted Row")
                wsUtility.CopyRowActiveSheet(row + 1, row, False)
                'MessageBox.Show("Copied Row")
            End If

            'Should the line be bold due to highest sales for the month?
            If ConsolidatedDetail(index).LargestSale Then BoldRow = row

            wsUtility.WriteValueToSheet(row, 1, "")
            wsUtility.WriteValueToSheet(row, 2, ConsolidatedDetail(index).Company)
            wsUtility.WriteValueToSheet(row, 3, (index + 1).ToString)
            wsUtility.WriteValueToSheet(row, 4, ConsolidatedDetail(index).ItemDate.ToShortDateString)
            wsUtility.WriteValueToSheet(row, 5, ConsolidatedDetail(index).CustomerName)
            wsUtility.WriteValueToSheet(row, 6, ConsolidatedDetail(index).Amount.ToString)
            wsUtility.WriteValueToSheet(row, 8, ConsolidatedDetail(index).SalesToDate.ToString)
            row += 1
            'MessageBox.Show("Row Completed")
        Next

        'Set Largest Sale Row bold
        If BoldRow > 0 Then wsUtility.SetRowBold(BoldRow)

        'Hide Column A
        wsUtility.HideColumn(1)

        'Add a new worksheet to hold Chinook Invoices detail
        wsUtility.AddWorksheet("Chinook Detail", True)
        wsUtility.AddChinookInvoicesForPeriod("Chinook Detail", AccountingPeriod)
    End Sub

    Private Sub btnWorksheetSize_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnWorksheetSize.Click
        Dim wsUtility As New Utility
        Dim wsSize = wsUtility.GetActiveWorksheetDimensions()
        MessageBox.Show(String.Format("Active WorkSheet Size = {0} x {1}", wsSize.LastRow, wsSize.LastColumn))
    End Sub

    Private Sub txtColumnNumber_TextChanged(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles txtColumnNumber.TextChanged
        Dim colNum = txtColumnNumber.Text
        Dim wsUtility As New Utility
        Dim colLetter = wsUtility.GetColumnLetter(Integer.Parse(colNum))
        MessageBox.Show(String.Format("Column Letter for {0} is {1}", colNum, colLetter))
    End Sub

    Private Sub btnLargestOrderForPeriod_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnLargestOrderForPeriod.Click
        Dim AccountingPeriod = txtPeriod.Text
        Dim LargestChinook = Data.Chinook.Data.LargestSaleForPeriod(AccountingPeriod)
        Dim msg1 = String.Format("Largest Invoice for Chinook in period {0} is InvoiceID {1}", AccountingPeriod, LargestChinook)
        MessageBox.Show(msg1)
        Dim LargestNorthwind = Data.Northwind.Data.LargestSaleForPeriod(AccountingPeriod)
        Dim msg2 = String.Format("Largest Order for Chinook in period {0} is OrderID {1}", AccountingPeriod, LargestNorthwind)
        MessageBox.Show(msg2)
    End Sub
End Class
comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube