I am making a Blackjack program for my programming class (using numbers only from 1 through 10 and cannot use Global Variables, which reason is beyond me). Whenever, I press butHit (button Hit), it always gets the answer, "Bust : Tie" even when the values for both the computer and your own hand are clearly below 21. This happens no matter what if you press butHit.
lblPlayV checks if the player has pressed butPlay before to get the random values and such.
lblCounter represents how many times butHit has been pressed.
I have no idea what could be wrong.
Here's the Code:
Public Class Form1
Private Sub butClose_Click(sender As System.Object, e As System.EventArgs) Handles butClose.Click
End
End Sub
Private Sub butPlay_Click(sender As System.Object, e As System.EventArgs) Handles butPlay.Click
Dim rand As New Random
Dim intM1 As Integer = rand.Next(1, 11)
Dim intM2 As Integer = rand.Next(1, 11)
Dim intM3 As Integer = rand.Next(1, 11)
Dim intM4 As Integer = rand.Next(1, 11)
Dim intM5 As Integer = rand.Next(1, 11)
Dim intOP1 As Integer = rand.Next(1, 11)
Dim intOP2 As Integer = rand.Next(1, 11)
Dim intOP3 As Integer = rand.Next(1, 11)
lblPlayV.Text = 1
lblCounter.Text = 1
butPlay.Text = "Replay"
lblM1.Text = intM1
lblM2.Text = intM2
lblM3.Text = intM3
lblM4.Text = intM4
lblM5.Text = intM5
lblOP1.Text = intOP1
lblOP2.Text = intOP2
lblOP3.Text = intOP3
lblM1.Visible = True
lblM2.Visible = True
End Sub
Private Sub butHit_Click(sender As System.Object, e As System.EventArgs) Handles butHit.Click
If lblPlayV.Text = 1 Then
'<<<<<<<<<<<<<<<<<<<<<<'
If lblCounter.Text = 1 Then
lblM3.Visible = True
If lblM1.Text + lblM2.Text + lblM3.Text = 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If
ElseIf lblM1.Text + lblM2.Text + lblM3.Text > 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If
Else
lblCounter.Text = lblCounter.Text + 1
End If
End If
'<<<<<<<<<<<<<<<<<<<<<<'
If lblCounter.Text = 2 Then
lblM4.Visible = True
If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text = 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If
ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text > 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If
Else
lblCounter.Text = lblCounter.Text + 1
End If
End If
'<<<<<<<<<<<<<<<<<<<<<<'
If lblCounter.Text = 3 Then
lblM5.Visible = True
If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If
ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > 21 Then
If lblOP1.Text + lblOP2.Text + lblOP3.Text > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If
Else
If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Draw")
ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
lblCounter.Text = 0
lblPlayV.Text = 0
End If
'<<<<<<<<<<<<<<<<<<<<<<'
End If
End Sub
Private Sub ButStand_Click(sender As System.Object, e As System.EventArgs) Handles ButStand.Click
If lblPlayV.Text = 1 Then
lblPlayV.Text = 0
If lblM3.Visible = False Then
If lblM1.Text + lblM2.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Tie")
ElseIf lblM1.Text + lblM2.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
If lblCounter.Text = 1 Then
If lblM1.Text + lblM2.Text + lblM3.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Tie")
ElseIf lblM1.Text + lblM2.Text + lblM3.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
If lblCounter.Text = 2 Then
If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Tie")
ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
If lblCounter.Text = 3 Then
If lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text = lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("Tie")
ElseIf lblM1.Text + lblM2.Text + lblM3.Text + lblM4.Text + lblM5.Text > lblOP1.Text + lblOP2.Text + lblOP3.Text Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
End If
End Sub
End Class
There is still allot missing in your code....
Like 1010 said if you get 3 cards " 10 10 1" the way you count the result will be "10101"and not 21.
To solve your problem... "Maybe you solve it already".
Use your ints to count "intM1 + intM2 + intM3"
Dim intM1 As Integer
Dim intM2 As Integer
Dim intM3 As Integer
Dim intM4 As Integer
Dim intM5 As Integer
Dim intOP1 As Integer
Dim intOP2 As Integer
Dim intOP3 As Integer
Place them outside the playbutton so you can use the value everywhere in the form.
Then inside your playbutton you give them a value.
intM1 = rand.Next(1, 11)
intM2 = rand.Next(1, 11)
intM3 = rand.Next(1, 11)
intM4 = rand.Next(1, 11)
intM5 = rand.Next(1, 11)
intOP1 = rand.Next(1, 11)
intOP2 = rand.Next(1, 11)
intOP3 = rand.Next(1, 11)
Also in your hit button you need to put "lblCounter.Text = lblCounter.Text + 1" at the bottom of the event.
I changed some of your code already ,so here you have it.
Dim rand As New Random
Dim intM1 As Integer
Dim intM2 As Integer
Dim intM3 As Integer
Dim intM4 As Integer
Dim intM5 As Integer
Dim intOP1 As Integer
Dim intOP2 As Integer
Dim intOP3 As Integer
Dim intOP4 As Integer
Dim intOP5 As Integer
Private Sub butPlay_Click(sender As System.Object, e As System.EventArgs) Handles butPlay.Click
intM1 = rand.Next(1, 11)
intM2 = rand.Next(1, 11)
intM3 = rand.Next(1, 11)
intM4 = rand.Next(1, 11)
intM5 = rand.Next(1, 11)
intOP1 = rand.Next(1, 11)
intOP2 = rand.Next(1, 11)
intOP3 = rand.Next(1, 11)
lblPlayV.Text = 1
lblCounter.Text = 1
butPlay.Text = "Replay"
lblM1.Text = intM1
lblM2.Text = intM2
lblM3.Text = intM3
lblOP1.Text = intOP1
lblOP2.Text = intOP2
lblOP3.Text = intOP3
lblM1.Visible = True
lblM2.Visible = True
lblM3.Visible = False
lblM4.Visible = False
lblM5.Visible = False
End Sub
Private Sub butHit_Click(sender As System.Object, e As System.EventArgs) Handles butHit.Click
If lblPlayV.Text = 1 Then
If lblCounter.Text = 1 Then
lblM3.Visible = True
If intM1 + intM2 + intM3 = 21 Then
If intOP1 + intOP2 + intOP3 = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If
ElseIf intM1 + intM2 + intM3 > 21 Then
If intOP1 + intOP2 + intOP3 > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If
Else
End If
End If
'<<<<<<<<<<<<<<<<<<<<<<'
If lblCounter.Text = 2 Then
lblM4.Visible = True
If intM1 + intM2 + intM3 + intM4 = 21 Then
If intOP1 + intOP2 + intOP3 = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If
ElseIf intM1 + intM2 + intM3 + intM4 > 21 Then
If intOP1 + intOP2 + intOP3 > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If
Else
End If
End If
'<<<<<<<<<<<<<<<<<<<<<<'
If lblCounter.Text = 3 Then
lblM5.Visible = True
If intM1 + intM2 + intM3 + intM4 + intM5 = 21 Then
If intOP1 + intOP2 + intOP3 = 21 Then
MsgBox("21 : Draw")
Else
MsgBox("21 : You Win")
End If
ElseIf intM1 + intM2 + intM3 + intM4 + intM5 > 21 Then
If intOP1 + intOP2 + intOP3 > 21 Then
MsgBox("Bust : Draw")
Else
MsgBox("Bust : You Lose")
End If
Else
If intM1 + intM2 + intM3 + intM4 + intM5 = intOP1 + intOP2 + intOP3 Then
MsgBox("Draw")
ElseIf intM1 + intM2 + intM3 + intM4 + intM5 > intOP1 + intOP2 + intOP3 Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
lblCounter.Text = 0
lblPlayV.Text = 0
End If
'<<<<<<<<<<<<<<<<<<<<<<'
End If
lblCounter.Text = lblCounter.Text + 1
End Sub
Private Sub ButStand_Click(sender As System.Object, e As System.EventArgs) Handles ButStand.Click
If lblPlayV.Text = 1 Then
lblPlayV.Text = 0
If lblM3.Visible = False Then
If intM1 + intM2 = intOP1 + intOP2 + intOP3 Then
MsgBox("Tie")
ElseIf intM1 + intM2 > intOP1 + intOP2 + intOP3 Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
If lblCounter.Text = 1 Then
If intM1 + intM2 + intM3 = intOP1 + intOP2 + intOP3 Then
MsgBox("Tie")
ElseIf intM1 + intM2 + intM3 > intOP1 + intOP2 + intOP3 Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
If lblCounter.Text = 2 Then
If intM1 + intM2 + intM3 + intM4 = intOP1 + intOP2 + intOP3 Then
MsgBox("Tie")
ElseIf intM1 + intM2 + intM3 + intM4 > intOP1 + intOP2 + intOP3 Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
If lblCounter.Text = 3 Then
If intM1 + intM2 + intM3 + intM4 + intM5 = intOP1 + intOP2 + intOP3 Then
MsgBox("Tie")
ElseIf intM1 + intM2 + intM3 + intM4 + intM5 > intOP1 + intOP2 + intOP3 Then
MsgBox("You Win")
Else
MsgBox("You Lose")
End If
End If
End If
End Sub
Related
I have a problem with sparklines in my macro in VBA in excel.
In the debug, step through mode it works fine, however on RUN the sparklines are completely wrong, they are in the same areas, but seem to cover other data.
I have no idea what may be the problem at this point.
Here is the code:
* the pivot tables copied are (private) variables defined earlier
Sub DashboardMarket()
Dim ws As Worksheet
Dim lastColD As Integer, lastColD2 As Integer, lastRowD As Integer
Dim ptDM1 As PivotTable, ptDM2 As PivotTable, ptDM3 As PivotTable, ptDM4 As
PivotTable
Application.DisplayAlerts = False
With ThisWorkbook
If DoesSheetExist("Dashboard Market") = True Then
If MsgBox("Do you wish to update existing Market Dashboard?", vbYesNo, "Confirm") = vbYes Then
.Sheets("Dashboard Market").Delete
.Sheets.Add(Before:=Sheets("data")).Name = "Dashboard Market"
Else: Exit Sub
End If
Else: .Sheets.Add(Before:=Sheets("data")).Name = "Dashboard Market"
End If
End With
Application.DisplayAlerts = True
Set ws = ThisWorkbook.Sheets("Dashboard Market")
ws.Select
scdpt4.TableRange2.Cells.Resize(, scdpt4.TableRange2.Columns.Count + 2).Copy
Destination:=ws.Cells(5, 2)
lastColD = 1 + scdpt4.TableRange2.Columns.Count + 2
lastRowD = 4 + scdpt4.TableRange2.Rows.Count
Set ptDM1 = ws.Cells(5, 2).PivotTable
'create sparklines
Range(Cells(ptDM1.DataBodyRange.Cells(1, 1).Row, lastColD), Cells(lastRowD, lastColD)).SparklineGroups.Add Type:=xlSparkLine, _
SourceData:=Range(ptDM1.DataBodyRange.Cells(1, 2), ws.Cells(lastRowD, lastColD - 2)).Address
'format sparklines
With Range(Cells(ptDM1.DataBodyRange.Cells(1, 1).Row, lastColD), Cells(lastRowD, lastColD)).SparklineGroups.Item(1)
.LineWeight = 1.1
.Points.Markers.Visible = True
.Points.Markers.Color.ColorIndex = 3
.SeriesColor.ColorIndex = 32
.SeriesColor.TintAndShade = 0
End With
scdpt5.TableRange2.Cells.Resize(, scdpt5.TableRange2.Columns.Count + 2).Copy
Destination:=ws.Cells(lastRowD + 2, 2)
Set ptDM2 = ws.Cells(lastRowD + 2, 2).PivotTable
lastRowD = 1 + lastRowD + scdpt5.TableRange2.Rows.Count
'create sparklines
Range(Cells(ptDM2.DataBodyRange.Cells(1, 1).Row, lastColD), Cells(lastRowD, lastColD)).SparklineGroups.Add Type:=xlSparkLine, _
SourceData:=Range(ptDM2.DataBodyRange.Cells(1, 2), Cells(lastRowD, lastColD - 2)).Address
'format sparklines
With Range(Cells(ptDM2.DataBodyRange.Cells(1, 1).Row, lastColD), ws.Cells(lastRowD, lastColD)).SparklineGroups.Item(1)
.LineWeight = 1.1
.Points.Markers.Visible = True
.Points.Markers.Color.ColorIndex = 3
.SeriesColor.ColorIndex = 32
.SeriesColor.TintAndShade = 0
End With
pt1.TableRange2.Copy Destination:=ws.Cells(5, lastColD + 2)
lastRowD = 4 + pt1.TableRange2.Rows.Count
pt2.TableRange2.Copy Destination:=ws.Cells(lastRowD + 2, lastColD + 2)
lastRowD = 1 + lastRowD + pt2.TableRange2.Rows.Count
pt3.TableRange2.Copy Destination:=ws.Cells(lastRowD + 2, lastColD + 2)
lastRowD = 1 + lastRowD + pt3.TableRange2.Rows.Count
pt4.TableRange2.Copy Destination:=ws.Cells(lastRowD + 2, lastColD + 2)
lastRowD = 1 + lastRowD + pt4.TableRange2.Rows.Count
pt5.TableRange2.Copy Destination:=ws.Cells(lastRowD + 2, lastColD + 2)
lastRowD = 1 + lastRowD + pt5.TableRange2.Rows.Count
lastColD = 1 + lastColD + pt1.TableRange2.Columns.Count
With ws.Range(Cells(3, 2), Cells(3, lastColD))
.MergeCells = True
.Value = "COMMUNICATED DISCOUNTS"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Font.Bold = True
.Interior.ColorIndex = 16
.Font.ColorIndex = 2
.Font.Size = 16
End With
pp_pt1.TableRange2.Cells.Resize(, pp_pt1.TableRange2.Columns.Count + 2).Copy Destination:=ws.Cells(5, lastColD + 2)
lastRowD = 4 + pp_pt1.TableRange2.Rows.Count
lastColD2 = 1 + lastColD + pp_pt1.TableRange2.Columns.Count + 2
Set ptDM3 = ws.Cells(5, lastColD + 2).PivotTable
'create sparklines
Range(Cells(ptDM3.DataBodyRange.Cells(1, 1).Row, lastColD2), Cells(lastRowD, lastColD2)).SparklineGroups.Add Type:=xlSparkLine, _
SourceData:=Range(ptDM3.DataBodyRange.Cells(1, 2), ws.Cells(lastRowD, lastColD2 - 2)).Address
'format sparklines
With Range(Cells(ptDM3.DataBodyRange.Cells(1, 1).Row, lastColD2), Cells(lastRowD, lastColD2)).SparklineGroups.Item(1)
.LineWeight = 1.1
.Points.Markers.Visible = True
.Points.Markers.Color.ColorIndex = 3
.SeriesColor.ColorIndex = 32
.SeriesColor.TintAndShade = 0
End With
pp_pt2.TableRange2.Cells.Resize(, pp_pt2.TableRange2.Columns.Count + 2).Copy Destination:=ws.Cells(lastRowD + 2, lastColD + 2)
Set ptDM4 = ws.Cells(lastRowD + 2, lastColD + 2).PivotTable
lastRowD = 1 + lastRowD + pp_pt2.TableRange2.Rows.Count
'create sparklines
Range(Cells(ptDM4.DataBodyRange.Cells(1, 1).Row, lastColD2), Cells(lastRowD, lastColD2)).SparklineGroups.Add Type:=xlSparkLine, _
SourceData:=Range(ptDM4.DataBodyRange.Cells(1, 2), ws.Cells(lastRowD, lastColD2 - 2)).Address
'format sparklines
With Range(Cells(ptDM4.DataBodyRange.Cells(1, 1).Row, lastColD2), Cells(lastRowD, lastColD2)).SparklineGroups.Item(1)
.LineWeight = 1.1
.Points.Markers.Visible = True
.Points.Markers.Color.ColorIndex = 3
.SeriesColor.ColorIndex = 32
.SeriesColor.TintAndShade = 0
End With
With ws.Range(Cells(3, lastColD + 2), Cells(3, lastColD2))
.MergeCells = True
.Value = "PROMO PRESSURE"
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.Font.Bold = True
.Interior.ColorIndex = 32
.Font.ColorIndex = 2
.Font.Size = 16
End With
With ws.Range(Cells(1, 1), Cells(1, lastColD2))
.VerticalAlignment = xlCenter
.Interior.ColorIndex = 32
.Font.ColorIndex = 2
End With
ActiveWindow.DisplayZeros = False
ws.UsedRange.EntireColumn.AutoFit
End Sub
Totally forgot to post about it, but the solution I came up with was as easy as they come, I just selected the data body of the new copy of a pivot table, so: pt.DataBodyRange.Select Before creating sparklines, every time it is done. Does anybody know what was the cause and solution without using Selection?
Hello Everyone Good Morning.
I have a Program in VB.Net that will Populate Data from Mysql into the Datagridview.
I have also a button called Export and It will Export Datagridview Data in Excel Format like this.
But my our Prof. likes this Format.
How can I achieve this?
Put a Center Header
Put a .00 at the End of the Number of a Number Column
Find the Last Cell in a Column and Sum It.
I hope someone would help me.
Here is my code in Export
If DataGridView1.Rows.Count = 0 Then
MsgBox("Nothing to Export")
Else
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
For Each column As DataGridViewColumn In DataGridView1.Columns
.cells(1, column.Index + 1) = column.HeaderText
Next
For i = 1 To Me.DataGridView1.RowCount
.cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("ItemCode").Value
For j = 1 To DataGridView1.Columns.Count - 1
.cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
Next
Next
End With
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End If
Try this code :
If DataGridView1.Rows.Count = 0 Then
MsgBox("Nothing to Export")
Else
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
Dim rowIndex As Integer = 1
Dim total As Double = 0
Dim indexTotal As Integer
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
With .Range(.Cells(rowIndex, 1), .Cells(rowIndex, DataGridView1.Columns.Count))
.HorizontalAlignment = Excel.Constants.xlCenter
.VerticalAlignment = Excel.Constants.xlCenter
.MergeCells = True
.Value = "PURCHASE REQUISITION"
.Font.Bold = True
End With
rowIndex += 2
For Each column As DataGridViewColumn In DataGridView1.Columns
.cells(rowIndex, column.Index + 1) = column.HeaderText
Next
.Range(.Cells(rowIndex, 1), .Cells(rowIndex, DataGridView1.Columns.Count)).Font.Bold = True
rowIndex += 1
For i = 0 To Me.DataGridView1.RowCount - 1
.cells(rowIndex, 1) = Me.DataGridView1.Rows(i).Cells("ItemCode").Value
For j = 1 To DataGridView1.Columns.Count - 1
If IsNumeric(DataGridView1.Rows(i).Cells(j).Value) Then
.cells(rowIndex, j + 1).NumberFormat = "#,##0.00"
.cells(rowIndex, j + 1) = DataGridView1.Rows(i).Cells(j).Value
Else
.cells(rowIndex, j + 1) = DataGridView1.Rows(i).Cells(j).Value
End If
'You can test also by index for example : if j = indexofTotalColumn then
If DataGridView1.Columns(j).Name = "Total" Then
total += DataGridView1.Rows(i).Cells(j).Value
indexTotal = j
End If
Next
rowIndex += 1
Next
.cells(rowIndex, indexTotal) = "Grand Total"
.cells(rowIndex, indexTotal + 1).NumberFormat = "#,##0.00"
.cells(rowIndex, indexTotal + 1) = total
.Range(.Cells(rowIndex, indexTotal), .Cells(rowIndex, indexTotal + 1)).Font.Bold = True
End With
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End If
Hi I am using the following code to print a datagridview. It works but it prints every single column, even the ones that are not visible on the form. Is there a way to make it so it only prints visible columns. Thanks.
Private Structure pageDetails
Dim columns As Integer
Dim rows As Integer
Dim startCol As Integer
Dim startRow As Integer
End Structure
Private pages As Dictionary(Of Integer, pageDetails)
Dim maxPagesWide As Integer
Dim maxPagesTall As Integer
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
''this removes the printed page margins
PrintDocument1.OriginAtMargins = True
PrintDocument1.DefaultPageSettings.Margins = New Drawing.Printing.Margins(0, 0, 0, 0)
pages = New Dictionary(Of Integer, pageDetails)
Dim maxWidth As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width) - 40
Dim maxHeight As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Height) - 40 + Label1.Height
Dim pageCounter As Integer = 0
pages.Add(pageCounter, New pageDetails)
Dim columnCounter As Integer = 0
Dim columnSum As Integer = DataGridView1.RowHeadersWidth
For c As Integer = 0 To DataGridView1.Columns.Count - 1
If columnSum + DataGridView1.Columns(c).Width < maxWidth Then
columnSum += DataGridView1.Columns(c).Width
columnCounter += 1
Else
pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol}
columnSum = DataGridView1.RowHeadersWidth + DataGridView1.Columns(c).Width
columnCounter = 1
pageCounter += 1
pages.Add(pageCounter, New pageDetails With {.startCol = c})
End If
If c = DataGridView1.Columns.Count - 1 Then
If pages(pageCounter).columns = 0 Then
pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol}
End If
End If
Next
maxPagesWide = pages.Keys.Max + 1
pageCounter = 0
Dim rowCounter As Integer = 0
Dim rowSum As Integer = DataGridView1.ColumnHeadersHeight
For r As Integer = 0 To DataGridView1.Rows.Count - 2
If rowSum + DataGridView1.Rows(r).Height < maxHeight Then
rowSum += DataGridView1.Rows(r).Height
rowCounter += 1
Else
pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow}
For x As Integer = 1 To maxPagesWide - 1
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow}
Next
pageCounter += maxPagesWide
For x As Integer = 0 To maxPagesWide - 1
pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = r})
Next
rowSum = DataGridView1.ColumnHeadersHeight + DataGridView1.Rows(r).Height
rowCounter = 1
End If
If r = DataGridView1.Rows.Count - 2 Then
For x As Integer = 0 To maxPagesWide - 1
If pages(pageCounter + x).rows = 0 Then
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow}
End If
Next
End If
Next
maxPagesTall = pages.Count \ maxPagesWide
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim rect As New Rectangle(20, 20, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label1.Height)
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, rect, sf)
sf.Alignment = StringAlignment.Near
Dim startX As Integer = 50
Dim startY As Integer = rect.Bottom
Static startPage As Integer = 0
For p As Integer = startPage To pages.Count - 1
Dim cell As New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.ColumnHeadersHeight)
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
e.Graphics.DrawRectangle(Pens.Black, cell)
startY += DataGridView1.ColumnHeadersHeight
For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
cell = New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.Rows(r).Height)
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
e.Graphics.DrawRectangle(Pens.Black, cell)
e.Graphics.DrawString(DataGridView1.Rows(r).HeaderCell.Value, DataGridView1.Font, Brushes.Black, cell, sf)
startY += DataGridView1.Rows(r).Height
Next
startX += cell.Width
startY = rect.Bottom
For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.ColumnHeadersHeight)
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
e.Graphics.DrawRectangle(Pens.Black, cell)
e.Graphics.DrawString(DataGridView1.Columns(c).HeaderCell.Value, DataGridView1.Font, Brushes.Black, cell, sf)
startX += DataGridView1.Columns(c).Width
Next
startY = rect.Bottom + DataGridView1.ColumnHeadersHeight
For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
startX = 50 + DataGridView1.RowHeadersWidth
For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.Rows(r).Height)
e.Graphics.DrawRectangle(Pens.Black, cell)
e.Graphics.DrawString(DataGridView1(c, r).Value, DataGridView1.Font, Brushes.Black, cell, sf)
startX += DataGridView1.Columns(c).Width
Next
startY += DataGridView1.Rows(r).Height
Next
If p <> pages.Count - 1 Then
startPage = p + 1
e.HasMorePages = True
Return
Else
startPage = 0
End If
Next
End Sub
Private Sub PrintAMToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintAMToolStripMenuItem.Click
PrintDocument1.DefaultPageSettings.Landscape = True
PrintDocument1.Print()
End Sub
I added If condition to your PrintDocument1_PrintPage and your PrintDocument1_BeginPrint events to check if the column is visible each time there is a loop against your columns:
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
''this removes the printed page margins
PrintDocument1.OriginAtMargins = True
PrintDocument1.DefaultPageSettings.Margins = New Drawing.Printing.Margins(0, 0, 0, 0)
pages = New Dictionary(Of Integer, pageDetails)
Dim maxWidth As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width) - 40
Dim maxHeight As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Height) - 40 + Label1.Height
Dim pageCounter As Integer = 0
pages.Add(pageCounter, New pageDetails)
Dim columnCounter As Integer = 0
Dim columnSum As Integer = DataGridView1.RowHeadersWidth
For c As Integer = 0 To DataGridView1.Columns.Count - 1
If columnSum + DataGridView1.Columns(c).Width < maxWidth Then
columnSum += DataGridView1.Columns(c).Width
columnCounter += 1
Else
If DataGridView1.Columns(c).Visible = True Then
pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol}
columnSum = DataGridView1.RowHeadersWidth + DataGridView1.Columns(c).Width
columnCounter = 1
pageCounter += 1
pages.Add(pageCounter, New pageDetails With {.startCol = c})
End If
End If
If c = DataGridView1.Columns.Count - 1 Then
If pages(pageCounter).columns = 0 Then
pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol}
End If
End If
Next
maxPagesWide = pages.Keys.Max + 1
pageCounter = 0
Dim rowCounter As Integer = 0
Dim rowSum As Integer = DataGridView1.ColumnHeadersHeight
For r As Integer = 0 To DataGridView1.Rows.Count - 2
If rowSum + DataGridView1.Rows(r).Height < maxHeight Then
rowSum += DataGridView1.Rows(r).Height
rowCounter += 1
Else
pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow}
For x As Integer = 1 To maxPagesWide - 1
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow}
Next
pageCounter += maxPagesWide
For x As Integer = 0 To maxPagesWide - 1
pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = r})
Next
rowSum = DataGridView1.ColumnHeadersHeight + DataGridView1.Rows(r).Height
rowCounter = 1
End If
If r = DataGridView1.Rows.Count - 2 Then
For x As Integer = 0 To maxPagesWide - 1
If pages(pageCounter + x).rows = 0 Then
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow}
End If
Next
End If
Next
maxPagesTall = pages.Count \ maxPagesWide
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim rect As New Rectangle(20, 20, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label1.Height)
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, rect, sf)
sf.Alignment = StringAlignment.Near
Dim startX As Integer = 50
Dim startY As Integer = rect.Bottom
Static startPage As Integer = 0
For p As Integer = startPage To pages.Count - 1
Dim cell As New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.ColumnHeadersHeight)
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
e.Graphics.DrawRectangle(Pens.Black, cell)
startY += DataGridView1.ColumnHeadersHeight
For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
cell = New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.Rows(r).Height)
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
e.Graphics.DrawRectangle(Pens.Black, cell)
e.Graphics.DrawString(DataGridView1.Rows(r).HeaderCell.Value, DataGridView1.Font, Brushes.Black, cell, sf)
startY += DataGridView1.Rows(r).Height
Next
startX += cell.Width
startY = rect.Bottom
For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
If DataGridView1.Columns(c).Visible = True Then
cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.ColumnHeadersHeight)
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
e.Graphics.DrawRectangle(Pens.Black, cell)
e.Graphics.DrawString(DataGridView1.Columns(c).HeaderCell.Value, DataGridView1.Font, Brushes.Black, cell, sf)
startX += DataGridView1.Columns(c).Width
End If
Next
startY = rect.Bottom + DataGridView1.ColumnHeadersHeight
For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
startX = 50 + DataGridView1.RowHeadersWidth
For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
If DataGridView1.Columns(c).Visible = True Then
cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.Rows(r).Height)
e.Graphics.DrawRectangle(Pens.Black, cell)
e.Graphics.DrawString(DataGridView1(c, r).Value, DataGridView1.Font, Brushes.Black, cell, sf)
startX += DataGridView1.Columns(c).Width
End If
Next
startY += DataGridView1.Rows(r).Height
Next
If p <> pages.Count - 1 Then
startPage = p + 1
e.HasMorePages = True
Return
Else
startPage = 0
End If
Next
End Sub
I have read many solutions but none have worked... My code has been working fine for 2 weeks but now it is failing at the following line:
Sheets("Order Forms").Range(Cells(25 + intAbove, intCol - 3), Cells(25 + intAbove, intCol + 1)).Insert Shift:=xlDown
I have tried re-writing it by using Select and then Selection but nothing worked. It still works sometimes if I run some other macros first. I get both errors mentioned in the subject.
Here's my entire procedure if that helps...
Sub Modify_List(ByVal intNewVal As Integer, ByVal intOldVal As Integer, strType As String, intCol As Integer)
Dim intA1 As Integer
Dim intA2 As Integer
Dim intAbove As Integer
Dim i As Integer
Application.ScreenUpdating = False
Select Case strType
Case "Standard User Phones - Cisco 7965"
intAbove = 0
intCol = intCol + 1
Case "Public Space Phones - Cisco 7965"
intAbove = Cells(17, intCol - 1).Value
If intAbove > 0 Then
intAbove = intAbove + 2
End If
Case "Public Space Phones - Cisco 8831"
intA1 = Cells(17, intCol - 1).Value
intA2 = Cells(17, intCol).Value
If intA1 + intA2 = 0 Then
intAbove = 0
ElseIf intA1 = 0 Then
intAbove = intA2 + 2
ElseIf intA2 = 0 Then
intAbove = intA1 + 2
Else
intAbove = intA1 + intA2 + 4
End If
End Select
Select Case intNewVal
Case Is = intOldVal
'do nothing
Case Is < intOldVal
'remove rows
If intNewVal = 0 Then
'remove header and lines
Range(Cells(25 + intAbove, intCol - 3), Cells(26 + intAbove + intOldVal, intCol + 1)).Delete Shift:=xlUp
Cells(20, intCol).Select
Else
'remove ending lines
Range(Cells(26 + intAbove + intNewVal + 1, intCol - 3), Cells(26 + intAbove + intOldVal, intCol + 1)).Delete Shift:=xlUp
Cells(26 + intAbove + intNewVal, intCol - 2).Select
End If
Case Is > intOldVal
'add rows
If intOldVal = 0 Then
'add header and lines
SheetAU.Range("B1").Value = strType
SheetAU.Range("A1:E2").Copy
'ActiveWorkbook.Sheets("Order Forms").Activate
**Sheets("Order Forms").Range(Cells(25 + intAbove, intCol - 3), Cells(25 + intAbove, intCol + 1)).Insert Shift:=xlDown**
Application.CutCopyMode = False
For i = 1 To intNewVal
SheetAU.Range("A3:E3").Copy
Sheets("Order Forms").Range(Cells(26 + intAbove + i, intCol - 3), Cells(26 + intAbove + i, intCol + 1)).Insert Shift:=xlDown
Application.CutCopyMode = False
Cells(26 + intAbove + i, intCol - 3).Value = i
Next
Cells(27 + intAbove, intCol - 2).Select
Else
'insert extra lines
For i = intOldVal + 1 To intNewVal
SheetAU.Range("A3:E3").Copy
Sheets("Order Forms").Range(Cells(26 + intAbove + i, intCol - 3), Cells(26 + intAbove + i, intCol + 1)).Insert Shift:=xlDown
Application.CutCopyMode = False
Cells(26 + intAbove + i, intCol - 3).Value = i
Next
Cells(26 + intAbove + intOldVal + 1, intCol - 2).Select
End If
End Select
Application.ScreenUpdating = True
End Sub
The scenario is i want to populate the label data dynamically based o the size of the string.say if the string length is 100 ,first 30 characters must be populated in first label,the remaining 30 on next label and so on.
what i have done so far
Dim remcontent, remcontentlength, remcontentlabelcount, labelcounter As New Integer
Dim rc As Integer = 30
Dim remcontentstring As String
remcontentlength = row.Item(4).ToString.Length
Dim endindex As Integer = 30
Dim startindex As Integer = 0
If remcontentlength > 30 Then
Dim actualremcontent As String
actualremcontent = "Note :" + row.Item(4).ToString
Dim modcheck As Integer
modcheck = remcontentlength Mod 30
If modcheck = 0 Then
remcontentlabelcount = (remcontentlength / 30)
Else
remcontentlabelcount = (Math.Floor(remcontentlength \ 30)) + 1
End If
ReDim remindercontantmulti(remcontentlabelcount)
For labelcounter = 1 To remcontentlabelcount
remindercontantmulti(labelcounter) = New Label
With remindercontantmulti(labelcounter)
.Name = "remindercontant " + x.ToString + labelcounter.ToString
.Text = actualremcontent.ToString.Substring(startindex, endindex)
.Visible = True
.Top = (22 * (x + (aa + 1))) + 10
.AutoSize = True
'.Left = reminderTime(x).Left + reminderTime(x).Width + 20
.BackColor = Color.Transparent
'.BorderStyle = BorderStyle.FixedSingle
.TextAlign = ContentAlignment.MiddleRight
.Font = New Drawing.Font("Trebuchet MS", 8, FontStyle.Regular)
Me.Panel4.Controls.Add(remindercontantmulti(labelcounter))
aa = aa + 1
End With
If Not startindex = 0 Then
startindex = startindex + rc
If (rc * labelcounter) < remcontentlength Then
endindex = remcontentlength - startindex
End If
End If
Next labelcounter
Else
remindercontant(x) = New Label
With remindercontant(x)
.Name = "remindercontant " + x.ToString
.Text = "Note :" + row.Item(4).ToString
.Visible = True
.Top = (22 * (x + (aa + 1))) + 10
.AutoSize = True
'.Left = reminderTime(x).Left + reminderTime(x).Width + 20
.BackColor = Color.Transparent
'.BorderStyle = BorderStyle.FixedSingle
.TextAlign = ContentAlignment.MiddleRight
.Font = New Drawing.Font("Trebuchet MS", 8, FontStyle.Regular)
Me.Panel4.Controls.Add(remindercontant(x))
aa = aa + 1
End With
End If