Sizechanged event question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnb41

    #1

    Sizechanged event question...

    I have a Picturebox that has an image displayed inside. Through code,
    I was able to have the "whole image" display in it without distortion.
    (Like a "Fit All" in most any image viewer program)

    Now, when the Picturebox gets resized by the user, I want the image to
    recalculate and display as "Fit All" again.

    So I have code in my SizeChanged event of the picturebox. When the
    "size changed", it does it's calculation, and displays properly.

    It works, but there is a problem: If the user drags the Picturebox
    window larger or smaller, the SizeChanged event fires many times per
    second. This causes alot of sluggishness in the app when trying to
    display the image.

    Is there anyway that i can have the SizeChanged event fire ONCE when
    i'm done resizing the picturebox?

    (By the way, the picturebox gets resized indirectly. It's dock is set
    to Fill. When the interface "Next" to it gets resized, the Picturebox
    naturally will resize accordingly)

    Thanks for all your help!

    John

  • Cor Ligthert

    #2
    Re: Sizechanged event question...

    John,

    You can use in the event two static variables (or one static point) to see
    everytime if it changed,

    I hope this helps,

    Cor


    Comment

    • Peter Proost

      #3
      Re: Sizechanged event question...

      Hi John,

      which sizechanged event are you handling? Because I have written a bit of
      code which does the same thing, and tried it in these 3 events and they all
      only fire once, when I finish dragging the form border

      Private Sub ptbImg_Resize(B yVal sender As Object, ByVal e As
      System.EventArg s) Handles ptbImg.Resize
      End Sub

      Private Sub ptbImg_SizeChan ged(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles _ ptbImg.SizeChan ged
      End Sub

      Private Sub frmResize_SizeC hanged(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles _ MyBase.SizeChan ged

      End Sub

      greetz Peter

      --
      Programming today is a race between software engineers striving to build
      bigger and better idiot-proof programs, and the Universe trying to produce
      bigger and better idiots. So far, the Universe is winning.

      "Cor Ligthert" <notmyfirstname @planet.nl> schreef in bericht
      news:uq$esOOZFH A.3488@tk2msftn gp13.phx.gbl...[color=blue]
      > John,
      >
      > You can use in the event two static variables (or one static point) to see
      > everytime if it changed,
      >
      > I hope this helps,
      >
      > Cor
      >
      >[/color]


      Comment

      • johnb41

        #4
        Re: Sizechanged event question...

        Peter,

        It's actually a Panel that I have the SizeChanged event attached to.
        This is so the I get scrollbars when the image is larger than the
        Panel.

        If it's not any trouble, could you post some sample code that makes the
        event fire only once? I just did a sample test on resizing a plain
        form:

        Private Sub Form1_SizeChang ed(...) Handles MyBase.SizeChan ged
        MessageBox.Show ("Size Changed!")
        End Sub

        The form won't even resize because the messagebox pops up immediately.
        If I could get the messagebox to show up after finishing the resize,
        that would be awesome. I could then adapt it to my real app.

        Thanks!
        John

        Comment

        • johnb41

          #5
          Re: Sizechanged event question...

          Thanks for the reply. You make it seem obvious! Could you describe
          more of what you mean, as it's very unclear to me. Thanks!

          John

          Comment

          • Peter Proost

            #6
            Re: Sizechanged event question...

            Hi John,

            when you run you code:
            [color=blue]
            > Private Sub Form1_SizeChang ed(...) Handles MyBase.SizeChan ged
            > MessageBox.Show ("Size Changed!")
            > End Sub[/color]

            the first time you see the messagebox pop up is because the form is
            initialized, but after that the event normal only fires if you let go the
            form border, so at the end of the SizeChanged event.

            hth
            Greetz Peter

            --
            Programming today is a race between software engineers striving to build
            bigger and better idiot-proof programs, and the Universe trying to produce
            bigger and better idiots. So far, the Universe is winning.

            "johnb41" <orders@informa tik.com> schreef in bericht
            news:1117457542 .581437.123130@ z14g2000cwz.goo glegroups.com.. .[color=blue]
            > Peter,
            >
            > It's actually a Panel that I have the SizeChanged event attached to.
            > This is so the I get scrollbars when the image is larger than the
            > Panel.
            >
            > If it's not any trouble, could you post some sample code that makes the
            > event fire only once? I just did a sample test on resizing a plain
            > form:
            >
            > Private Sub Form1_SizeChang ed(...) Handles MyBase.SizeChan ged
            > MessageBox.Show ("Size Changed!")
            > End Sub
            >
            > The form won't even resize because the messagebox pops up immediately.
            > If I could get the messagebox to show up after finishing the resize,
            > that would be awesome. I could then adapt it to my real app.
            >
            > Thanks!
            > John
            >[/color]


            Comment

            • Cor Ligthert

              #7
              Re: Sizechanged event question...

              "johnb41"
              [color=blue]
              > Thanks for the reply. You make it seem obvious! Could you describe
              > more of what you mean, as it's very unclear to me. Thanks!
              >[/color]
              \\\
              Static OldSize As Size
              If Not OldSize.Equals( MeControl.Size) Then
              ....procedure
              End If
              ///
              I hope this helps,

              Cor


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: Sizechanged event question...

                "johnb41" <orders@informa tik.com> schrieb:[color=blue]
                >I have a Picturebox that has an image displayed inside. Through code,
                > I was able to have the "whole image" display in it without distortion.
                > (Like a "Fit All" in most any image viewer program)
                >
                > Now, when the Picturebox gets resized by the user, I want the image to
                > recalculate and display as "Fit All" again.
                >
                > So I have code in my SizeChanged event of the picturebox. When the
                > "size changed", it does it's calculation, and displays properly.
                >
                > It works, but there is a problem: If the user drags the Picturebox
                > window larger or smaller, the SizeChanged event fires many times per
                > second. This causes alot of sluggishness in the app when trying to
                > display the image.[/color]

                If you are experiencing a flicker, you could enable double buffering for the
                picturebox control. To do so, create a class that inherits from
                'System.Windows .Forms.PictureB ox' and provides the constructor shown in the
                listing below:

                \\\
                Public Sub New()
                Me.SetStyle( _
                ControlStyles.R esizeRedraw Or _
                ControlStyles.D oubleBuffer Or _
                ControlStyles.A llPaintingInWmP aint, _
                True _
                )
                Me.UpdateStyles ()
                End Sub
                ///

                Then you use your custom picturebox control instead of the standard Windows
                Forms picturebox.

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://classicvb.org/petition/>

                Comment

                • johnb41

                  #9
                  Re: Sizechanged event question...

                  I gotta leave and do family stuff on this Memorial Day, so i'll only
                  post this quick message:

                  For me, the SizeChanged event seems to keep firing again and again,
                  until I let go of the mouse. This is in my real app, not the sample
                  one w/ the messagebox. For example, the image constantly tries to
                  redraw its self until I let go of the mouse.

                  I'll have to do more testing, and then play w/ Cor and herfried's
                  suggestions...

                  John

                  Comment

                  • johnb41

                    #10
                    Re: Sizechanged event question...

                    Thanks for the code. But it made no difference. Here is what I now
                    have:

                    Private Sub Pnl_Image_SizeC hanged(...) Handles
                    Pnl_Image.SizeC hanged
                    Static OldSize As Size
                    If Not OldSize.Equals( Pnl_Image.Size) Then
                    ' call procedure to resize and display image
                    End If
                    End Sub

                    What am I missing?

                    John

                    Comment

                    • Cor Ligthert

                      #11
                      Re: Sizechanged event question...

                      John,

                      I forgot it, however this is classic
                      [color=blue]
                      > Private Sub Pnl_Image_SizeC hanged(...) Handles
                      > Pnl_Image.SizeC hanged
                      > Static OldSize As Size
                      > If Not OldSize.Equals( Pnl_Image.Size) Then
                      > ' call procedure to resize and display image
                      > End If[/color]

                      OldSize = Pnl_Image.Size
                      [color=blue]
                      > End Sub[/color]

                      I hope this helps,

                      :-)

                      Cor


                      Comment

                      • johnb41

                        #12
                        Re: Sizechanged event question...

                        Cor,

                        That didn't make a difference. My procedure fires again and again
                        until I let go of the mouse.

                        It's actually not "as" bad now that i'm at work on a much faster
                        computer. But it's still annoying.

                        Any more suggestions, or should I just give up?

                        Thanks,
                        John

                        Comment

                        • Cor Ligthert

                          #13
                          Re: Sizechanged event question...

                          John,

                          Sorry, I was reading it not well I thought that the picture stayed the same,
                          of course does this not help.

                          The only thing that I can come up that can help you here is probably a
                          timer.
                          We people are not able to see some things that fast.

                          I never tried it, however you can probably do it in the same way as
                          mysample, and than there is not much to change.

                          Private Sub Pnl_Image_SizeC hanged(...) Handles
                          Pnl_Image.SizeC hanged
                          Static OldTime As Integer
                          If (Environment.Ti cksCount - OldTime) > 50 Then
                          ' call procedure to resize and display image
                          End If
                          OldTime = Environments.Ti cksCount
                          End Sub

                          And than set that 50 to a proper value.

                          Maybe this helps,

                          I am curious.

                          Cor


                          Comment

                          • johnb41

                            #14
                            Re: Sizechanged event question...

                            Cor,

                            Interesting idea, thanks!

                            But the only benefit it has is the image redraw happens a little less
                            frequently. And if I let go of the mouse at the wrong time, the image
                            doesn't redraw. I have to let go of the mouse immediately after it
                            redraws if I want the image displayed properly.

                            I played w/ the number value. A number too high refreshes too
                            infrequently, and often when I let go of the mouse, my subroutine
                            doesn't fire, and thus my image doesn't get resized/redrawn. A number
                            too low, and the effect is similar to the original code.

                            Well, I really appreciate all the help you've given me, Cor. But it
                            looks like this is pretty much impossible! :(

                            John

                            Comment

                            • Peter Proost

                              #15
                              Re: Sizechanged event question...

                              John, have you looked at my example?

                              in my opinion it does what you want.

                              Greetz Peter

                              --
                              Programming today is a race between software engineers striving to build
                              bigger and better idiot-proof programs, and the Universe trying to produce
                              bigger and better idiots. So far, the Universe is winning.

                              "johnb41" <orders@informa tik.com> schreef in bericht
                              news:1117551471 .570946.61990@g 44g2000cwa.goog legroups.com...[color=blue]
                              > Cor,
                              >
                              > Interesting idea, thanks!
                              >
                              > But the only benefit it has is the image redraw happens a little less
                              > frequently. And if I let go of the mouse at the wrong time, the image
                              > doesn't redraw. I have to let go of the mouse immediately after it
                              > redraws if I want the image displayed properly.
                              >
                              > I played w/ the number value. A number too high refreshes too
                              > infrequently, and often when I let go of the mouse, my subroutine
                              > doesn't fire, and thus my image doesn't get resized/redrawn. A number
                              > too low, and the effect is similar to the original code.
                              >
                              > Well, I really appreciate all the help you've given me, Cor. But it
                              > looks like this is pretty much impossible! :(
                              >
                              > John
                              >[/color]


                              Comment

                              Working...