• 0

[VB.NET] Form Redraw?


Question

this is probably extremely easy to fix, but here goes.

i'm using a graphicspen to draw on a picturebox. however, every time i minimize/move another window oveer it, it disappears. i seem to remember a .ReDraw property in VB6, but it's not there. What's the .Net equivelant?

(Visual Basic, VS.NET 2003)

Link to comment
https://www.neowin.net/forum/topic/352258-vbnet-form-redraw/
Share on other sites

8 answers to this question

Recommended Posts

  • 0
  AMDPOX said:
this is probably extremely easy to fix, but here goes.

i'm using a graphicspen to draw on a picturebox.  however, every time i minimize/move another window oveer it, it disappears.  i seem to remember a .ReDraw property in VB6, but it's not there.  What's the .Net equivelant?

(Visual Basic, VS.NET 2003)

586306143[/snapback]

.Paint() usually forces whatever component it is to redraw itself.

  • 0
  AMDPOX said:
this is probably extremely easy to fix, but here goes.

i'm using a graphicspen to draw on a picturebox.? however, every time i minimize/move another window oveer it, it disappears.? i seem to remember a .ReDraw property in VB6, but it's not there.? What's the .Net equivelant?

(Visual Basic, VS.NET 2003)

586306143[/snapback]

.Invalidate() forces a redraw

If you use .Paint() you calling the sub directly, which isn't a good idea in this situation. It shouldn't hurt anything, but it's just better practice to use .Invalidate().

  • 0

i've tried:

    Private Sub Form1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        pD.Invalidate()
    End Sub

    Private Sub Form1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LostFocus
        pD.Invalidate()
    End Sub

and

    Private Sub Form1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        Form1.Invalidate()
    End Sub

    Private Sub Form1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LostFocus
        Form1.Invalidate()
    End Sub

where pD is the picturebox. But it appears that b the Invalidate are simply clearing the the picturebox. Paint() seems to be an event, not a method....?

  • 0
  AMDPOX said:
this is probably extremely easy to fix, but here goes.

i'm using a graphicspen to draw on a picturebox.  however, every time i minimize/move another window oveer it, it disappears.  i seem to remember a .ReDraw property in VB6, but it's not there.  What's the .Net equivelant?

(Visual Basic, VS.NET 2003)

586306143[/snapback]

Are you drawing on the picture box during its paint event? If not, do so. Then, in your form's resize and/or move event, invalidate the picturebox.

Edited by weenur
  • 0
  AMDPOX said:
i have the drawing commands in the MouseDown and MouseMove events... i dont see how i could put them in .Paint() and leave the functionality the same.. :(

586311835[/snapback]

Ah, ok. Makes sense now.

You need to draw to an image to preserve state. Create a class member variable that is an Image or Bitmap, and base it on the pictureBox.

Do this in the form constructor(New sub)

Dim g1 as Graphics = Graphics.FromHwnd(picturebox1.Handle)

' declared at the class level
picImg = new Bitmap(pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height, g1)

Handle the paint event for your picturebox and use the Graphics object to draw the image.

private sub PicPaint(sender as object, e as PaintEventArgs) handles pictureBox1.Paint

  e.Graphics.DrawImage(picImg, 0, 0)

end sub

Then in your mouse handler(up, i assume), draw on the image

  	end = new PointF(e.X, e.Y);

  	dim g2 as Graphics = Graphics.FromImage(picImg)
  	g2.DrawLine(Pens.Black, begin, end) ' or whatever
  	pictureBox1.Invalidate(false)

  • 0

I'm a newbie and pretty confused at this point. I have searched quite a bit for a solution to the same problem and have not been able to understand the code you posted.

I have a form with PictureBox1 and Button1 on it. My code looks like this:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
     g = PictureBox1.CreateGraphics() 
     Dim g As Graphics
     Dim pRed As New Pen(Color.Red, 2)
     g.DrawLine(pRed, x1, y1, x2, y2)
End Sub

with x1, y1, x2, y2 being coordinates of the start and end of the line. That part works, but much like AMDPOX describes, when I minimize the window, the line disappears. I see that weenur has given him a reply with some code, but I guess I don't fully understand it.

When you say

  Quote
Do this in the form constructor(New sub)
I understand what the form constructor is, but not sure how I should make the New sub.

Also, I'm not sure what you mean by

  Quote
' declared at the class level
.

Could you show me what I should do with my code, and maybe paste the entire code of the form from top to bottom? I think that would help me understand the placement of each piece of code.

Again, I'm a newbie, so thank you for your patience with my 'newbieness' :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Minecraft Chase the Skies update and Vibrant Visuals graphics overhaul lands next week by Pulasthi Ariyasinghe The second major game update of the year for Minecraft is almost here. Mojang today announced that its next Game Drop, Chase the Skies, is landing on June 17. Alongside it, the highly anticipated graphics overhaul, dubbed Vibrant Visuals, will be available too, making the day a rather big occasion for Minecraft fans. The Chase the Skies update has a large number of gameplay features and content, but one of the biggest is the expansion of the Ghast. Now, players will be able to find Happy Ghasts as a tameable mob, letting up to five players ride them on journeys across the skies. To get one, players will have to find small, dehydrated Ghasts near fossil structures in the Nether, then bring it to the overworld and feed it plenty of water. It is also possible to craft dried Ghasts from Nether materials. Other features of this update include a handy player locator bar, better leads functionality, craftable saddles, a new music disc, and more. As for the Vibrant Visuals upgrade, Mojang is touting a visual refresh that lets players witness how lights and shadows affect the Minecraft world. "Fly high with your happy ghast to watch the sunrise reflect on the rippling ocean, make art with shifting shadows, descend into dark depths, or simply sit back and enjoy the vivid beauty of each biome," says the studio. "With updated in-game visuals you’ll see how light and shadow transform the Overworld – water reflects, forests become shaded, fog drifts, and waterfalls glimmer in the light." The currently supported devices for Vibrant Visuals include Xbox Series X|S, Xbox One, PlayStation 4, PlayStation 5, PC, as well as Android (Android: Adreno 640, Mali-G68, Mali-G77, or Xclipse 530 or higher) and iOS (A12 or M1 or higher.)   Unfortunately, the Vibrant Visuals upgrade will only be available on the Bedrock Edition of Minecraft, at least for now. Mojang has said that it plans to bring the visual overhaul to the Java Edition of Minecraft at a later date, but no information or release window has been announced just yet.
    • Once people get use to the square-ish case, they'll probably switch the display to square as well.
    • Why would you think it's not enabled all the time? Because they said so?
    • $100 billion of the $200 billion is in N.Y. State over the upcoming 20 years plus. Obviously 20+ year spending plans are subject to change.
    • I own one somewhere and even got the email from Amazon letting me know about the recall but not sure I'll be able to find it. Haven't seen it in months.
  • Recent Achievements

    • One Month Later
      POR2GAL4EVER earned a badge
      One Month Later
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • Week One Done
      serfegyed earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      550
    2. 2
      ATLien_0
      250
    3. 3
      +Edouard
      163
    4. 4
      +FloatingFatMan
      158
    5. 5
      Michael Scrip
      109
  • Tell a friend

    Love Neowin? Tell a friend!