HScrollBar and AutoRedraw

HScrollBar

From the toolbox, the HScrollBar does not seem to be working (as it did in VB6)
It does not fire on "Gotfocus", no matter where you click
There is no "Mouseup" event for ending the scroll.
Is this tool being phased out, or are there known bugs

Related to this problem is Auto-redraw. I use the Scrollbar to help make a selection. As the user pulls the cursor, I have a floating textbox to provide information as the scrollbar passes the choice in question. In VB6 the AutoRedraw of the form avoided erasing stuff in the pathway of the textbox.

I read that AutoRedraw is no longer necessary.
From MS VS Doc:

The AutoRedraw property is no longer supported and is not necessary because the Paint and Draw events automatically persist graphics.

I made a few more tests. It appears that controls can pass over one another without problem, but everything (text or graphics) written on the form itself gets wiped out. Does this mean we are no longer supposed to place any of our graphics directly on the form




Answer this question

HScrollBar and AutoRedraw

  • teuneboon

    There is no automatic redraw. You can draw at any point, but the graphics are not persistent. You have to do your drawing in the paint event for the form if you want to mimic persistence.

    The graphics object you create is directly tied to the surface you will be drawing on. You can use a picturebox or the forms BackgroundImage property to mimic autoredraw. For example, create a new project and put a picturebox on it (at least 100 by 100), then paste this code into the form load event:

    Dim poBM As Bitmap
    Dim poG As Graphics
    poBM =
    New Bitmap(100, 100)
    poG = Graphics.FromImage(poBM)
    ' 'clear' the bitmap by filling in with white
    poG.FillRectangle(Brushes.White, 0, 0, poBM.Width, poBM.Height)
    ' this will draw a diagonal line on the bitmap
    poG.DrawLine(Pens.Black, 0, 0, 99, 99)
    ' make this new image the background for a picturebox
    PictureBox1.BackgroundImage = poBM
    PictureBox1.BackgroundImageLayout = ImageLayout.None
    poG.Dispose()
    poG =
    Nothing

    The graphics object in this case we create from scratch then put in the picturebox. In the form Paint event, the event arugment has the graphics object already created (e.Graphics) and linked to the form's drawing surface.

    Does that help or confuse more


  • Vishwa Mohan Saxena

    You can, you just have to do it in the paint event. One brute force method I"ve used to work around the lack of an autoredraw property when converting a VB5 project is to create a form level bitmap object, draw on that throughout the module and in the form's paint event i just throw up the whole bitmap object.
  • dzimmy

    The graphics do not seem to persist. For example, I draw a grid in a picturebox and open a large text file and draw an extensive map in the picture box. The graphic does not persist but has to redraw in the paint event and takes the original 40 or so seconds to redraw. Avery poor excuse for vb6's instantaneous autoredraw. We tried VB.net, vb.net 2003, we cerainly are not going to go to vb.net 2005 unless a very simple autredraw equivalent is introduced. We have reverted back to vb6 for all our development.
  • zeyansoft

    Well, I'm not a MS developer but judging from how the framework is laid out, you won't be getting your autoredraw property.

    You might try creating a form-level bitmap, paint to that in your load text file logic, then in the picturebox paint event a simple e.Graphics.DrawImage will make it appear "instantaneous."  For example,

    Paint_Event
       If Form_Level_Bitmap IsNot Nothing Then
          e.Graphics.DrawImage(Form_Level_Bitmap....)
       Else
          e.Graphics.FillRectangle(....fill with white or black or whatever)
       End If
    End Sub

    That's a total of five lines, I'm sure that would be fast enough.


  • paisanperu

    Thanks for the info.
    Apparently you used an intermediate object to obtain a graph or image that persists (redraw).
    I also get ReDraw on other controls, but not when I use Graphics methods.

    My question remains: can Form itself accept such Graphics with ReDraw
    Or am I not using the "paint event" correctly

    The Help/Index on Paint gives some rather obtuse remarks.

    Let me summarize my transition to NET
    I could not draw a line, because I did not have a Pen
    I could not use my Pen, because I did not have a Graphics system.
    I could not use Graphics, because MS did not provide one automatically

    So three lines of code later (define Graphics, pen, brush, etc)
    I can now draw lines, rectangles, strings etc., but my graphs are wiped out my moving objects.
    Am I Painting or just drawing
    When is ReDraw automatic







  • LeHobbit31

    Yes, that helps - because it works.
    Thanks

    Drawing to the form is apparently like drawing on sand: the next wave will wipe it out.
    Your intermediate layer of a using the bitmap image is persistent.

    It also works for drawing directly on a control. I have a few command buttons that did not show their text very clearly (the old Fixedsys font of VB6 was easier to read). I tried drawing on the button (GrButtonX = ButtonX.CreateGraphics), which works, but is not only cleared by other windows or control, but simply with the Mouse hover. The bitmap helped for that problem as well.

    Am I confused Yes.
    from Help / Index / "painting" :

    The act of drawing on the screen is known as painting. Forms and controls have a Paint event that occurs whenever they need to be redrawn, for example, when a form is first displayed or when it has been covered by another window. Any code that you write to display graphics is usually contained in the Paint event handler.

    (note the use of the word "usually" in the last line)
    Graphics commands placed directly in the events sub FormX.paint is persistent, but the same graphics commands in other subs is volatile. Any way to force the paint event in other subs







  • Scott Lezberg

    I didn't say save the bitmap, just keep a copy of it in memory (as a form-level variable). This pseduocode for example:

    Class Form1
    dim poBM as Bitmap = nothing

    Private sub Button1_click
    poBM = New Bitmap(500,500)
    ...draw some stuff on it
    picturebox1.Invalidate()
    end sub

    Private sub Button2_click
    If poBM IsnotNothing then
    ... draw some more stuff on it
    picturebox1.Invalidate()
    End If
    end sub

    Picturebox1_Paint
    if pBM IsNot Nothing
    ...draw it up there
    Else
    .....fill picturebox with black
    End if

    end class

    I think that type of solution will work. I'm coming from a long VB history (started with 3 in win 3.11) and I know exactly what you mean about the switch to .NET. There is a steep learning curve but If you tough it out I think you'll be glad you did.


  • Ramvarun

    The problem is that, if I create and save a bitmap, then I have to continue to add more vector graphics and remove some parts of the original (bitmap). I would like to use vb.net, and have versions of the original through 2003, but the help files in vb.net are atrocious compared to vb6. I can complete 20,000 lines of code in vb6 in the time it takes to complete 1,000 lines in net after coping with the continuos popups disallowing steps and then trying to sort out some idea from texts or the help file all of which are next to useless. I keep asking Why oh why didn't MS keep net backwards compatible with VB6 I know the answer, but it's one more reason to hate MS.

  • Nats_007

    Yes, invoking a Object.Invalidate() will cause the object to be repainted.
  • HScrollBar and AutoRedraw