Results 1 to 34 of 34

Thread: Utilizing drawstrings with slider bars

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Utilizing drawstrings with slider bars

    Hi guru,

    I am pretty new with vb.net draw functions and need a couple of guidance.

    I have a trackbar with a drawstring and drawing stars in there.

    Whenever I slide the trackbar, the text gets over-written on top or draw lines get overlap rather than redraw or refresh.
    What is the best way to refresh this? I am planning to make a custom control and utilize this multiple times on a form. Best if we can help to increase performance.

    Below is a code that I have, hope somebody could help me take a look at it.

    Name:  textoverlap.png
Views: 342
Size:  516 Bytes
    Code:
    Public Class Form2
        Dim number As Integer = 40
    
       Private Sub DrawText(ByVal g As Graphics, ByVal rect As Rectangle, ByVal percentage As Single)
    
            'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
            Using fnt As New Font(Me.Font.FontFamily, 14)
                Dim text As String = percentage.ToString + "%"
                Dim textSize = g.MeasureString(text, fnt)
                Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
                'now we have all the values draw the text
                g.DrawString(text, fnt, Brushes.Black, textPoint)
            End Using
        End Sub
    
        Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            Dim g As Graphics = CreateGraphics()
            i16Percent = TrackBar1.Value
    
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
            DrawText(g, New Rectangle(5, 5, 100, 100), number )
        End Sub
    End Class

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Utilizing drawstrings with slider bars

    Do a FillRectangle with the background color (to "erase" the previous text) where you're going to draw the text, before you draw the text. (see post #4 below)
    If it flashes too much, you're going to need to double buffer.
    Last edited by passel; Apr 8th, 2015 at 03:35 PM.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Utilizing drawstrings with slider bars

    Of course the way it should be done is just set the DoubledBuffered property (assuming you're using a form), and in the TrackBar1_Scroll just issue a Me.Invalidate() to trigger the Paint event.
    The Paint Event will provide you a graphics object (e.Graphics), and the old text will already have been erased for you with the background color. You just need to draw the new text.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Utilizing drawstrings with slider bars

    Didn't seem too flashy to me.
    Code:
      Private Sub DrawText(ByVal g As Graphics, ByVal rect As Rectangle, ByVal percentage As Single)
    
        'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
        Using fnt As New Font(Me.Font.FontFamily, 14)
          Dim text As String = percentage.ToString + "%"
          Dim textSize = g.MeasureString(text, fnt)
          Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
          'now we have all the values draw the text
          Using backBrush As New SolidBrush(Me.BackColor)
            g.FillRectangle(backBrush, rect)
          End Using
          g.DrawString(text, fnt, Brushes.Black, textPoint)
        End Using
      End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    Where do you put the Me.invalidate()?
    When i put that in there, it pretty much erased all the image when my image generates.

    Code:
        Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            Dim g As Graphics = CreateGraphics()
            Me.Invalidate()
            i16Percent = TrackBar1.Value
    
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
            DrawText(g, New Rectangle(5, 5, 100, 100), number )
        End Sub
    What's the benefit of drawing rectangle to erase the previous text in comparison with refresh() or invalidate()?

  6. #6
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    What passel was suggesting was that you use the form's paint event to do the drawing instead of creating a graphics object yourself on the scroll event.

    One of the good reasons to do so is that many things can cause the form to redraw itself.
    When this happens the paint event is fired and your drawing takes place.

    If you do not use the paint event you run the risk of what you have drawn will be overdrawn.
    when windows repaint the form.

    Example:
    Code:
    Public Class Form1
      Dim number As Integer = 0
    
      Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        number = TrackBar1.Value
        Me.Refresh()  ' or Me.Invalidate
      End Sub
    
      Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g As Graphics = e.Graphics
        'g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
        DrawText(g, New Rectangle(5, 5, 100, 100), number)
      End Sub
    
      Private Sub DrawText(ByVal g As Graphics, ByVal rect As Rectangle, ByVal percentage As Single)
        'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
        Using fnt As New Font(Me.Font.FontFamily, 14)
          Dim text As String = percentage.ToString + "%"
          Dim textSize = g.MeasureString(text, fnt)
          Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
          'now we have all the values draw the text
          Using backBrush As New SolidBrush(Me.BackColor)
            g.FillRectangle(backBrush, rect)
          End Using
          g.DrawString(text, fnt, Brushes.Black, textPoint)
        End Using
      End Sub
    End Class
    What I am confused about is why are you bothering draw on the form when you could simply change the text property of a label.

    Code:
      Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        Label1.Text = (TrackBar1.Value * 0.01).ToString("p0")
      End Sub
    Last edited by Gruff; Apr 8th, 2015 at 05:50 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    Gruff,

    I guess what I was trying to say is when i utilize the Me.Invalidate() shown in the code quote, i do not see the text image anymore. But if i put Me.refresh() i can see the text image.

    What is the best way should I draw on to if I would want to put it to the control toolbox control later for multiple usage?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Utilizing drawstrings with slider bars

    You should do all the control drawing in the On Paint event handler for the control. .Invalidate and .Refresh are very similar, but differ in a crucial way. .Invalidate will trigger the painting...eventually. All it does is invalidates the control and puts a paint event on the message queue. Whatever needs to be painted will be painted when the application gets around to pumping the message from the queue. Refresh is an imperative command, in contrast. When you call Refresh, the redraw happens immediately, without dealing with the message queue. That's great if you need to update the control immediately in the middle of running some code (which would otherwise block message processing), but Refresh isn't necessarily an efficient use of time. Invalidate pretty much ensures that the drawing will only happen when the UI has nothing better to do, whereas Refresh happens immediately, regardless of what else could be happening.

    Me.Invalidate and Me.Refresh, will cause the whole form to redraw, which is almost certainly overkill. Why draw the whole form when it is only one control that you really care about?

    On a different note: One of the advantages of using On Paint, aside from the fact that it is raised whenever ANYTHING causes the control to redraw, is the fact that you don't have to create a graphics object because one is provided for you in the e argument.
    My usual boring signature: Nothing

  9. #9
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Quote Originally Posted by AmazingTrans View Post
    Gruff,

    I guess what I was trying to say is when i utilize the Me.Invalidate() shown in the code quote, i do not see the text image anymore. But if i put Me.refresh() i can see the text image.
    The reason is you are creating a graphics object from the form (CreateGraphics) and drawing on it.
    Refresh or Invalidate creates its own graphics object and draws on it overwriting what you just drew.

    As Passel, Shaggy and I have been saying you should not be creating your own graphics object at all, but using the one in the Paint event.
    Quote Originally Posted by AmazingTrans View Post
    What is the best way should I draw on to if I would want to put it to the control toolbox control later for multiple usage?
    Ah! I missed that in your first post.
    You would either create your own control class from scratch or inherit an existing control class and use that control's paint event.

    Where on or near the scrollbar would you want the text? Fighting an existing control's drawing routines can sometimes be downright impossible. For example if you wanted the text in an area where the scrollbar was painting the slider.

    Another option would be to pop both a scrollbar and a label into the same usercontrol wrapper.
    This would most likely be the better approach.
    Last edited by Gruff; Apr 8th, 2015 at 06:20 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Utilizing drawstrings with slider bars

    As Shaggy said, a Control would be done differently, using the On Paint event.

    But for the contrived example you gave, the reason it "worked" with Refresh and not Invalidate, is for the reason that Shaggy mentioned. Refresh says update the form now, which wipes out the text, and then you draw on the form after that, so you see it.
    Invalidate says update the form later, when the GUI events are processed, then you draw on the form, and later the Paint event happens and erases your drawing.
    So, Refresh causes the Paint Event to happen before you draw, so the Paint Event prepares the surface (erases) before you draw on it.
    Invalidate causes the Paint Event to happen after you draw, so the Paint Event prepares the surface (erases) after you draw on it.

    You were suppose to do your drawing from the Paint event itself, as Gruff said, as the Paint event prepares the surface for you and you can immediately draw on it using code along these lines.
    Code:
      Dim i16Percent As Single
    
      Private Sub DrawText(ByVal g As Graphics, ByVal rect As Rectangle, ByVal percentage As Single)
        'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
        Using fnt As New Font(Me.Font.FontFamily, 14)
          Dim text As String = percentage.ToString + "%"
          Dim textSize = g.MeasureString(text, fnt)
          Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
          'now we have all the values draw the text
          g.DrawString(text, fnt, Brushes.Black, textPoint)
        End Using
      End Sub
    
      Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        DrawText(e.Graphics, New Rectangle(5, 5, 100, 100), i16Percent)
      End Sub
    
      Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        i16Percent = TrackBar1.Value
        Me.Invalidate(New Rectangle(5, 5, 100, 100))
      End Sub
    You invalidate the area that needs to be updated, and the next paint event, you draw it.
    You should notice when you start the program that you don't have to move the trackbar to see the first value displayed, since the paint event will happen and you will update the value.

    But, again, doing a control is a whole other matter, so this doesn't really apply. You will get the OnPaint event in the control and should do your drawing there.
    Last edited by passel; Apr 8th, 2015 at 06:28 PM.

  11. #11
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Here you go. I made a usercontrol in a test project.

    In your project right mouse on the project name in the solution browser and select Add. Then select user control.
    I named mine "HScrollBarEx".

    Once you are looking at the graphical usercontrol window add a horizonal scrollbar and a label to it.
    Be sure the autosize property of the label is true and dock the label to the right side of the control.
    Change its font size to 16.

    Next dock the scrollbar to fill the space to the left.
    Adjust the overall usercontrol height to match the label height.

    Open the code pane for the user control and paste the code from the next post into it.

    Build the project. Open the form and in the toolbox scroll to the very top and select the new usercontrol HScrollBarEx.
    Draw it on your form.

    You adjust the new control height by changing its font size. It works just like a textbox in single line mode.

    All the HScrollbar properties and events have been mapped to the new control so it should work just like it.

    Ummm... Just noticed that the usercontrol has a scroll event by default.
    You can Add a <Browseable(false)> attribute to a shadow copy of the event to hide it.
    I'd to this to avoid confusion with the 'Scrolling' event I provided.
    Last edited by Gruff; Apr 8th, 2015 at 07:50 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  12. #12
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Code:
    Imports System.ComponentModel
    
    Public Class HscrollBarEx
      Public Event Scrolling(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs)
    
    #Region "Hide Properties"
      <Browsable(False)> _
      Public Shadows Property BackColor As Color = Nothing
      <Browsable(False)> _
      Public Shadows Property BackGroundImage As Image = Nothing
    #End Region
    
    #Region "Mapped Properties"
      'Map internal scrollbar.value to usercontrol property
      Public Property Value() As Integer
        Get
          Return HScrollBar1.Value
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Value = nValue
        End Set
      End Property
    
      'Map internal scrollbar.Minimum to usercontrol property
      Public Property Minimum() As Integer
        Get
          Return HScrollBar1.Minimum
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Minimum = nValue
        End Set
      End Property
    
      'Map internal scrollbar.Maximum to usercontrol property
      Public Property Maximum() As Integer
        Get
          Return HScrollBar1.Maximum
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Maximum = nValue
        End Set
      End Property
    
      'Map internal scrollbar.SmallChange to usercontrol property
      Public Property SmallChange() As Integer
        Get
          Return HScrollBar1.SmallChange
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.SmallChange = nValue
        End Set
      End Property
    
      'Map internal scrollbar.LargeChange to usercontrol property
      Public Property LargeChange() As Integer
        Get
          Return HScrollBar1.LargeChange
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.LargeChange = nValue
        End Set
      End Property
    
      'Map internal label.ForeColor to a usercontrol property
      Public Shadows Property ForeColor() As Color
        Get
          Return Label1.ForeColor
        End Get
        Set(ByVal Value As Color)
          Label1.ForeColor = Value
        End Set
      End Property
    
      'Map internal label.Font to usercontrol property
      Public Shadows Property Font() As Font
        Get
          Return Label1.Font
        End Get
        Set(ByVal Value As Font)
          Label1.Font = Value
        End Set
      End Property
    #End Region
    
    #Region "Mapped Events"
      Private Sub HScrollBar1_Scroll(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        Label1.Text = HScrollBar1.Value.ToString & "%"
        RaiseEvent Scrolling(Me, New ScrollEventArgs(e.Type, e.OldValue, e.NewValue))
      End Sub
    #End Region
    
    #Region "Internal Events"
      Private Sub Label1_SizeChanged(sender As Object, e As System.EventArgs) Handles Label1.SizeChanged
        Me.Height = Label1.Height
      End Sub
    
      Private Sub HscrollBarEx_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
        Me.Height = Label1.Height
      End Sub
    #End Region
    
      Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        Me.Height = Label1.Height
        Me.BackColor = SystemColors.Control
        Me.Label1.BackColor = SystemColors.Control
      End Sub
    
    End Class
    Last edited by Gruff; Apr 8th, 2015 at 07:43 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    Quote Originally Posted by Gruff View Post
    What I am confused about is why are you bothering draw on the form when you could simply change the text property of a label.

    Gruff, I was just showing a sample of utilizing drawstring for text. I am actually utilizing other the drawing function such as drawcurve, drawline, drawpie.

  14. #14
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Fair enough.
    My point is that controls that redraw themselves a lot are difficult if not impossible to custom draw on.
    The vscrollbars and hscrollbars are this type of control.
    With an inherited control you only have the existing area of the control to work with.

    If you use a usercontrol you can make it larger than the original control and then draw to the area outside the scrollbar you want to use.

    In my example I am not doing any drawing but you could if you wanted to.
    Just remove the label control and position the hscrollbar where you want it (No fill dock) and draw away in the usercontrol paint event.

    Attachment 125517
    Last edited by Gruff; Apr 9th, 2015 at 10:14 AM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    First of all, thank you passell, Gruff, Shaggy for everybody support. I would have never learn much without you all.

    I guess I was doing it on form initially because I do not know how to create a usercontrol, but i do now.

    Gruff,
    I tried your code, but it gives me error on the Hide Prop region such as
    Error 2 Statement cannot appear within a property body. End of property assumed. C:\
    Error 1 End of statement expected. C:\HScrollBarEx.vb 30 46
    Error 3 Property missing 'End Property'. C:\\HScrollBarEx.vb 31 3

    I edited the code to this, not sure if this is correct.
    Code:
    #Region "Hide Properties"
        <Browsable(False)> _
        Public Shadows Property BackColor() As Color
            Get
                Return Nothing
            End Get
            Set(ByVal value As Color)
                Value = Nothing
            End Set
        End Property
    
        <Browsable(False)> _
          Public Shadows Property BackGroundImage() As Image
            Get
                Return Nothing
            End Get
            Set(ByVal value As Image)
                value = Nothing
            End Set
        End Property
    #End Region
    If it is not too much to request, I would like to add a paint property in that control code if possible: for example drawing a pie next to the hbar. Could you help me out with a sample on the paint event?

    Using progressPen As New Pen(Color.LightSalmon, 2)
    'set the smoothing to high quality for better output
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    Dim myrect As New Rectangle(10, 10, 50, 50)
    g.DrawPie(progressPen, myrect, 0, 360)
    End Using

  16. #16
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Honestly I do not know why you are getting those errors. I do not get them.

    You can remove that code and the control will still work.
    You will just see those properties in the control's properties when browsing during development.

    Here is HScrollBarEx2.
    It is just one hscrollbar dropped into a usercontrol. It will align the scrollbar to the left and center it vertically. It will keep it away from the usercontrol rightside by the distance of the usercontrol height. (Givng you a square drawing area to the right.)

    This is a one trick pony as I made no built-in adjustments to the height via the font. You will have to develop that yourself.

    Attachment 125539
    Code:
    Imports System.ComponentModel
    
    Public Class HScrollBarEx2
      Public Event Scrolling(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs)
    
    #Region "Mapped Properties"
      'Map internal scrollbar.value to usercontrol property
      Public Property Value() As Integer
        Get
          Return HScrollBar1.Value
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Value = nValue
        End Set
      End Property
    
      'Map internal scrollbar.Minimum to usercontrol property
      Public Property Minimum() As Integer
        Get
          Return HScrollBar1.Minimum
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Minimum = nValue
        End Set
      End Property
    
      'Map internal scrollbar.Maximum to usercontrol property
      Public Property Maximum() As Integer
        Get
          Return HScrollBar1.Maximum
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Maximum = nValue
        End Set
      End Property
    
      'Map internal scrollbar.SmallChange to usercontrol property
      Public Property SmallChange() As Integer
        Get
          Return HScrollBar1.SmallChange
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.SmallChange = nValue
        End Set
      End Property
    
      'Map internal scrollbar.LargeChange to usercontrol property
      Public Property LargeChange() As Integer
        Get
          Return HScrollBar1.LargeChange
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.LargeChange = nValue
        End Set
      End Property
    #End Region
    
    #Region "Mapped Events"
      Private Sub HScrollBar1_Scroll(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        Me.Refresh()
        RaiseEvent Scrolling(Me, New ScrollEventArgs(e.Type, e.OldValue, e.NewValue))
      End Sub
    #End Region
    
    #Region "Internal Events"
      Private Sub HScrollBarEx2_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        ' Center the scrollbar vertically in the control
        ' Abut the scrollbar right side to stop at a square rectangle (Right justified) 
        ' Set the scrollbar height to one third the control height
        HScrollBar1.Size = New Size(Me.Width - Me.Height, Me.Height \ 3)
        HScrollBar1.Location = New Point(0, (Me.Height \ 2) - (HScrollBar1.Height \ 2))
      End Sub
    #End Region
    
    #Region "Overridden Events"
    
      Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
    
        Dim g As Graphics = e.Graphics
    
        Using outlinepen As Pen = New Pen(Color.Black)
          Using fillbrush As New SolidBrush(Color.White)
            'set the smoothing to high quality for better output
            g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    
            Dim SF As New StringFormat
            SF.Alignment = StringAlignment.Center
            SF.LineAlignment = StringAlignment.Center
            ' Get text size
            Dim TextSize As SizeF = g.MeasureString("100%", Me.Font, 200, SF)
            'Convert to degrees
            Dim Degrees As Single = CSng(HScrollBar1.Value * 3.6)
            Dim myrect As Rectangle = New Rectangle(Me.Width - Me.Height, 0, Me.Height, Me.Height)
    
            myrect.Inflate(-3, -3)
            g.FillPie(fillbrush, myrect, 0, Degrees)
            g.DrawPie(outlinepen, myrect, 0, Degrees)
    
            myrect.Inflate(-10, -CInt((myrect.Height - TextSize.Height) / 2))
            g.FillRectangle(fillbrush, myrect)
            g.DrawRectangle(outlinepen, myrect)
            g.DrawString(HScrollBar1.Value.ToString & "%", Me.Font, Brushes.Black, myrect, SF)
          End Using
        End Using
      End Sub
    #End Region
    
      Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        Me.DoubleBuffered = True
        'Me.BackColor = SystemColors.Control
      End Sub
    End Class
    Last edited by Gruff; Apr 9th, 2015 at 02:53 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  17. #17
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    BTW. To get a scrollbar to reach the maximum value when Minimum = 0 and Maximum = 100.

    1) Set both the SmallChange and LargeChange properties to 1
    or
    2) Set the Maximum to 100 + the LargeChange value. (if LargeChange is 10 then maximum needs to be 110)
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Utilizing drawstrings with slider bars

    Quote Originally Posted by Gruff View Post
    Honestly I do not know why you are getting those errors. I do not get them.
    ...
    Perhaps ask which version of VB.Net is being used, or which FrameWork is being targeted.
    I know some things have changed in regards to Properties, etc... but I haven't played around with them enough myself to even be considered dangerous, so don't know if asking the version of VB is a good question or a dumb question or doesn't matter.

  19. #19
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Honestly I do not know why you are getting those errors. I do not get them.

    You can remove that code and the control will still work.
    You will just see those properties in the control's properties when browsing during development.

    Here is HScrollBarEx2.
    It is just one hscrollbar dropped into a usercontrol. It will align the scrollbar to the left and center it vertically. It will keep it away from the usercontrol rightside by the distance of the usercontrol height. (Givng you a square drawing area to the right.)

    This is a one trick pony as I made no build in adjustments to the height via the font. You will have to develop that yourself.
    Code:
    Imports System.ComponentModel
    
    Public Class HScrollBarEx2
      Public Event Scrolling(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs)
    
    #Region "Mapped Properties"
      'Map internal scrollbar.value to usercontrol property
      Public Property Value() As Integer
        Get
          Return HScrollBar1.Value
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Value = nValue
        End Set
      End Property
    
      'Map internal scrollbar.Minimum to usercontrol property
      Public Property Minimum() As Integer
        Get
          Return HScrollBar1.Minimum
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Minimum = nValue
        End Set
      End Property
    
      'Map internal scrollbar.Maximum to usercontrol property
      Public Property Maximum() As Integer
        Get
          Return HScrollBar1.Maximum
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.Maximum = nValue
        End Set
      End Property
    
      'Map internal scrollbar.SmallChange to usercontrol property
      Public Property SmallChange() As Integer
        Get
          Return HScrollBar1.SmallChange
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.SmallChange = nValue
        End Set
      End Property
    
      'Map internal scrollbar.LargeChange to usercontrol property
      Public Property LargeChange() As Integer
        Get
          Return HScrollBar1.LargeChange
        End Get
        Set(ByVal nValue As Integer)
          HScrollBar1.LargeChange = nValue
        End Set
      End Property
    #End Region
    
    #Region "Mapped Events"
      Private Sub HScrollBar1_Scroll(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
        Me.Refresh()
        RaiseEvent Scrolling(Me, New ScrollEventArgs(e.Type, e.OldValue, e.NewValue))
      End Sub
    #End Region
    
    #Region "Internal Events"
      Private Sub HScrollBarEx2_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        ' Center the scrollbar vertically in the control
        ' Abut the scrollbar right side to stop at a square rectangle (Right justified) 
        ' Set the scrollbar height to one third the control height
        HScrollBar1.Size = New Size(Me.Width - Me.Height, Me.Height \ 3)
        HScrollBar1.Location = New Point(0, (Me.Height \ 2) - (HScrollBar1.Height \ 2))
      End Sub
    #End Region
    
    #Region "Overridden Events"
    
      Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
    
        Dim g As Graphics = e.Graphics
    
        Using outlinepen As Pen = New Pen(Color.Black)
          Using fillbrush As New SolidBrush(Color.White)
            'set the smoothing to high quality for better output
            g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    
            Dim SF As New StringFormat
            SF.Alignment = StringAlignment.Center
            SF.LineAlignment = StringAlignment.Center
            ' Get text size
            Dim TextSize As SizeF = g.MeasureString("100%", Me.Font, 200, SF)
            'Convert to degrees
            Dim Degrees As Single = CSng(HScrollBar1.Value * 3.6)
            Dim myrect As Rectangle = New Rectangle(Me.Width - Me.Height, 0, Me.Height, Me.Height)
    
            myrect.Inflate(-3, -3)
            g.FillPie(fillbrush, myrect, 0, Degrees)
            g.DrawPie(outlinepen, myrect, 0, Degrees)
    
            myrect.Inflate(-10, -CInt((myrect.Height - TextSize.Height) / 2))
            g.FillRectangle(fillbrush, myrect)
            g.DrawRectangle(outlinepen, myrect)
            g.DrawString(HScrollBar1.Value.ToString & "%", Me.Font, Brushes.Black, myrect, SF)
          End Using
        End Using
      End Sub
    #End Region
    
      Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        Me.DoubleBuffered = True
        'Me.BackColor = SystemColors.Control
      End Sub
    End Class
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  20. #20

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    I'm using vs08 with .net 3.5 sp1

  21. #21
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Utilizing drawstrings with slider bars

    Yes, I believe those were shorthand property declarations which was introduced with VB.Net 2010. .net 4.0 (from what I've heard. , no personal experience... )

  22. #22
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Oh! For some reason I was under the impression you were using VB.NET 2013 CE.

    Yes I am using 2010-2013 these days. I use the shorthand property declarations when ever possible.
    Saves one heck of a lot of typing.

    Have you thought about upgrading from 2008 to the free 2013 CE (Community Edition)?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  23. #23

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    Thanks for everything, everyone!!!
    Gruff, your snippets & explanation help me a lot here to get forward.

    One last question,
    If I were to lay this usercontrol ontop of another usercontrol, I would want the yellow area to show what's on the other usercontrol? For example i put a label behind this saying "Hello". We can see that the later word got concatenated.

    Name:  test.png
Views: 158
Size:  4.4 KB

    Hope to hear from everyone!

    Cheerios!

  24. #24
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Now your getting into bigger issues.

    I haven't had a need to learn how to do transparent background yet.

    Perhaps Passel or one of the other gurus here will jump in.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  25. #25
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Utilizing drawstrings with slider bars

    Just another note about refresh and invalidate ... it isn't just the instant redraw difference ... invalidate will also just draw the single control you are calling it on (and transparent controls within that control ... and places where your sub controls call things like .DrawParentBackground) where refresh will also paint ALL sub controls... which can be quite inefficient!

    If you really want to repaint something when running code I would NEVER suggest using .Refresh (unless that's actually what you want to do for some reason) ... I would suggest that you look into threading in this case instead.

  26. #26
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Utilizing drawstrings with slider bars

    As for overlapping controls ... .Net transparency isn't really transparent ... it supports drawing its parents background ONLY (VisualStyleRenderer.DrawParentBackground) so it doesn't work for overlapping controls...

    There used to be a "TransparentKey" (or similar) property in some languages ... but I don't think there is one in .Net ... but Googling TransparentKey control c sharp should turn up something.

    Kris

  27. #27

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    Quote Originally Posted by Gruff View Post
    Oh! For some reason I was under the impression you were using VB.NET 2013 CE.

    Yes I am using 2010-2013 these days. I use the shorthand property declarations when ever possible.
    Saves one heck of a lot of typing.

    Have you thought about upgrading from 2008 to the free 2013 CE (Community Edition)?
    Ahah, I will check that out.
    Gruff, I have a q10,
    If i were put this control (HScrollBarEx21) onto a form, then i have an Horizontalbar call HBar1 on the Form.
    I guess when i do something like this, i guess the pie in the usercontrol doesn't update but the Horizontal Bar in usercontrol does follow.
    What can i do to make the pie draw?

    Code:
        Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
            HScrollBarEx21.HValue = HScrollBar1.Value
            TextBox1.Text = HScrollBar1.Value
    
        End Sub

  28. #28
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Why would you ever want to do such a thing?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  29. #29

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    Gruff, let's see, I actually have this draw function which will be in the usercontrol. The draw function draws a clock / gauges. This usercontrol will be duplicate multiple times onto a form. The gauges / clock will be dependent on textbox and/or function that is located on the form. When the values changes, the clock/gauges value will change too.

  30. #30
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Utilizing drawstrings with slider bars

    It's easy to make a custom control with transparent holes by using the Region property. For example, you could put something like this in the Load event handler of your UserControl:
    Code:
    		Dim r As Region = New Region(Me.ClientRectangle)
    		r.Exclude(New Rectangle(50, 50, 100, 100))
    		Me.Region = r
    You can provide a GraphicsPath instead of a rectangle as arguments to the region's constructor and other methods. So any shaped control and any shaped holes are possible.

    TransparencyKey can be used to do something similar but only for a Form.

    BB

  31. #31
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    Quote Originally Posted by AmazingTrans View Post
    Gruff, let's see, I actually have this draw function which will be in the usercontrol. The draw function draws a clock / gauges. This usercontrol will be duplicate multiple times onto a form. The gauges / clock will be dependent on textbox and/or function that is located on the form. When the values changes, the clock/gauges value will change too.
    So you did not need a scrollbar built into the usercontrol in the first place?
    You see my confusion?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  32. #32

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    Gruff, you are on the spot all the way.
    Yes i needed scrollbar built in there too, but that I am using that on the other UC & you did help me clarify a lot of information and jumpstart me quick.

    The reason I am asking about how to trigger the refresh is because at the same time after that I tried another UC where I want an event in the UC to be triggered when there is a change of values in the UC property values by a textbox in a form and this UC event will do the Refresh to repaint.

    Sorry for the confusion. Didn't want to open another thread since it is pretty related in terms of functionality for UC.

    Thanks!

  33. #33
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Utilizing drawstrings with slider bars

    The confusion is the reason you should open new threads. You can always add a link from the old thread to the new.

    Are you saying you now have a control with just the pie display alone and you want to drive it from outside the control?
    If so then you have to create a property that will hold the equivalent of the scrollbar value property and have that repaint the control via a refresh command inside the 'value' property 'set' section.

    You will note that Visual Studio will always use the name 'value' for the property argument inside the property by default. This will conflict with a property you want to name 'Value'. My general fix for this is to name the argument something other than value.
    Code:
      Private _Value As Integer
    
      Public Property Value() As Integer
        Get
          Return _Value
        End Get
        Set(ByVal PropValue As Integer)  '<--- PropValue is normally named value by Visual Studio
          _Value = PropValue
          Me.Refresh()
        End Set
      End Property
    Last edited by Gruff; Apr 13th, 2015 at 01:21 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  34. #34

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    41

    Re: Utilizing drawstrings with slider bars

    Gruff, Thanks for the help.
    I noticed when i put multiple UC, the memory started off with 15Mb. Not sure if that's normal.
    Or is there something about dispose() i have to utilize in the code under paint events?

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