Print selected index item - vb.net

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

Related

How I can change the text of a single label from a string array vb.net?

so I want to display a string array in a single label that change the contents with a period of time ,
I ve tried every thing the timer, the background worker every thing , the problem when I use a loop inset a timer the interval in the start should be so long if the array items was so many so I tried the background worker but it not works
this is the code :
Dim array() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
Dim delay As Integer = 2000
Dim interval As Integer = 100
Dim elapsed As Integer = 0
Dim pos As Integer = array.Length
While Not worker.CancellationPending
If (elapsed >= delay) Then
worker.ReportProgress(pos)
' change label text in the Progress event handler
pos = (pos + 1)
elapsed = 0
If (pos = array.Length) Then
Exit While
End If
End If
Thread.Sleep(interval)
End While
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim j As Integer
For j = 0 To array.Length
Label1.Text = array(j)
Next
End Sub
Your for loop is overwriting the .Text property of the lable on each iteration. You will also get an Index Out of Range exception because the indexes of an array are zero based. The highest index will be 1 less than the .Length.
Private arStr() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim LabelText = String.Join(Environment.NewLine, arStr)
Label1.Text = LabelText
End Sub
EDIT
Private arStr() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static index As Integer
If index < arStr.Length Then
Label1.Text &= arStr(index) & Environment.NewLine
index += 1
End If
End Sub
With asynchronous approach you can display array values one after another without BackgroundWorker and explicitly created Timer.
Private _values As New String() From {"so", "nb", "de", "rty", "dcds"}
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each value In _values
await Task.Delay(2000)
Next
End Sub
With asynchronous approach you can prevent button click before all values are displayed in the simple way as you would do in synchronous code.
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim button = DirectCast(sender, Button);
button.Enabled = False
For Each value In _values
await Task.Delay(2000)
Next
button.Enabled = True
End Sub

How to save an image from Picturebox that load from camera?

