Results 1 to 9 of 9

Thread: How to create ' temporary' graphics with text changed event?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    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?

    Thanks,
    Andrew
    Attached Images Attached Images  

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to create ' temporary' graphics with text changed event?

    invalidate the rectangle containing the square

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    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.

    Thanks,
    Andrew

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to create ' temporary' graphics with text changed event?

    you need to keep track of the location of the black square, then:

    vb Code:
    1. picShowMap.Invalidate(New Rectangle(50, 50, 10, 10)) 'l,t,w,h

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to create ' temporary' graphics with text changed event?

    Quote Originally Posted by Lord Orwell View Post
    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.
    no. he's using creategraphics from the picturebox

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: How to create ' temporary' graphics with text changed event?

    Quote Originally Posted by .paul. View Post
    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to create ' temporary' graphics with text changed event?

    no. that's not how it works. try this + see:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         'i loaded a bitmap in PictureBox1 at design time
    5.         Dim gr As Graphics = PictureBox1.CreateGraphics
    6.         gr.FillRectangle(Brushes.Red, New Rectangle(20, 20, 50, 50))
    7.     End Sub
    8.  
    9.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    10.         PictureBox1.Invalidate()
    11.     End Sub
    12.  
    13. End Class

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    5

    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.

    Andrew

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width