Only does two files instead of 100 - vb.net

Basically I have this file creator program which is helping me to make a bunch of files and for some reason, it only does 2 instead of 100 files specified.
Code:
Imports System.IO
Public Class Main
Dim CURRENT_NUM As Integer
Dim CONTENT_STRING As String
Dim PATH_STRING As String
Dim FROM_NUM As Integer
Dim TO_NUM As Integer
Public Sub SetupVar()
Try
CONTENT_STRING = tbContent.Text
PATH_STRING = tbPath.Text
FROM_NUM = Integer.Parse(tbFrom.Text)
TO_NUM = Integer.Parse(tbTo.Text)
Catch ex As Exception
End Try
End Sub
Private Sub Make()
tmrMake.Enabled = True
End Sub
Private Sub btnMake_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMake.Click
SetupVar()
Make()
End Sub
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub tmrMake_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMake.Tick
'disable quit
'and control box
Me.ControlBox = False
btnQuit.Enabled = False
CURRENT_NUM = FROM_NUM
CURRENT_NUM = CURRENT_NUM + 1
Using sw As New StreamWriter(PATH_STRING & CURRENT_NUM & ".txt")
sw.Write(CONTENT_STRING)
End Using
If CURRENT_NUM = TO_NUM Then
btnQuit.Enabled = True
Me.ControlBox = True
tmrMake.Enabled = False
End If
End Sub
End Class
The result is c1 and c2 in the folder. Instead of 1-100 As i specified in the two tb.

You need to move some of your code around and then you should be good. See the code below...
Imports System.IO
Public Class Main
Dim CURRENT_NUM As Integer
Dim CONTENT_STRING As String
Dim PATH_STRING As String
Dim FROM_NUM As Integer
Dim TO_NUM As Integer
Public Sub SetupVar()
Try
CONTENT_STRING = tbContent.Text
PATH_STRING = tbPath.Text
FROM_NUM = Integer.Parse(tbFrom.Text)
TO_NUM = Integer.Parse(tbTo.Text)
' Moved code from tmrMake_Tick to here
'disable quit
'and control box
Me.ControlBox = False
btnQuit.Enabled = False
CURRENT_NUM = FROM_NUM
Catch ex As Exception
End Try
End Sub
Private Sub Make()
tmrMake.Enabled = True
End Sub
Private Sub btnMake_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMake.Click
SetupVar()
Make()
End Sub
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub tmrMake_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMake.Tick
Using sw As New StreamWriter(PATH_STRING & CURRENT_NUM & ".txt")
sw.Write(CONTENT_STRING)
End Using
' Increment your CURRENT_NUM after you've written the file
CURRENT_NUM = CURRENT_NUM + 1
If CURRENT_NUM = TO_NUM Then
btnQuit.Enabled = True
Me.ControlBox = True
tmrMake.Enabled = False
End If
End Sub
End Class

Related

Error : reference to a non-shared member requires an object reference

The game i am making is connect 4 , the form I am calling from is here :
Public Class Form1
Public players(1) As IPlayer
Public playerturn As IPlayer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttons()
init()
End Sub
Public Sub init()
playerturn = players(0)
Board1.Init()
currentaturn.Text = String.Format("{0}'s turn", playerturn.name)
End Sub
Sub buttons()
For i As Integer = 0 To 7
Dim btn As New Button()
btn.Text = "drop"
btn.BackColor = Color.Pink
btn.Size = New Size(40, 40)
btn.Location = New Point(i * 48, 0)
btn.Tag = i
AddHandler btn.Click, AddressOf clickbutton
Panel1.Controls.Add(btn)
Next
End Sub
Sub clickbutton(ByVal sender As Object, ByVal e As EventArgs)
Dim clickedbutton As Button = CType(sender, Button)
Dim clickedcolumn As Integer = clickedbutton.Tag
If (Board1.addpiecetocolumn(clickedcolumn, Color.Red) = False) Then
Else
End If
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LDBTN.Click
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles currentaturn.Click
End Sub
Private Sub RESETBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RESETBTN.Click
loginsys.Show()
Me.close
End Sub
End Class
and the form I am calling to :
Public Class board
Public board(8, 8) As panelbox
Public columns As Integer = 7
Public rows As Integer = 7
Public Sub New()
InitializeComponent()
Init()
End Sub
Public Sub Init()
Me.Controls.Clear()
For i As Integer = 0 To columns - 1
For j As Integer = 0 To rows - 1
board(i, j) = New panelbox()
board(i, j).Size = New Size(50, 50)
board(i, j).Location = New Point((i * 45), (j * 45))
board(i, j).BackColor = Color.DodgerBlue
Me.Controls.Add(board(i, j))
Next
Next
End Sub
Public Function addpiecetocolumn(ByVal x As Integer, ByVal colo As Color) As Boolean
For y As Integer = rows - 1 To 0 Step -1
If (board(x, y).Used = False) Then
board(x, y).Used = True
board(x, y).BackColor = colo
Return True
End If
Next
Return False
End Function
End Class
it should be noted that I am naming the second form board and the part that's calling the error is from the first form :
Public Sub init()
playerturn = players(0)
Board1.Init()
currentaturn.Text = String.Format("{0}'s turn", playerturn.name)
End Sub
Any help would be great
Use Public Shared Function function_name, in this case, you have to call it like board.function_name

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.

