Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
How do I make an "Undo" button in Visual Basic 2008 for a user controlled graphics screen?
I'm using a PictureBox, with a DataGridView to put the x & y coordinances in. I'm using the TryParse method to record them, so they can be saved. I have not found any good examples of an "Undo" button anywhere on the 'net' for the undo button. The screen will save a line, or graphic made once applied, even if you remove it from the datagridview. In the event of a 'missed' line', where the line does not connect where it's supposed to, I need to be able to remove it. Here's a section of the code I'm using...
Public Class Form1
Dim canvas As Bitmap
Dim gfx As Graphics
Sub New()
InitializeComponent()
Me.DoubleBuffered = True
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
canvas = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
gfx = Graphics.FromImage(canvas)
Dim p As String = Application.StartupPath & "Test.txt"
Dim line As String = Nothing
If IO.File.Exists(p) Then
Using fn As New IO.StreamReader(p)
Do
line = fn.ReadLine()
If Not line = Nothing Then
Dim s() As String = Split(line, "~")
Me.DataGridView1.Rows.Add(s)
End If
Loop Until line Is Nothing
End Using
Me.DataGridView1.Rows(0).Selected = True
End If
Me.DataGridView1.Rows(0).Selected = True
PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
End Sub
Private Sub Set1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Set1.Click
For Each i As DataGridViewRow In Me.DataGridView1.Rows
With i
Dim x1 As Integer = .Cells("X1").Value
Dim y1 As Integer = .Cells("Y1").Value
Dim x2 As Integer = .Cells("X2").Value
Dim y2 As Integer = .Cells("Y2").Value
Dim x11 As Integer = .Cells("X11").Value
Dim y11 As Integer = .Cells("Y11").Value
Dim x21 As Integer = .Cells("X21").Value
Dim y21 As Integer = .Cells("Y21").Value
Dim x12 As Integer = .Cells("X12").Value
Dim y12 As Integer = .Cells("Y12").Value
Dim x22 As Integer = .Cells("X22").Value
Dim y22 As Integer = .Cells("Y22").Value
Dim x13 As Integer = .Cells("X13").Value
Dim y13 As Integer = .Cells("Y13").Value
Dim x23 As Integer = .Cells("X23").Value
Dim y23 As Integer = .Cells("Y23").Value
Dim ParseResult As Boolean = True
ParseResult = ParseResult And Integer.TryParse(.Cells("X1").Value, x1)
ParseResult = ParseResult And Integer.TryParse(.Cells("Y1").Value, y1)
ParseResult = ParseResult And Integer.TryParse(.Cells("X2").Value, x2)
ParseResult = ParseResult And Integer.TryParse(.Cells("Y2").Value, y2)
ParseResult = ParseResult And Integer.TryParse(.Cells("X11").Value, x11)
ParseResult = ParseResult And Integer.TryParse(.Cells("Y11").Value, y11)
ParseResult = ParseResult And Integer.TryParse(.Cells("X21").Value, x21)
ParseResult = ParseResult And Integer.TryParse(.Cells("Y21").Value, y21)
ParseResult = ParseResult And Integer.TryParse(.Cells("X12").Value, x12)
ParseResult = ParseResult And Integer.TryParse(.Cells("Y12").Value, y12)
ParseResult = ParseResult And Integer.TryParse(.Cells("X22").Value, x22)
ParseResult = ParseResult And Integer.TryParse(.Cells("Y22").Value, y22)
ParseResult = ParseResult And Integer.TryParse(.Cells("X13").Value, x13)
ParseResult = ParseResult And Integer.TryParse(.Cells("Y13").Value, y13)
ParseResult = ParseResult And Integer.TryParse(.Cells("X23").Value, x23)
ParseResult = ParseResult And Integer.TryParse(.Cells("Y23").Value, y23)
Dim MyGraphics As Graphics = Graphics.FromImage(PictureBox1.Image)
MyGraphics.DrawLine(Pens.Black, x1, y1, x2, y2)
MyGraphics.DrawEllipse(Pens.Black, x11, y11, x21, y21)
MyGraphics.DrawRectangle(Pens.Black, x12, y12, x22, y22)
MyGraphics.FillEllip
all works until I hit the button, then I get exception" Out of range" for:
1Lines.RemovalAt(1Lines.Count - 1)
I extended this question, cause I'm still working with it, & will thru the night. Im gonna keep toying with it, becuase I think it's a simple typo, but I'm looking into "out of range" exceptions also.Everything looks right, just gotta find the bug.
2 Answers
- Hal 9000Lv 41 decade agoFavorite Answer
Try looking at the List class
I don't know if this is the best way but here's some ideas of how i'd use it
Create your own structure:
Private Structure myLine
Public startX As Integer
Public startY As Integer
Public endX As Integer
Public endY As Integer
Sub New(ByVal sX As Integer, ByVal sY As Integer, ByVal eX As Integer, ByVal eY As Integer)
startX = sX
startY = sY
endX = sX
endY = sY
End Sub
End Structure
Add global variables to your form:
dim lLines as new list(of myLine)
dim newStartX as integer
dim newStartY as integer
On the MouseDown event:
newStartX = e.Location.X
newStartY = e.Location.Y
On the MouseUp event:
lLines.add(new myLine(newStartX, newStartY, e.location.X, e.location.Y)
drawImage()
Then have another subroutine that handles drawing the image
sub drawImage()
Dim MyGraphics As Graphics = Graphics.FromImage(PictureBox1.Image)
myGraphics.clear(color.white) ''whatever your background colour is
for x = 0 to lLines.count - 1 ''Loop through each entry and draw to bitmap
mygraphics.DrawLine(Pens.Aqua , lLines(x).startX, lLines(x).startY, lLines(x).endX, lLines(x).endY)
next
end sub
So all your undo button would do is remove the last addition to the List array and redraw:
lLines.RemoveAt(lLines.Count - 1)
drawImage()
Have fun!
- Anonymous5 years ago
Try this: Add a label, a timer and 2 buttons. Size your form to 300 by 300. Cut and paste this into the code pane. Play around with the "x" value and the timer interval. Public Class Form1 Dim x, y As Long Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick x = x - 5 Label1.Location = New Point(x, y) If x < -20 Then x = 300 End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click x = 350 y = 10 Button1.Text = "Start" Timer1.Enabled = True End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Button2.Text = "Stop" Timer1.Enabled = False End Sub End Class : )