How to create ' temporary' graphics with text changed event?
Hi,
I'm in an upper level industrial engineering class in which we're learning some entry level VB programming. We're working on building a program to solve the travelling salesmen problem (finding the shortest path through a collection of cities while visiting each only once) and I'm trying to add some 'fancy' to it so I can learn some more skills.
The basic program is designed to load a bitmap file of a map into a picture box and a text file with the xy coordinates of any city positions. From there, the user can plot the cities with red squares, draw a random tour through the cities, or tour the cities by always visiting the nearest neighbor not yet visited.
The modification I'm working on is a method of pointing out a specific city with a black square by entering the city's number (determined from the coordinate file) into a text box.
Code:
Private Sub TxtFindCity_textchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtFindCity.TextChanged
Dim ObjFindCity As Graphics = picShowMap.CreateGraphics
Dim City As Integer
If Integer.TryParse(TxtFindCity.Text, City) Then
If City > myCount - 1 Then
City = 0
ObjFindCity.FillRectangle(Brushes.Black, myData(City, 0) - 3, myData(City, 1) - 3, 10, 10)
ElseIf 0 <= City <= myCount - 1 Then
ObjFindCity.FillRectangle(Brushes.Black, myData(City, 0) - 3, myData(City, 1) - 3, 10, 10)
End If
End If
End Sub
So far, the program works, but I want the black squares (but none of the other graphics) that are created to go away once the text box is cleared. As of now they just hang around on the screen until I refresh the entire image with a 'clear map' sub. Any suggestions on how to accomplish this?
Re: How to create ' temporary' graphics with text changed event?
Sorry if this is ignorant... How do I do that? I've spent a couple hours researching that invalidate method but everything that I've tried has absolutely no effect on the picturebox.
Re: How to create ' temporary' graphics with text changed event?
i don't think invalidating will help since it appears you are drawing directly on the bitmap you created. One possible solution is drawing the squares instead in the paint event on the window. Then a simple "if squares = visible then" would solve your problem.
Re: How to create ' temporary' graphics with text changed event?
Originally Posted by Lord Orwell
i don't think invalidating will help since it appears you are drawing directly on the bitmap you created. One possible solution is drawing the squares instead in the paint event on the window. Then a simple "if squares = visible then" would solve your problem.
Re: How to create ' temporary' graphics with text changed event?
Originally Posted by .paul.
no. he's using creategraphics from the picturebox
i see that but isn't he drawing directly onto the bitmap in the picturebox?
please correct me if i'm wrong, but i thought that was how it worked. If you already had a bitmap loaded, creategraphics gave you a direct link to the drawing surface, allowing you to draw on a pre-existing bitmap. It reads to me like it's drawing directly to the buffer because you are doing this somewhere besides the paint event, therefore invalidating that square would only copy it again.
and i really am guessing here, since i do all my drawing in the paint event so i may be completely wrong, so feel free to give me a link or something that i can study if that isn't right.
Re: How to create ' temporary' graphics with text changed event?
Thanks for the help! The invalidation worked. I created a for loop which invalidated a rectangle at every possible city because I couldn't figure out a way to retain data entered into the text box at any one point in time. Because of that, I was unable to 'find' the rectangles drawn at any point.