Reciving data from Cell counter through rs232

I have a cell counter machine that sends data automatically after test end through a rs232 cable. I tried the following code to receive that data but I get nothing although I changed the BaudRate value many times. What is the problem?
My code:
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class ComReadWrite
Dim myPorts As Array
Dim txtline As String
Dim txtchar As String
Dim txtbyte As String
Dim txtexisting As String
Delegate Sub setTextCallBack(ByVal txt As String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPorts = IO.Ports.SerialPort.GetPortNames()
portnamecombo.Items.AddRange(myPorts)
WriteButton.Enabled = False
CloseButton.Enabled = False
BaudRateBox.Items.Add(110)
BaudRateBox.Items.Add(300)
BaudRateBox.Items.Add(600)
BaudRateBox.Items.Add(1200)
BaudRateBox.Items.Add(2400)
BaudRateBox.Items.Add(4800)
BaudRateBox.Items.Add(9600) 'Populate the baudratebox Combo box to common baud rates used
BaudRateBox.Items.Add(14400)
BaudRateBox.Items.Add(19200)
BaudRateBox.Items.Add(38400)
BaudRateBox.Items.Add(57600)
BaudRateBox.Items.Add(115200)
BaudRateBox.Items.Add(128000)
BaudRateBox.Items.Add(256000)
BaudRateBox.Text = 38400
End Sub
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
SerialPort1.PortName = portnamecombo.Text
SerialPort1.BaudRate = BaudRateBox.Text
SerialPort1.ReadTimeout = 500
SerialPort1.Parity = Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = StopBits.One
SerialPort1.Open()
Start.Enabled = False
WriteButton.Enabled = True
CloseButton.Enabled = True
End Sub
Private Sub WriteButton_Click(sender As Object, e As EventArgs) Handles WriteButton.Click
SerialPort1.Write(Chr(6))
End Sub
Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click
SerialPort1.Close()
Start.Enabled = True
WriteButton.Enabled = False
CloseButton.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
RecievedText(SerialPort1.ReadExisting())
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub RecievedText(ByVal txt As String)
If Me.ReadBox.InvokeRequired Then
Dim x As New setTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(txt)})
Else
Me.ReadBox.Text &= txt
End If
End Sub
End Class

VB.net RGB color sensor "Integer is not valid"

