I try to remove empty spaces using with StringSplitOptions.RemoveEmptyEntries before print the text but when I run the codes "The index was outside the bounds of the array" appears on the screen as an error.
Could you please help to solve that problem?
Public Class Form2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.ShowDialog()
TextBox2.Text = OpenFileDialog1.FileName
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim myfile As String = TextBox2.Text
Dim allines As String() = IO.File.ReadAllLines(myfile)
For Each line As String In allines
ListBox1.Items.Add(line)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sayı As Integer = TextBox3.Text
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If (TextBox3.Text = "") Then
MessageBox.Show("Please enter number")
TextBox2.Text = ""
Else
If TextBox1.TextLength = TextBox3.Text Then
Dim fields() As String = ListBox1.Text.Split(";")
Dim idx As Integer = ListBox1.FindString(TextBox1.Text)
If idx <> -1 Then
ListBox1.SelectedIndex = idx
ListBox1.SelectedIndex.ToString(fields(0))
ListBox2.Items.Add(ListBox1.Text)
TextBox1.Text = ""
PrintDocument1.Print()
End If
End If
End If
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim drawfont As New Font("arial", 16)
Dim drawbrush As New SolidBrush(Color.Black)
Dim fields() As String = ListBox1.Text.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
e.Graphics.DrawString(ListBox1.SelectedIndex.ToString(fields(0)), drawfont, drawbrush, 100, 50)
e.Graphics.DrawString(ListBox1.SelectedIndex.ToString(fields(1)), drawfont, drawbrush, 100, 70)
e.Graphics.DrawString(ListBox1.SelectedIndex.ToString(fields(2)), drawfont, drawbrush, 100, 90)
e.Graphics.DrawString(ListBox1.SelectedIndex.ToString(fields(3)), drawfont, drawbrush, 100, 110)
e.Graphics.DrawString(ListBox1.SelectedIndex.ToString(fields(4)), drawfont, drawbrush, 100, 130)
e.Graphics.DrawString(ListBox1.SelectedIndex.ToString(fields(7)), drawfont, drawbrush, 10, 20)
e.Graphics.DrawString(ListBox1.SelectedIndex.ToString(fields(10)), drawfont, drawbrush, 270, 20)
ListBox1.Items.Remove(TextBox1.Text)
End Sub
End Class
Related
As a new programmer in VB, I struggled to get my print routine working. Searching through a number of sources I developed the hybrid below which is working to print a simple .txt file. My question sounds like a simple one but I've learned nothing is simple in printing. How do I get the PrintPreviewDialog to close once the printing is complete?
Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click
Try
PrintPreviewDialog1.Document = PrintDocument1
PageSetupDialog1.PageSettings =
PrintDocument1.DefaultPageSettings
If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.DefaultPageSettings =
PageSetupDialog1.PageSettings
PrintPreviewDialog1.Size = New System.Drawing.Size(500, 600)
PrintPreviewDialog1.ShowDialog()
End If
Catch ex As Exception
MessageBox.Show("Printing Operation Failed" & vbCrLf &
ex.Message)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static MyNewAcctFile As String = IO.File.ReadAllText(strErrorFile)
Dim printFont As New Font("Arial", 14, FontStyle.Regular)
Dim charsFitted As Integer
Dim linesFilled As Integer
e.Graphics.MeasureString(MyNewAcctFile, printFont, New SizeF(e.MarginBounds.Width, e.MarginBounds.Height), Drawing.StringFormat.GenericTypographic, charsFitted, linesFilled)
e.Graphics.DrawString(MyNewAcctFile, printFont, Brushes.Black, e.MarginBounds, Drawing.StringFormat.GenericTypographic)
MyNewAcctFile = MyNewAcctFile.Substring(charsFitted)
If MyNewAcctFile <> "" Then
e.HasMorePages = True
Else
e.HasMorePages = False
MyNewAcctFile = IO.File.ReadAllText(strErrorFile)
End If
End Sub
I would expect to see something more like:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private FileName As String
Private txtFileContents As String
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
FileName = "C:\Users\mikes\Documents\SomeFile.txt"
txtFileContents = IO.File.ReadAllText(FileName)
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
If txtFileContents.Length > 0 Then
Using printFont As New Font("Arial", 14, FontStyle.Regular)
Dim charsFitted As Integer
Dim linesFilled As Integer
e.Graphics.MeasureString(txtFileContents, printFont, New SizeF(e.MarginBounds.Width, e.MarginBounds.Height), Drawing.StringFormat.GenericTypographic, charsFitted, linesFilled)
e.Graphics.DrawString(txtFileContents, printFont, Brushes.Black, e.MarginBounds, Drawing.StringFormat.GenericTypographic)
txtFileContents = txtFileContents.Substring(charsFitted)
e.HasMorePages = (txtFileContents.Length > 0)
End Using
End If
End Sub
Private Sub PrintDocument1_EndPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.EndPrint
If Not PrintDocument1.PrintController.IsPreview Then
PrintPreviewDialog1.Close()
End If
End Sub
End Class
I am adding all the textboxes and labels to display in the listview. When I click the clear button everything on my form clears as it should, but when I then want to add more items to the listview, nothing displays in the listview, and my header information is also cleared. Can someone please assist?
Public Class Form2
Dim decTotalDue As Decimal
Dim intTotalItems As Integer
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles txtUnitPrice.TextChanged
End Sub
Private Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click
Dim decUnitPrice As Decimal
Dim intQuantity As Integer
Dim decTotal As Decimal
Dim decTotalPayable As Decimal
Dim item As New ListViewItem
Decimal.TryParse(txtUnitPrice.Text, decUnitPrice)
Integer.TryParse(txtQuantity.Text, intQuantity)
decTotal = decUnitPrice * intQuantity
lblTotal.Text = decTotal.ToString("C2")
decTotalDue = decTotal + decTotalDue
lblTotalDue.Text = decTotalDue.ToString("C2")
intTotalItems = intQuantity + intTotalItems
lblTotalItems.Text = intTotalItems.ToString
decTotalPayable = decTotalDue
lblTotalPayable.Text = decTotalPayable.ToString("C2")
lblTotalPayable.Hide()
lblTotalItems.Hide()
item = ListView1.Items.Add(cboItemName.Text)
item.SubItems.Add(txtUnitPrice.Text)
item.SubItems.Add(txtQuantity.Text)
item.SubItems.Add(lblTotal.Text)
ListView1.ForeColor = Color.White
txtUnitPrice.Text = decUnitPrice.ToString("C2")
End Sub
Private Sub btnPurchase_Click(sender As Object, e As EventArgs) Handles btnPurchase.Click
lblTotalItems.Show()
lblTotalPayable.Show()
cboItemName.Text = String.Empty
txtUnitPrice.Clear()
txtQuantity.Clear()
lblTotal.Text = ""
lblTotalDue.Text = ""
ListView1.Clear()
End Sub
Private Sub btnCalculateChange_Click(sender As Object, e As EventArgs) Handles btnCalculateChange.Click
Dim decCashTenderted As Decimal
Dim decChange As Decimal
Decimal.TryParse(txtCashTendered.Text, decCashTenderted)
txtCashTendered.Text = decCashTenderted.ToString("C2")
decChange = decCashTenderted - decTotalDue
lblChange.Text = decChange.ToString("C2")
If decCashTenderted < decTotalDue Then
MessageBox.Show("Cash Tendered is less than Total Due", "Invalid", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning)
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
cboItemName.Text = String.Empty
txtCashTendered.Clear()
txtUnitPrice.Clear()
txtQuantity.Clear()
lblTotalDue.Text = ""
lblTotalItems.Text = ""
lblTotalPayable.Text = ""
lblChange.Text = ""
ListView1.Clear()
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
End Class
MSDN on the behaviour of the Clear function is:
You can use this method to remove all items and columns from the ListView control without having to call the individual Clear methods from the ListView.ColumnHeaderCollection and ListView.ListViewItemCollection classes.
From what you describe you want, you should be doing is calling:
ListView1.Items.Clear()
This will remove just the items that are displayed and not remove the column definitions.
I try to print selected index item but I couldnt succeed.
Could you please help me for fixing the codes?
I get "-1" value on the printed page.
Private Sub Textbox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
Dim idx As Integer = ListBox1.FindStringExact(TextBox2.Text)
If idx <> -1 Then
ListBox1.SelectedIndex = idx
ListBox2.Items.Add(ListBox1.Text)
ListBox1.Items.Remove(TextBox2.Text)
PrintDoc.Print()
End sub
Private Sub PrintDoc_PrintPage(ByVal sender As System.Object, ByVal e As Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
Dim drawfont As New Font("arial", 16)
Dim drawbrush As New SolidBrush(Color.Black)
e.Graphics.DrawString(ListBox1.SelectedIndex, drawfont, drawbrush, 100, 100)
End Sub
http://watchmynewvideoxyz.blogspot.in/2017/02/user3900603-your-answer.html
go there your code ready
if ok make me +1
I'm designing a program that calculates the average of scores. the program works great except when I try to add a name and a number from textbox into the textfile it returns an error. I believe that I have closed the file every time it has been used. my code is:
Public Class Form4
Public Function GetNumberOfLines(ByVal file_path As String) As Integer
Using sr As New StreamReader("C:\file.txt")
Dim NumberOfLines As Integer
Do While sr.Peek >= 0 'a pre test loop to read lines
sr.ReadLine()
NumberOfLines += 1
Loop
Return NumberOfLines
sr.Close() 'closes file after it is read
sr.Dispose()
End Using
End Function
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Hide()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Dim FILE_NAME As String = "C:\file.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Close()
objWriter.Write(TextBox5.Text)
MsgBox("Text written to file")
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Using sr As New System.IO.StreamReader("C:\file.txt")
Dim LofInt As New List(Of Integer)
While sr.Peek <> -1
Dim line As String = sr.ReadLine
Dim intValue As String = String.Empty
For Each c As Char In line
If IsNumeric(c) Then
intValue += c
End If
Next
LofInt.Add(intValue)
End While
sr.Close()
sr.Dispose()
LofInt.Sort()
For Each i In LofInt
Scores.Items.Add(i)
Next
Dim sum As Decimal
For Each decAdded As Decimal In Me.Scores.Items
sum += Double.Parse(decAdded)
Next
TextBox4.Text = sum
sr.Close()
sr.Dispose()
End Using
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\file.txt")
TextBox2.Text = GetNumberOfLines("C:\file.txt")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sum As Decimal
Dim avg As Decimal
For Each decAdded As Decimal In Me.Scores.Items
sum += Double.Parse(decAdded)
Next
avg = sum / (GetNumberOfLines("C:\file.txt"))
TextBox3.Text = avg
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Close()
End Sub
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
TextBox1.Clear()
TextBox2.Clear()
Scores.Items.Clear()
TextBox4.Clear()
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\file.txt")
TextBox2.Text = GetNumberOfLines("C:\file.txt")
Using sr As New System.IO.StreamReader("C:\file.txt")
Dim LofInt As New List(Of Integer)
While sr.Peek <> -1
Dim line As String = sr.ReadLine
Dim intValue As String = String.Empty
For Each c As Char In line
If IsNumeric(c) Then
intValue += c
End If
Next
LofInt.Add(intValue)
End While
LofInt.Sort()
For Each i In LofInt
Scores.Items.Add(i)
Next
Dim sum As Decimal
For Each decAdded As Decimal In Me.Scores.Items
sum += Double.Parse(decAdded)
Next
TextBox4.Text = sum
sr.Close()
sr.Dispose()
End Using
End Sub
Dim objWriter As New System.IO.StreamWriter(FILE_NAME) is where the error occurs
Hey all i have the following code that works just fine when my form loads up:
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim custFont As New PrivateFontCollection()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Dim string2 As String = "AntiAlias"
custFont.AddFontFile("C:\aFont.ttf")
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
e.Graphics.DrawString(string2, New Font(custFont.Families(0), 100, FontStyle.Regular, GraphicsUnit.Pixel), solidBrush, New PointF(10, 60))
End Sub
However, i need a way to update that font text whenever i push a button(s) on the form itself. I tried making a sub like so:
Public Sub changeText(ByVal e As System.Windows.Forms.PaintEventArgs, ByVal theText as string)
Dim custFont As New PrivateFontCollection()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
custFont.AddFontFile("C:\aFont.ttf")
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
e.Graphics.DrawString(theText, New Font(custFont.Families(0), 100, FontStyle.Regular, GraphicsUnit.Pixel), solidBrush, New PointF(10, 60))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
changeText(Me.OnPaint, "just a test")
End Sub
But i end up having an error:
Overload resolution failed because no accessible 'OnPaint' accepts this number of arguments.
on line:
changeText(Me.OnPaint, "just a test")
Any help would be great! Thanks!
Move the string out to class level so you can change it:
Imports System.Drawing.Text
Public Class Form1
Private string2 As String = "AntiAlias"
Private custFont As New PrivateFontCollection()
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
custFont.AddFontFile("C:\aFont.ttf")
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Using solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Using fnt As New Font(custFont.Families(0), 100, FontStyle.Regular, GraphicsUnit.Pixel)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
e.Graphics.DrawString(string2, fnt, solidBrush, New PointF(10, 60))
End Using
End Using
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
string2 = "just a test"
Me.Refresh()
End Sub
End Class