Fist off, I am new to coding so please no negativity.
I have attempted to build an app for a shopping list.
I think i have most of it complete but i have a couple of issues. Mainly Printing. Right now I have the main form that the shopping list is created on then a print lists button that prints the shopping list then sorts it and put the sorted list into a text box on form2 ( Cell allignment is off) then I have an OK or cancel button. I would like on form 1 for the pring lists button to just bring up the sorted list in the textbox on form 2 and NOT print the origional list. THen on form2 with my OK button i would like to print both lists.
Also if anyone can help me fix the cell allinment in form2 i would appreciate it.
HEre is my code for the main Form
I have attempted to build an app for a shopping list.
I think i have most of it complete but i have a couple of issues. Mainly Printing. Right now I have the main form that the shopping list is created on then a print lists button that prints the shopping list then sorts it and put the sorted list into a text box on form2 ( Cell allignment is off) then I have an OK or cancel button. I would like on form 1 for the pring lists button to just bring up the sorted list in the textbox on form 2 and NOT print the origional list. THen on form2 with my OK button i would like to print both lists.
Also if anyone can help me fix the cell allinment in form2 i would appreciate it.
HEre is my code for the main Form
Code:
Imports System.Drawing.Printing
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing
Public Class Grocery_List
Private Sub ShoppingListView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
End Sub
Dim mDataTable As New DataTable
Dim mIsEditMode As Boolean
Dim mIndex As Integer
Private Sub Twyman_Family_Shopping_List_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindGrid()
End Sub
Private Sub BindGrid()
mDataTable.Columns.Add("Item")
mDataTable.Columns.Add("Brand")
mDataTable.Columns.Add("Store")
mDataTable.Columns.Add("Price")
mDataTable.Columns.Add("Size")
mDataTable.Columns.Add("Coupon")
ShoppingListView.DataSource = mDataTable
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim dr As DataRow
If mIsEditMode = False Then
dr = mDataTable.NewRow
mDataTable.Rows.Add(dr)
Else
dr = mDataTable.Rows(mIndex)
mIsEditMode = False
End If
dr("Item") = txtItem.Text
dr("Brand") = txtBrand.Text
dr("Store") = cmbPrice_Match_Store.SelectedItem
dr("Price") = txtPrice.Text
dr("Size") = txtSize.Text
dr("Coupon") = txtCoupon.Text
txtItem.Text = ""
txtItem.Select()
txtBrand.Text = ""
cmbPrice_Match_Store.Text = ""
txtPrice.Text = ""
txtSize.Text = ""
txtCoupon.Text = ""
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ShoppingListView.CellContentClick
If e.RowIndex >= 0 Then
If e.ColumnIndex = ShoppingListView.Columns("clmDelete").Index Then
If MessageBox.Show("Are you sure you want to delete?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
mDataTable.Rows.RemoveAt(e.RowIndex)
End If
End If
End If
End Sub
Private Sub Clear_Lists_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearLists.Click
ShoppingListView.DataSource = mDataTable
mDataTable.Rows.Clear()
txtItem.Focus()
End Sub
Private Sub Coupon_Text_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCoupon.KeyDown
If e.KeyCode = Keys.Enter Then
Dim dr As DataRow
If mIsEditMode = False Then
dr = mDataTable.NewRow
mDataTable.Rows.Add(dr)
Else
dr = mDataTable.Rows(mIndex)
mIsEditMode = False
End If
If txtCoupon.Text.Length > 0 Then
txtCoupon.Text = -txtCoupon.Text
End If
If txtCoupon.Text.Length = 0 Then txtCoupon.Text = "$0.00"
Dim dblAmt As Double = Decimal.Parse(txtCoupon.Text, Globalization.NumberStyles.Currency)
txtCoupon.Text = dblAmt.ToString("c")
dr("Item") = txtItem.Text
dr("Brand") = txtBrand.Text
dr("Store") = cmbPrice_Match_Store.SelectedItem
dr("Price") = txtPrice.Text
dr("Size") = txtSize.Text
dr("Coupon") = txtCoupon.Text
txtItem.Text = ""
txtItem.Select()
txtBrand.Text = ""
cmbPrice_Match_Store.Text = ""
txtPrice.Text = ""
txtSize.Text = ""
txtCoupon.Text = ""
End If
End Sub
Private Sub txtCoupon_Validating(ByVal sender As Object, ByVal e As EventArgs) Handles txtCoupon.Validating
If txtCoupon.Text.Length > 0 Then
txtCoupon.Text = -txtCoupon.Text
End If
End Sub
Private Sub txtcoupon_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txtCoupon.Validated
If txtCoupon.Text.Length = 0 Then txtCoupon.Text = "$0.00"
Dim dblAmt As Double = Decimal.Parse(txtCoupon.Text, Globalization.NumberStyles.Currency)
txtCoupon.Text = dblAmt.ToString("c")
End Sub
Private Sub txtPrice_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtPrice.Validating
If txtPrice.Text.Length = 0 Then txtPrice.Text = "$0.00"
Dim dblAmt As Double = Decimal.Parse(txtPrice.Text, Globalization.NumberStyles.Currency)
txtPrice.Text = dblAmt.ToString("c")
End Sub
Private Sub PrintLists_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintLists.Click
Dim MyPrintDialog = New PrintDialog()
MyPrintDialog.Document = printDocument1
MyPrintDialog.UseEXDialog = True
If DialogResult.OK = MyPrintDialog.ShowDialog() Then
printDocument1.DocumentName = "Shopping List"
printDocument1.Print()
End If
ShoppingListView.Sort(ShoppingListView.Columns(3), System.ComponentModel.ListSortDirection.Ascending) 'simply sorts by store name
Dialog1.TextBox1.Text = ""
For i = 0 To ShoppingListView.Rows.Count - 2
If i = 0 OrElse Not ShoppingListView.Rows(i).Cells(3).Value.Equals(ShoppingListView.Rows(i - 1).Cells(3).Value) Then
NewHeading(ShoppingListView.Rows(i).Cells(3).Value.ToString, Dialog1.TextBox1)
End If
AddLine(ShoppingListView.Rows(i), Dialog1.TextBox1)
Next
Dialog1.ShowDialog()
PrintDocument2.Print()
End Sub
Private Sub NewHeading(ByVal s As String, ByVal tb As TextBox)
tb.AppendText(vbCrLf & s.ToUpper & vbCrLf & New String("="c, s.Length) & vbCrLf)
tb.AppendText("Item" & vbTab & "Brand" & vbTab & "Price" & vbTab & "Size/No." & vbTab & "Coupon" & vbCrLf)
End Sub
Private Sub AddLine(ByVal dgr As DataGridViewRow, ByVal tb As TextBox)
For i = 0 To 5
If i <> 2 Then ' no point showing the store a second time
Try 'prevent unhandled null exceptions from blank cells
tb.AppendText(dgr.Cells(i).Value.ToString & vbTab)
Catch ex As Exception
tb.AppendText(vbTab)
End Try
End If
Next
tb.AppendText(vbCrLf)
End Sub
Private WithEvents printDocument1 As New PrintDocument
Dim arrColumnLefts = New ArrayList()
Dim arrColumnWidths = New ArrayList()
Dim iCellHeight As Integer = 0
Dim iTotalWidth As Integer = 0
Dim iCount As Integer = 0
Dim iRow As Integer = 0
Dim bFirstPage As Boolean = False
Dim bNewPage As Boolean = False
Dim iHeaderHeight As Integer = 0
Dim strFormat = New StringFormat()
Private Sub printDocument1_BeginPrint(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles _
printDocument1.BeginPrint
Try
strFormat.Alignment = StringAlignment.Near
strFormat.LineAlignment = StringAlignment.Center
strFormat.Trimming = StringTrimming.EllipsisCharacter
arrColumnLefts.Clear()
arrColumnWidths.Clear()
iCellHeight = 0
bFirstPage = True
bNewPage = True
iTotalWidth = 0
For Each dgvGridCol As DataGridViewColumn In ShoppingListView.Columns
iTotalWidth = iTotalWidth + dgvGridCol.Width
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
printDocument1.PrintPage
Try
Dim iLeftMargin As Integer = e.MarginBounds.Left
Dim iTopMargin As Integer = e.MarginBounds.Top
Dim bMorePagesToPrint As Boolean = False
Dim iTmpWidth As Integer = 0
If bFirstPage = True Then
For Each Gridcol As DataGridViewColumn In ShoppingListView.Columns
iTmpWidth = CType(Math.Floor(CType(CType(Gridcol.Width, Double) / CType(iTotalWidth, Double) * CType(iTotalWidth, Double) * (CType(e.MarginBounds.Width, Double) / CType(iTotalWidth, Double)), Double)), Integer)
iHeaderHeight = CType((e.Graphics.MeasureString(Gridcol.HeaderText, Gridcol.InheritedStyle.Font, iTmpWidth).Height) + 11, Integer)
arrColumnLefts.Add(iLeftMargin)
arrColumnWidths.Add(iTmpWidth)
iLeftMargin = iLeftMargin + iTmpWidth
Next
End If
While iRow <= ShoppingListView.Rows.Count - 1
Dim GridRow As DataGridViewRow = ShoppingListView.Rows(iRow)
iCellHeight = GridRow.Height + 5
Dim iCount As Integer = 0
If iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
bNewPage = True
bFirstPage = False
bMorePagesToPrint = True
'********* what should this be **********
Exit While
Else
If bNewPage = True Then
e.Graphics.DrawString("Shopping List", New Font(ShoppingListView.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(ShoppingListView.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)
Dim strDate As String = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString()
e.Graphics.DrawString(strDate, New Font(ShoppingListView.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(strDate, New Font(ShoppingListView.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary", New Font(New Font(ShoppingListView.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13)
iTopMargin = e.MarginBounds.Top
For Each GridCol As DataGridViewColumn In ShoppingListView.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight))
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, New SolidBrush(GridCol.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iHeaderHeight), strFormat)
iCount = iCount + 1
Next GridCol
bNewPage = False
iTopMargin += iHeaderHeight
End If
iCount = 0
For Each Cel As DataGridViewCell In GridRow.Cells
If Not IsDBNull(Cel.Value) Then
e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, New SolidBrush(Cel.InheritedStyle.ForeColor), New RectangleF(CType(arrColumnLefts(iCount), Integer), CType(iTopMargin, Single), CType(arrColumnWidths(iCount), Integer), CType(iCellHeight, Single)), strFormat)
End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CType(arrColumnLefts(iCount), Integer), iTopMargin, CType(arrColumnWidths(iCount), Integer), iCellHeight))
iCount = iCount + 1
Next
End If
iRow = iRow + 1
iTopMargin = iTopMargin + iCellHeight
End While
If bMorePagesToPrint = True Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch exc As Exception
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class