I'm trying to make VB.net show the data "colors" from my Arduino,
My serial port working great but I have this massage every-time I press connect & weird data show's up
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string " = " to type 'Integer' is not valid.
Can someone help me in this?
this is my code
Public Class Form1
Private _msg As String
Dim R As String
Dim G As String
Dim B As String
Dim iR As String
Dim iG As String
Dim iB As String
Private indata As String
Dim IsConnected As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each str As String In IO.Ports.SerialPort.GetPortNames()
Ports.Items.Add(str)
Next
If (Ports.Items.Count > 0) Then
Ports.SelectedIndex = 0
End If
IsConnected = False
Status.Text = "Disconnected"
Status.BackColor = Color.MistyRose
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If (SerialPort1.IsOpen()) Then
Try
SerialPort1.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
IsConnected = False
Status.Text = "Disconnected"
Status.BackColor = Color.MistyRose
Button1.Enabled = True
Button2.Enabled = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (SerialPort1.IsOpen = False) Then
SerialPort1.PortName = Ports.SelectedItem
SerialPort1.Open()
IsConnected = True
Status.Text = "Connected"
Status.BackColor = Color.LightGreen
Button1.Enabled = False
Button2.Enabled = True
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'user chose string
'read data waiting in the buffer
Try
Dim msg As String = SerialPort1.ReadExisting()
indata = indata + msg
'Dim where As Integer = InStr(indata, ControlChars.Lf)
Dim where As Integer = InStr(indata, "R=")
If (indata.Length > where + 18) Then
R = indata.Substring(where + 1, 3)
G = indata.Substring(where + 7, 3)
B = indata.Substring(where + 13, 3)
indata = ""
End If
'display the data to the user
_msg = msg
_msg = _msg.Replace(ControlChars.Cr, "")
: DisplayData(msg)
Catch ex As Exception
End Try
End Sub
#Region "DisplayData"
''' <summary>
''' Method to display the data to and
''' from the port on the screen
''' </summary>
''' <remarks></remarks>
<STAThread()> _
Private Sub DisplayData(ByVal msg As String)
DisplayWindow.BeginInvoke(New EventHandler(AddressOf DoDisplay))
End Sub
#End Region
#Region "DoDisplay"
Private Sub DoDisplay(ByVal sender As Object, ByVal e As EventArgs)
'DisplayWindow.SelectedText = String.Empty
'DisplayWindow.SelectionFont = New Font(_DisplayWindow.SelectionFont, FontStyle.Bold)
'DisplayWindow.SelectionColor = MessageColor(CType(_type, Integer))
DisplayWindow.AppendText(_msg)
DisplayWindow.ScrollToCaret()
End Sub
#End Region
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
RL.Text = R
GL.Text = G
BL.Text = B
iR = CInt(R) * CInt(RM.Text)
iG = CInt(G) * CInt(GM.Text)
iB = CInt(B) * CInt(BM.Text)
RF.Text = iR
GF.Text = iG
BF.Text = iB
Try
Label6.BackColor = Color.FromArgb(CType(iR, Byte), CType(iG, Byte), CType(iB, Byte))
Catch ex As Exception
End Try
End Sub
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
System.Diagnostics.Process.Start("http://www.narzan.weebly.com/p-1049.html")
End Sub
Private Sub LinkLabel1_LinkClicked_1(sender As Object, e As LinkLabelLinkClickedEventArgs)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
picOn.Visible = True
SerialPort1.Open()
SerialPort1.Write("^")
SerialPort1.Close()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
picOn.Visible = False
SerialPort1.Open()
SerialPort1.Write("<")
SerialPort1.Close()
End Sub
Private Sub picOn_Click(sender As Object, e As EventArgs) Handles picOn.Click
End Sub
Private Sub RL_Click(sender As Object, e As EventArgs) Handles RL.Click
End Sub
Private Sub RM_TextChanged(sender As Object, e As EventArgs) Handles RM.TextChanged
End Sub
End Class
My problem was from my Arduino code not from VB.net
the solution is to remove "=" from R,G,B
void printColour(){
Serial.print("R = ");
Serial.println(int(colourArray[0]));
Serial.print("G = ");
Serial.println(int(colourArray[1]));
Serial.print("B = ");
Serial.println(int(colourArray[2]));
//delay(2000);
}
so the code will look like that
void printColour(){
Serial.print("");
Serial.println(int(colourArray[0]));
Serial.print("");
Serial.println(int(colourArray[1]));
Serial.print("");
Serial.println(int(colourArray[2]));
//delay(2000);
}
Thanks everyone, everything work great now

How to remove a line which is drawn on a form

I'm trying to draw a triangle like this:
Dim triangle As Graphics
Dim pen1 As New Pen(Color.LimeGreen, 2)
Dim lside As Integer
Dim wside As Integer
Dim dside As Integer
triangle = Me.CreateGraphics()
triangle.DrawLine(pen1, wside, 420, 640, 420)
triangle.DrawLine(pen1, 640, lside, 640, 420)
triangle.DrawLine(pen1, dside, 420, 640, lside)
lside, wside and dside stand for length side, width side and diagonal side.
I've got 4 textboxes, for the length, width, diagonal side and one for the angle.
The purpose is to fill in 2 of the values, and then a rectangular triangle gets drawn following Pythagoras' theorem. I want to draw a line for Angle as well later on. But I first want to get this to work.
But every time I click the button to draw a new triangle, the previous one should get deleted. And that's the problem.
I've tried multiple methods, like triangle.Dispose triangle.Restore triangle.Clear and more. None of them work.
Why am I not drawing them in a picturebox you might ask. Well, when I drew a line in a picturebox, the picturebox sort of went in front of the line, making the line invisible. And I didn't know how to fix that.
Try using Me.Invalidate(), it basically clears, then draws the shape in the area you're painting on. Reference.
Private Sub ClearCanvas_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Invalidate()
End Sub
Priavte DrawTriangle_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim triangle As Graphics
Dim pen1 As New Pen(Color.LimeGreen, 2)
Dim lside As Integer
Dim wside As Integer
Dim dside As Integer
triangle = Me.CreateGraphics()
triangle.DrawLine(pen1, wside, 420, 640, 420)
triangle.DrawLine(pen1, 640, lside, 640, 420)
triangle.DrawLine(pen1, dside, 420, 640, lside)
End Sub
‘Draw select delete multiple lines on Picturebox
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim drawrec, undo_delete As Boolean
Dim index_arrary_line_tobe_deleted(10000) As Integer
Dim ptA, ptB As Point ' starting and ending point
Dim down As Boolean
Dim k, Last_index_line_tobe_selected As Integer
Private temp As line
Dim List_of_line_tobe_deleted As New List(Of line)
Dim List_of_line_to_Undo As New List(Of line)
Private m_Lines As New List(Of line)
Private m_Pt As Point
Private m_Pt2 As Point
Private m_tracking As Boolean
Private Sub B2_index_arrary_line_tobe_deletedete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2_Delete.Click
Try
m_Lines.RemoveAll(AddressOf List_of_line_tobe_deleted.Contains)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub B3_Undodelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B3_Undodelete.Click
undo_delete = True
Try
m_Lines.AddRange(List_of_line_tobe_deleted)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub B1_Select_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1_Select.Click
drawrec = True
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))
If m_tracking = True And drawrec = True Then
k = -1
For i As Integer = 0 To m_Lines.Count - 1
If m_Lines(i).ContainsCompletely(r) = True Then
k = k + 1
index_arrary_line_tobe_deleted(i) = k
Debug.Print("Index of NOT selected lines " + i.ToString + "Index of selected lines " + Last_index_line_tobe_selected.ToString) 'to compare idex of two lists !!!!
index_arrary_line_tobe_deleted(k) = i
List_of_line_tobe_deleted.Add(m_Lines(i))
End If
Next
Last_index_line_tobe_selected = k 'so far no use, just to know
e.Graphics.DrawRectangle(Pens.Cyan, r)
End If
If undo_delete = False Then
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Draw(e.Graphics, r)
Debug.Print("Index of remaining lines " + i.ToString)
Next
End If
If undo_delete = True Then
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Re_Draw(e.Graphics)
Debug.Print("Index of remaining lines " + i.ToString)
Next
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
drawrec = False
down = False
undo_delete = False
For i As Integer = 0 To index_arrary_line_tobe_deleted.Length - 1
index_arrary_line_tobe_deleted(0) = -1
Next i
k = -1
Last_index_line_tobe_selected = -1
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
down = True
If down = True And drawrec = False Then
ptA = e.Location
temp = New line
temp.StartPoint = e.Location
End If
If e.Button = MouseButtons.Left Then
ResetSelected(Me.m_Lines)
m_Pt = e.Location
End If
End Sub
Private Sub ResetSelected(ByVal m_Lines As List(Of line))
For i As Integer = 0 To m_Lines.Count - 1
m_Lines(i).Selected = False
Next
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If down = True Then
End If
If e.Button = MouseButtons.Left Then
If down = True And drawrec = False Then
ptB = e.Location
temp.EndPoint = e.Location
End If
m_Pt2 = e.Location
m_tracking = True
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
down = False
If drawrec = False Then
temp.EndPoint = e.Location
m_Lines.Add(temp)
temp = Nothing
Me.PictureBox1.Invalidate()
End If
m_tracking = False
Me.PictureBox1.Invalidate()
End Sub
End Class
Public Class line
Public StartPoint As Point
Public EndPoint As Point
Private m_selected As Boolean
Public Property Selected() As Boolean
Get
Return m_selected
End Get
Set(ByVal value As Boolean)
m_selected = value
End Set
End Property
Public Sub Draw(ByVal g As Graphics, ByVal r As Rectangle)
Dim myPen1 As New Pen(Color.Red, 1)
g.SmoothingMode = SmoothingMode.AntiAlias
If Me.ContainsCompletely(r) OrElse Me.Selected Then
Me.Selected = True
g.DrawLine(myPen1, Me.StartPoint, Me.EndPoint)
Else
Dim myPen2 As New Pen(Color.Blue, 1)
g.DrawLine(myPen2, Me.StartPoint, Me.EndPoint)
End If
End Sub
Public Sub Re_Draw(ByVal g As Graphics)
g.SmoothingMode = SmoothingMode.AntiAlias
Dim myPen2 As New Pen(Color.Blue, 1)
g.DrawLine(myPen2, Me.StartPoint, Me.EndPoint)
End Sub
Public Function ContainsCompletely(ByVal r As Rectangle) As Boolean
If r.Contains(Me.StartPoint) AndAlso r.Contains(Me.EndPoint) Then
Return True
End If
Return False
End Function
End Class
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim drawrec As Boolean
Dim del As Integer
Dim ptA, ptB As Point ' starting and ending point
Dim down As Boolean
Private temp As line
Private m_Lines As New List(Of line)
Private m_rnd As New Random
Private m_Pt As Point
Private m_Pt2 As Point
Private m_tracking As Boolean
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Delete.Click
Try
m_Lines.RemoveAt(del)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
drawrec = False
down = False
del = -1
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
down = True
If down = True And drawrec = False Then
ptA = e.Location
temp = New line
temp.StartPoint = e.Location
End If
If e.Button = MouseButtons.Left Then
ResetSelected(Me.m_Lines)
m_Pt = e.Location
End If
End Sub
Private Sub ResetSelected(ByVal m_Lines As List(Of line))
For i As Integer = 0 To m_Lines.Count - 1
m_Lines(i).Selected = False
Next
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If down = True Then
End If
If e.Button = MouseButtons.Left Then
If down = True And drawrec = False Then
ptB = e.Location
temp.EndPoint = e.Location
End If
m_Pt2 = e.Location
m_tracking = True
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
down = False
If drawrec = False Then
temp.EndPoint = e.Location
m_Lines.Add(temp)
temp = Nothing
Me.PictureBox1.Invalidate()
End If
m_tracking = False
Me.PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))
If m_tracking And drawrec = True Then
For i As Integer = 0 To m_Lines.Count - 1
If m_Lines(i).ContainsCompletely(r) = True Then
Debug.Print("KKKKKKKKKKKKKKK " + i.ToString)
del = i
End If
Next
e.Graphics.DrawRectangle(Pens.Cyan, r)
End If
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Draw(e.Graphics, r)
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Select.Click
drawrec = True
End Sub
End Class
Public Class line
Public StartPoint As Point
Public EndPoint As Point
Public Filled As Boolean
Public ShapeColor As Color
Public PenWidth As Integer
Private m_selected As Boolean
Public Property Selected() As Boolean
Get
Return m_selected
End Get
Set(ByVal value As Boolean)
m_selected = value
End Set
End Property
Public Sub Draw(ByVal g As Graphics, ByVal r As Rectangle)
g.SmoothingMode = SmoothingMode.AntiAlias
If Me.ContainsCompletely(r) OrElse Me.Selected Then
Me.Selected = True
g.DrawLine(Pens.Red, Me.StartPoint, Me.EndPoint)
Else
g.DrawLine(Pens.Blue, Me.StartPoint, Me.EndPoint)
End If
End Sub
Public Function ContainsCompletely(ByVal r As Rectangle) As Boolean
If r.Contains(Me.StartPoint) AndAlso r.Contains(Me.EndPoint) Then
Return True
End If
Return False
End Function
End Class