I create a simple program that getting image from camera and display it in picturebox using a Aforge library. However, when I try to save the image from picture box. It prompt an error like this " 'System.Runtime.InteropServices.ExternalException' in System.Drawing.dll. A generic error occurred in GDI+. "
I did some research but not found any helpful solution.
Below are my following code use to save the image:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox1.Image.Save("C:\a.jpeg", ImageFormat.Jpeg)
End Sub
Updated Code :
Imports AForge
Imports AForge.Video
Imports AForge.Video.DirectShow
Imports AForge.Imaging.Filters
Imports AForge.Imaging
Imports System.Drawing.Imaging
Public Class Form1
Dim Filter As FilterInfoCollection
Dim Camera As VideoCaptureDevice
Dim MINR As Integer = 0
Dim MING As Integer = 0
Dim MINB As Integer = 0
Dim MAXR As Integer = 255
Dim MAXG As Integer = 255
Dim MAXB As Integer = 255
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Filter = New FilterInfoCollection(FilterCategory.VideoInputDevice)
If Filter.Count > 0 Then
For Each ITEM In Filter
ComboBox1.Items.Add(ITEM.Name.ToString())
Next
CheckForIllegalCrossThreadCalls = False
Me.Location = New System.Drawing.Point(Me.Location.X, 0)
Else
MsgBox("NO Camera Found")
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Camera = New VideoCaptureDevice(Filter(ComboBox1.SelectedIndex).MonikerString)
AddHandler Camera.NewFrame, New NewFrameEventHandler(AddressOf Video_NewFrame)
Camera.Start()
ComboBox1.Visible = False
End Sub
Private Sub Video_NewFrame(sender As Object, eventArgs As AForge.Video.NewFrameEventArgs)
Dim ORIGINAL As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap)
Dim FILTER As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap)
Dim CFILTER As New ColorFiltering
CFILTER.Red = New IntRange(MINR, MAXR)
CFILTER.Green = New IntRange(MING, MAXG)
CFILTER.Blue = New IntRange(MINB, MAXB)
CFILTER.ApplyInPlace(FILTER)
Dim GRAY As Grayscale = Grayscale.CommonAlgorithms.BT709
Dim IMAGING As Bitmap = GRAY.Apply(FILTER)
Dim BLOBS As New BlobCounter()
BLOBS.MinHeight = 10
BLOBS.MinWidth = 10
BLOBS.ObjectsOrder = ObjectsOrder.Size
BLOBS.ProcessImage(IMAGING)
Dim Rectangle As Rectangle() = BLOBS.GetObjectsRectangles()
If Rectangle.Count > 0 Then
Dim Rectangle2 As Rectangle = Rectangle(0)
Dim STYLE As Graphics = Graphics.FromImage(ORIGINAL)
Dim CLR As New Pen(Color.Lime, 5)
STYLE.DrawRectangle(CLR, Rectangle2)
STYLE.Dispose()
End If
PictureBoxDefault.Image = ORIGINAL '
PictureBoxFilter.Image = FILTER
End Sub
Private Sub TrackBarMINR_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMINR.Scroll
MINR = TrackBarMINR.Value
LabelMINR.Text = "MINR: " & MINR
End Sub
Private Sub TrackBarMING_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMING.Scroll
MING = TrackBarMING.Value
LabelMING.Text = "MING: " & MING
End Sub
Private Sub TrackBarMINB_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMINB.Scroll
MINB = TrackBarMINB.Value
LabelMINB.Text = "MINB: " & MINB
End Sub
Private Sub TrackBarMAXR_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMAXR.Scroll
MAXR = TrackBarMAXR.Value
LabelMAXR.Text = "MAXR: " & MAXR
End Sub
Private Sub TrackBarMAXG_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMAXG.Scroll
MAXG = TrackBarMAXG.Value
LabelMAXG.Text = "MAXG: " & MAXG
End Sub
Private Sub TrackBarMAXB_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMAXB.Scroll
MAXB = TrackBarMAXB.Value
LabelMAXB.Text = "MAXB: " & MAXB
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
Camera.SignalToStop()
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnSave.Click
PictureBox1.Image.Save("D:\a.png", ImageFormat.Png)
End Sub
End Class
I had the same issue.
The code below is now working for me.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim img As Bitmap = PictureBox1.Image.Clone
img.Save("Z:\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
End Sub
Just change "Z:\test.bmp" to your directory, haven't tried any other image formats but this is the only way I have gotten it to work so far.

Add items to listview after cleared

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.

Remove empty spaces in delimited text (using StringSplitOptions.RemoveEmptyEntries)

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

How to stop the timer after specified time

I have 3 texbox which will continuously running the random numbers.
May I know how to stop the timer after certain time?
Example
Timer1 stop after 5 seconds
Timer2 stop after 10 seconds
TImer2 stop after 15 seconds
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim num1 As Integer
Dim rnd As New Random()
num1 = rnd.[Next](0, 2)
TextBox1.Text = Convert.ToString(num1)
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Dim num1 As Integer
Dim rnd As New Random
num1 = rnd.[Next](1, 9)
TextBox2.Text = Convert.ToString(num1)
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
Dim num1 As Integer
Dim rnd As New Random
num1 = rnd.[Next](1, 9)
TextBox3.Text = Convert.ToString(num1)
End Sub
Private Sub btnrndpick_Click(sender As Object, e As EventArgs) Handles btnrndpick.Click
Timer1.Enabled = True
Timer2.Enabled = True
Timer3.Enabled = True
Timer4.Enabled = True
Timer5.Enabled = True
btnrndpick.Text = "RUNNING"
End Sub
Here is one way to do it with only 1 timer:
Private Rnd As New Random()
Private buttonPressTime As DateTime
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If (DateTime.Now - buttonPressTime) < New TimeSpan(0, 0, 2) Then
SetTextBoxText(TextBox1, 0, 2)
End If
If (DateTime.Now - buttonPressTime) < New TimeSpan(0, 0, 5) Then
SetTextBoxText(TextBox2, 1, 9)
End If
If (DateTime.Now - buttonPressTime) < New TimeSpan(0, 0, 15) Then
SetTextBoxText(TextBox3, 1, 9)
Else
Timer1.Stop()
End If
End Sub
Private Sub SetTextBoxText(ByVal TextBox As TextBox, ByVal LowerBound As Integer, ByVal UpperBound As Integer)
TextBox.Text = Rnd.[Next](LowerBound, UpperBound).ToString()
End Sub
Private Sub btnrndpick_Click(sender As Object, e As EventArgs) Handles btnrndpick.Click
Timer1.Enabled = True
buttonPressTime = DateTime.Now
btnrndpick.Text = "RUNNING"
End Sub
Have you tried using an integer variable?
Something like
Dim number as integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
number += 1
If number = 5 Then 'For example. But you need to adjust the tick property.
Timer1.Stop
End Sub