Not understanding OnPaint in a control

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

    Not understanding OnPaint in a control

    Hello all,

    I am trying to override OnPaint in a custom textbox
    control (so I can drawstring a caption, etc.). In the
    code below, I get the "painting the form" message as
    expected, but not the "painting the control". It also
    has no effect to explicitly call txtTest.Invalid ate or
    txtTest.Refresh . Am I missing something simple here?
    Any push in the right direction is appreciated.

    BB

    Public Class Form1
    Inherits System.Windows. Forms.Form

    #Region " Windows Form Designer generated code "
    #End Region

    Private Sub Form1_Load(ByVa l sender As System.Object,
    ByVal e As System.EventArg s) Handles MyBase.Load
    Dim txtTest As New MyTextBox
    Controls.Add(tx tTest)
    End Sub

    Protected Overrides Sub onpaint(ByVal e As
    PaintEventArgs)
    MessageBox.Show ("Painting the form...")
    End Sub

    End Class

    Public Class MyTextBox
    Inherits TextBox

    Protected Overrides Sub onpaint(ByVal e As
    PaintEventArgs)
    MessageBox.Show ("Painting the textbox...")
    End Sub

    End Class

  • Herfried K. Wagner [MVP]

    #2
    Re: Not understanding OnPaint in a control

    * "BB" <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
    > I am trying to override OnPaint in a custom textbox
    > control (so I can drawstring a caption, etc.). In the
    > code below, I get the "painting the form" message as
    > expected, but not the "painting the control". It also[/color]

    Call 'MyBase.OnPaint (e)' in the handler on 'Form1'.

    --
    Herfried K. Wagner [MVP]
    <http://www.mvps.org/dotnet>

    Comment

    • One Handed Man [ OHM# ]

      #3
      Re: Not understanding OnPaint in a control

      Post your code . . .




      BB wrote:[color=blue]
      > Hello all,
      >
      > I am trying to override OnPaint in a custom textbox
      > control (so I can drawstring a caption, etc.). In the
      > code below, I get the "painting the form" message as
      > expected, but not the "painting the control". It also
      > has no effect to explicitly call txtTest.Invalid ate or
      > txtTest.Refresh . Am I missing something simple here?
      > Any push in the right direction is appreciated.
      >
      > BB
      >
      > Public Class Form1
      > Inherits System.Windows. Forms.Form
      >
      > #Region " Windows Form Designer generated code "
      > #End Region
      >
      > Private Sub Form1_Load(ByVa l sender As System.Object,
      > ByVal e As System.EventArg s) Handles MyBase.Load
      > Dim txtTest As New MyTextBox
      > Controls.Add(tx tTest)
      > End Sub
      >
      > Protected Overrides Sub onpaint(ByVal e As
      > PaintEventArgs)
      > MessageBox.Show ("Painting the form...")
      > End Sub
      >
      > End Class
      >
      > Public Class MyTextBox
      > Inherits TextBox
      >
      > Protected Overrides Sub onpaint(ByVal e As
      > PaintEventArgs)
      > MessageBox.Show ("Painting the textbox...")
      > End Sub
      >
      > End Class[/color]

      Regards - OHM# OneHandedMan@BT Internet.com


      Comment

      • Brian

        #4
        Re: Not understanding OnPaint in a control

        It's because the Textbox doesn't call the OnPaint Event.

        "BB" <anonymous@disc ussions.microso ft.com> wrote in message
        news:0b3001c3c4 c3$e88fc970$a50 1280a@phx.gbl.. .[color=blue]
        > Hello all,
        >
        > I am trying to override OnPaint in a custom textbox
        > control (so I can drawstring a caption, etc.). In the
        > code below, I get the "painting the form" message as
        > expected, but not the "painting the control". It also
        > has no effect to explicitly call txtTest.Invalid ate or
        > txtTest.Refresh . Am I missing something simple here?
        > Any push in the right direction is appreciated.
        >
        > BB
        >
        > Public Class Form1
        > Inherits System.Windows. Forms.Form
        >
        > #Region " Windows Form Designer generated code "
        > #End Region
        >
        > Private Sub Form1_Load(ByVa l sender As System.Object,
        > ByVal e As System.EventArg s) Handles MyBase.Load
        > Dim txtTest As New MyTextBox
        > Controls.Add(tx tTest)
        > End Sub
        >
        > Protected Overrides Sub onpaint(ByVal e As
        > PaintEventArgs)
        > MessageBox.Show ("Painting the form...")
        > End Sub
        >
        > End Class
        >
        > Public Class MyTextBox
        > Inherits TextBox
        >
        > Protected Overrides Sub onpaint(ByVal e As
        > PaintEventArgs)
        > MessageBox.Show ("Painting the textbox...")
        > End Sub
        >
        > End Class
        >[/color]


        Comment

        • Guest's Avatar

          #5
          Re: Not understanding OnPaint in a control

          Still nothing...
          [color=blue]
          >-----Original Message-----
          >* "BB" <anonymous@disc ussions.microso ft.com> scripsit:[color=green]
          >> I am trying to override OnPaint in a custom textbox
          >> control (so I can drawstring a caption, etc.). In the
          >> code below, I get the "painting the form" message as
          >> expected, but not the "painting the control". It also[/color]
          >
          >Call 'MyBase.OnPaint (e)' in the handler on 'Form1'.
          >
          >--
          >Herfried K. Wagner [MVP]
          ><http://www.mvps.org/dotnet>
          >.
          >[/color]

          Comment

          • Brian

            #6
            Re: Not understanding OnPaint in a control

            Basically, you're not allowed to paint on the textbox. Imagine the chaos if
            a user tries to highlight, cut or copy your text.
            You'll have to write your own textbox to do custom drawing. That's what I
            did.

            "BB" <anonymous@disc ussions.microso ft.com> wrote in message
            news:0b3001c3c4 c3$e88fc970$a50 1280a@phx.gbl.. .[color=blue]
            > Hello all,
            >
            > I am trying to override OnPaint in a custom textbox
            > control (so I can drawstring a caption, etc.). In the
            > code below, I get the "painting the form" message as
            > expected, but not the "painting the control". It also
            > has no effect to explicitly call txtTest.Invalid ate or
            > txtTest.Refresh . Am I missing something simple here?
            > Any push in the right direction is appreciated.
            >
            > BB
            >
            > Public Class Form1
            > Inherits System.Windows. Forms.Form
            >
            > #Region " Windows Form Designer generated code "
            > #End Region
            >
            > Private Sub Form1_Load(ByVa l sender As System.Object,
            > ByVal e As System.EventArg s) Handles MyBase.Load
            > Dim txtTest As New MyTextBox
            > Controls.Add(tx tTest)
            > End Sub
            >
            > Protected Overrides Sub onpaint(ByVal e As
            > PaintEventArgs)
            > MessageBox.Show ("Painting the form...")
            > End Sub
            >
            > End Class
            >
            > Public Class MyTextBox
            > Inherits TextBox
            >
            > Protected Overrides Sub onpaint(ByVal e As
            > PaintEventArgs)
            > MessageBox.Show ("Painting the textbox...")
            > End Sub
            >
            > End Class
            >[/color]


            Comment

            • BB

              #7
              Re: Not understanding OnPaint in a control

              There's nothing more to it than what's below. I've since
              made sure I'm calling mybase.onpaint in both the form and
              the textbox classes, but that hasn't changed anything.

              BB[color=blue]
              >-----Original Message-----
              >Post your code . . .
              >
              >
              >
              >
              >BB wrote:[color=green]
              >> Hello all,
              >>
              >> I am trying to override OnPaint in a custom textbox
              >> control (so I can drawstring a caption, etc.). In the
              >> code below, I get the "painting the form" message as
              >> expected, but not the "painting the control". It also
              >> has no effect to explicitly call txtTest.Invalid ate or
              >> txtTest.Refresh . Am I missing something simple here?
              >> Any push in the right direction is appreciated.
              >>
              >> BB
              >>
              >> Public Class Form1
              >> Inherits System.Windows. Forms.Form
              >>
              >> #Region " Windows Form Designer generated code "
              >> #End Region
              >>
              >> Private Sub Form1_Load(ByVa l sender As[/color][/color]
              System.Object,[color=blue][color=green]
              >> ByVal e As System.EventArg s) Handles MyBase.Load
              >> Dim txtTest As New MyTextBox
              >> Controls.Add(tx tTest)
              >> End Sub
              >>
              >> Protected Overrides Sub onpaint(ByVal e As
              >> PaintEventArgs)
              >> MessageBox.Show ("Painting the form...")
              >> End Sub
              >>
              >> End Class
              >>
              >> Public Class MyTextBox
              >> Inherits TextBox
              >>
              >> Protected Overrides Sub onpaint(ByVal e As
              >> PaintEventArgs)
              >> MessageBox.Show ("Painting the textbox...")
              >> End Sub
              >>
              >> End Class[/color]
              >
              >Regards - OHM# OneHandedMan@BT Internet.com
              >
              >
              >.
              >[/color]

              Comment

              • BB

                #8
                Re: Not understanding OnPaint in a control

                I've since tried that, but it hasn't changed. And if I'm
                understanding this right, shouldn't the textbox be
                *getting* the paint event when the form is invalidated
                and forced to repaint (which I presume the form should
                then pass along to its controls)?

                BB[color=blue]
                >-----Original Message-----
                >It's because the Textbox doesn't call the OnPaint Event.
                >
                >"BB" <anonymous@disc ussions.microso ft.com> wrote in[/color]
                message[color=blue]
                >news:0b3001c3c 4c3$e88fc970$a5 01280a@phx.gbl. ..[color=green]
                >> Hello all,
                >>
                >> I am trying to override OnPaint in a custom textbox
                >> control (so I can drawstring a caption, etc.). In the
                >> code below, I get the "painting the form" message as
                >> expected, but not the "painting the control". It also
                >> has no effect to explicitly call txtTest.Invalid ate or
                >> txtTest.Refresh . Am I missing something simple here?
                >> Any push in the right direction is appreciated.
                >>
                >> BB
                >>
                >> Public Class Form1
                >> Inherits System.Windows. Forms.Form
                >>
                >> #Region " Windows Form Designer generated code "
                >> #End Region
                >>
                >> Private Sub Form1_Load(ByVa l sender As[/color][/color]
                System.Object,[color=blue][color=green]
                >> ByVal e As System.EventArg s) Handles MyBase.Load
                >> Dim txtTest As New MyTextBox
                >> Controls.Add(tx tTest)
                >> End Sub
                >>
                >> Protected Overrides Sub onpaint(ByVal e As
                >> PaintEventArgs)
                >> MessageBox.Show ("Painting the form...")
                >> End Sub
                >>
                >> End Class
                >>
                >> Public Class MyTextBox
                >> Inherits TextBox
                >>
                >> Protected Overrides Sub onpaint(ByVal e As
                >> PaintEventArgs)
                >> MessageBox.Show ("Painting the textbox...")
                >> End Sub
                >>
                >> End Class
                >>[/color]
                >
                >
                >.
                >[/color]

                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Not understanding OnPaint in a control

                  * <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
                  > Still nothing...[/color]

                  Add 'MyBase.OnPaint (e)' to both of your 'OnPaint' methods. But I don't
                  understand your code -- one of the method is defined for the form, the
                  other for the textbox. What exactly do you want to archieve?

                  --
                  Herfried K. Wagner [MVP]
                  <http://www.mvps.org/dotnet>

                  Comment

                  • BB

                    #10
                    Re: Not understanding OnPaint in a control

                    I've added the calls to the base class in each case, but
                    still nothing. Per Brian elsewhere in this thread,
                    you're not allowed to get the paint message in the
                    textbox, but I'm still not sure why.

                    My code in both the form and the textbox is for
                    diagnostics only, to understand which events are firing
                    and when.

                    What I'm really trying to do is create a textbox that 1)
                    Has its own caption (without incurring the overhead of an
                    additional container control that is label + textbox,
                    e.g. do a straight "drawstring " instead) and 2) Has the
                    background hatched in a way to indicate that this is
                    a "required" input field.

                    I'm thinking the cleanest way to do this is to hook
                    OnPaint for the textbox, then do my extra stuff whenever
                    the control is painted. I know there are other ways to
                    skin this cat, but I'd love to understand why what I'm
                    trying to do won't work.

                    Any additional thought is appreciated.

                    Thanks,

                    BB
                    [color=blue]
                    >-----Original Message-----
                    >* <anonymous@disc ussions.microso ft.com> scripsit:[color=green]
                    >> Still nothing...[/color]
                    >
                    >Add 'MyBase.OnPaint (e)' to both of your 'OnPaint'[/color]
                    methods. But I don't[color=blue]
                    >understand your code -- one of the method is defined for[/color]
                    the form, the[color=blue]
                    >other for the textbox. What exactly do you want to[/color]
                    archieve?[color=blue]
                    >
                    >--
                    >Herfried K. Wagner [MVP]
                    ><http://www.mvps.org/dotnet>
                    >.
                    >[/color]

                    Comment

                    • BB

                      #11
                      Re: Not understanding OnPaint in a control

                      Brian, thanks for your comments. But, I still don't
                      understand. I know that hooking the paint event is
                      scary, but if it were disallowed altogether I would think
                      that would be true for *any* control, and in that case
                      why would there even be an OnPaint accessible at the
                      control level?
                      [color=blue]
                      >-----Original Message-----
                      >Basically, you're not allowed to paint on the textbox.[/color]
                      Imagine the chaos if[color=blue]
                      >a user tries to highlight, cut or copy your text.
                      >You'll have to write your own textbox to do custom[/color]
                      drawing. That's what I[color=blue]
                      >did.
                      >
                      >"BB" <anonymous@disc ussions.microso ft.com> wrote in[/color]
                      message[color=blue]
                      >news:0b3001c3c 4c3$e88fc970$a5 01280a@phx.gbl. ..[color=green]
                      >> Hello all,
                      >>
                      >> I am trying to override OnPaint in a custom textbox
                      >> control (so I can drawstring a caption, etc.). In the
                      >> code below, I get the "painting the form" message as
                      >> expected, but not the "painting the control". It also
                      >> has no effect to explicitly call txtTest.Invalid ate or
                      >> txtTest.Refresh . Am I missing something simple here?
                      >> Any push in the right direction is appreciated.
                      >>
                      >> BB
                      >>
                      >> Public Class Form1
                      >> Inherits System.Windows. Forms.Form
                      >>
                      >> #Region " Windows Form Designer generated code "
                      >> #End Region
                      >>
                      >> Private Sub Form1_Load(ByVa l sender As[/color][/color]
                      System.Object,[color=blue][color=green]
                      >> ByVal e As System.EventArg s) Handles MyBase.Load
                      >> Dim txtTest As New MyTextBox
                      >> Controls.Add(tx tTest)
                      >> End Sub
                      >>
                      >> Protected Overrides Sub onpaint(ByVal e As
                      >> PaintEventArgs)
                      >> MessageBox.Show ("Painting the form...")
                      >> End Sub
                      >>
                      >> End Class
                      >>
                      >> Public Class MyTextBox
                      >> Inherits TextBox
                      >>
                      >> Protected Overrides Sub onpaint(ByVal e As
                      >> PaintEventArgs)
                      >> MessageBox.Show ("Painting the textbox...")
                      >> End Sub
                      >>
                      >> End Class
                      >>[/color]
                      >
                      >
                      >.
                      >[/color]

                      Comment

                      • Codemonkey

                        #12
                        Re: Not understanding OnPaint in a control

                        In order for the OnPaint method to be called, you need to tell the control
                        to be drawn manually instead of by the OS in the constructor of the control.

                        Example:
                        -------------------
                        Public Class MyTextBox
                        Inherits TextBox

                        Public Sub New()
                        MyBase.New()

                        ' Set the UserPaint Style
                        Me.SetStyle(Con trolStyles.User Paint, True)

                        End Sub

                        ' OnPaint should now be called
                        Protected Overrides Sub onpaint(ByVal e As PaintEventArgs)

                        ' Make sure to call the base paint
                        Mybase.OnPaint( e)
                        End Sub

                        End Class
                        -------------------



                        You will still need to be careful when painting the textbox because of the
                        way it paints text and selected text. Refer to
                        microsoft.publi c.dotnet.framew ork.drawing if you need help with the drawing
                        methods.

                        Hope this helps,

                        Trev.


                        "BB" <anonymous@disc ussions.microso ft.com> wrote in message
                        news:0b3001c3c4 c3$e88fc970$a50 1280a@phx.gbl.. .[color=blue]
                        > Hello all,
                        >
                        > I am trying to override OnPaint in a custom textbox
                        > control (so I can drawstring a caption, etc.). In the
                        > code below, I get the "painting the form" message as
                        > expected, but not the "painting the control". It also
                        > has no effect to explicitly call txtTest.Invalid ate or
                        > txtTest.Refresh . Am I missing something simple here?
                        > Any push in the right direction is appreciated.
                        >
                        > BB
                        >
                        > Public Class Form1
                        > Inherits System.Windows. Forms.Form
                        >
                        > #Region " Windows Form Designer generated code "
                        > #End Region
                        >
                        > Private Sub Form1_Load(ByVa l sender As System.Object,
                        > ByVal e As System.EventArg s) Handles MyBase.Load
                        > Dim txtTest As New MyTextBox
                        > Controls.Add(tx tTest)
                        > End Sub
                        >
                        > Protected Overrides Sub onpaint(ByVal e As
                        > PaintEventArgs)
                        > MessageBox.Show ("Painting the form...")
                        > End Sub
                        >
                        > End Class
                        >
                        > Public Class MyTextBox
                        > Inherits TextBox
                        >
                        > Protected Overrides Sub onpaint(ByVal e As
                        > PaintEventArgs)
                        > MessageBox.Show ("Painting the textbox...")
                        > End Sub
                        >
                        > End Class
                        >[/color]


                        Comment

                        • BB

                          #13
                          Re: Not understanding OnPaint in a control

                          Trev, this is brilliant, thanks. I made the change and
                          am now hooking the event the way I want. You're right
                          that now I've got a bunch of weirdness with how the
                          control operates (for starters, the font is very odd, the
                          standard control keys don't work within the form, etc.),
                          but you've got me headed in the right direction, and I'll
                          check out microsoft.publi c.dotnet.framew ork.drawing.

                          Thanks,

                          Bill
                          [color=blue]
                          >-----Original Message-----
                          >In order for the OnPaint method to be called, you need[/color]
                          to tell the control[color=blue]
                          >to be drawn manually instead of by the OS in the[/color]
                          constructor of the control.[color=blue]
                          >
                          >Example:
                          >-------------------
                          >Public Class MyTextBox
                          > Inherits TextBox
                          >
                          > Public Sub New()
                          > MyBase.New()
                          >
                          > ' Set the UserPaint Style
                          > Me.SetStyle(Con trolStyles.User Paint, True)
                          >
                          > End Sub
                          >
                          > ' OnPaint should now be called
                          > Protected Overrides Sub onpaint(ByVal e As[/color]
                          PaintEventArgs)[color=blue]
                          >
                          > ' Make sure to call the base paint
                          > Mybase.OnPaint( e)
                          > End Sub
                          >
                          >End Class
                          >-------------------
                          >
                          >
                          >
                          >You will still need to be careful when painting the[/color]
                          textbox because of the[color=blue]
                          >way it paints text and selected text. Refer to
                          >microsoft.publ ic.dotnet.frame work.drawing if you need[/color]
                          help with the drawing[color=blue]
                          >methods.
                          >
                          >Hope this helps,
                          >
                          >Trev.
                          >
                          >
                          >"BB" <anonymous@disc ussions.microso ft.com> wrote in[/color]
                          message[color=blue]
                          >news:0b3001c3c 4c3$e88fc970$a5 01280a@phx.gbl. ..[color=green]
                          >> Hello all,
                          >>
                          >> I am trying to override OnPaint in a custom textbox
                          >> control (so I can drawstring a caption, etc.). In the
                          >> code below, I get the "painting the form" message as
                          >> expected, but not the "painting the control". It also
                          >> has no effect to explicitly call txtTest.Invalid ate or
                          >> txtTest.Refresh . Am I missing something simple here?
                          >> Any push in the right direction is appreciated.
                          >>
                          >> BB
                          >>
                          >> Public Class Form1
                          >> Inherits System.Windows. Forms.Form
                          >>
                          >> #Region " Windows Form Designer generated code "
                          >> #End Region
                          >>
                          >> Private Sub Form1_Load(ByVa l sender As[/color][/color]
                          System.Object,[color=blue][color=green]
                          >> ByVal e As System.EventArg s) Handles MyBase.Load
                          >> Dim txtTest As New MyTextBox
                          >> Controls.Add(tx tTest)
                          >> End Sub
                          >>
                          >> Protected Overrides Sub onpaint(ByVal e As
                          >> PaintEventArgs)
                          >> MessageBox.Show ("Painting the form...")
                          >> End Sub
                          >>
                          >> End Class
                          >>
                          >> Public Class MyTextBox
                          >> Inherits TextBox
                          >>
                          >> Protected Overrides Sub onpaint(ByVal e As
                          >> PaintEventArgs)
                          >> MessageBox.Show ("Painting the textbox...")
                          >> End Sub
                          >>
                          >> End Class
                          >>[/color]
                          >
                          >
                          >.
                          >[/color]

                          Comment

                          • Brian

                            #14
                            Re: Not understanding OnPaint in a control


                            If you do what codemonkey suggests and use owner drawn
                            style then you'll have to paint the entire text box
                            yourself. Do you know what that entails? You'll have to
                            repaint the client area every time you receive a paint
                            message. You have make sure that you have the correct
                            lines displayed and if the some of the text is
                            highlighted or not (there is much more to consider). This
                            shouldn't be taken lightly. Microsoft makes getting the
                            Onpaint method difficult for a reason, it's because
                            everything must fit together in order to make a text box
                            work properly. If you paint the text in the wrong
                            position or with the wrong font size then it won't work.

                            Lastly, if want a text box that will display only one
                            line of text then you should create your own. It isn't
                            that hard and you can use any background you want.

                            [color=blue]
                            >-----Original Message-----
                            >Brian, thanks for your comments. But, I still don't
                            >understand. I know that hooking the paint event is
                            >scary, but if it were disallowed altogether I would[/color]
                            think[color=blue]
                            >that would be true for *any* control, and in that case
                            >why would there even be an OnPaint accessible at the
                            >control level?
                            >[color=green]
                            >>-----Original Message-----
                            >>Basically, you're not allowed to paint on the textbox.[/color]
                            >Imagine the chaos if[color=green]
                            >>a user tries to highlight, cut or copy your text.
                            >>You'll have to write your own textbox to do custom[/color]
                            >drawing. That's what I[color=green]
                            >>did.
                            >>
                            >>"BB" <anonymous@disc ussions.microso ft.com> wrote in[/color]
                            >message[color=green]
                            >>news:0b3001c3 c4c3$e88fc970$a 501280a@phx.gbl ...[color=darkred]
                            >>> Hello all,
                            >>>
                            >>> I am trying to override OnPaint in a custom textbox
                            >>> control (so I can drawstring a caption, etc.). In the
                            >>> code below, I get the "painting the form" message as
                            >>> expected, but not the "painting the control". It also
                            >>> has no effect to explicitly call txtTest.Invalid ate or
                            >>> txtTest.Refresh . Am I missing something simple here?
                            >>> Any push in the right direction is appreciated.
                            >>>
                            >>> BB
                            >>>
                            >>> Public Class Form1
                            >>> Inherits System.Windows. Forms.Form
                            >>>
                            >>> #Region " Windows Form Designer generated code "
                            >>> #End Region
                            >>>
                            >>> Private Sub Form1_Load(ByVa l sender As[/color][/color]
                            >System.Objec t,[color=green][color=darkred]
                            >>> ByVal e As System.EventArg s) Handles MyBase.Load
                            >>> Dim txtTest As New MyTextBox
                            >>> Controls.Add(tx tTest)
                            >>> End Sub
                            >>>
                            >>> Protected Overrides Sub onpaint(ByVal e As
                            >>> PaintEventArgs)
                            >>> MessageBox.Show ("Painting the form...")
                            >>> End Sub
                            >>>
                            >>> End Class
                            >>>
                            >>> Public Class MyTextBox
                            >>> Inherits TextBox
                            >>>
                            >>> Protected Overrides Sub onpaint(ByVal e As
                            >>> PaintEventArgs)
                            >>> MessageBox.Show ("Painting the textbox...")
                            >>> End Sub
                            >>>
                            >>> End Class
                            >>>[/color]
                            >>
                            >>
                            >>.
                            >>[/color]
                            >.
                            >[/color]

                            Comment

                            • Codemonkey

                              #15
                              Re: Not understanding OnPaint in a control

                              > then you'll have to paint the entire text box[color=blue]
                              > yourself. Do you know what that entails?[/color]

                              If you call the MyBase.OnPaint( ) method, then this will draw the textbox
                              automatically (text, highlights and all). Granted, you'll have to take care
                              that the extra graphics don't overshadow the textbox's own graphics.
                              [color=blue]
                              > You'll have to repaint the client area
                              > every time you receive a paint message.[/color]

                              Isn't this what you're supposed to do every time the client area is
                              invalidated?
                              [color=blue]
                              > Microsoft makes getting the
                              > Onpaint method difficult for a reason,[/color]

                              They don't make the OnPaint method difficult for any reason. The reason why
                              OnPaint isn't called by default is because the UserPaint style is False by
                              default to allow the operating system to draw the textbox. I don't know the
                              exact reason for this, but I assume that it is to let the OS draw special
                              effects (like Windows XP themes etc.) or because it might be slightly faster
                              (correct me if I'm wrong).


                              HTH,

                              Trev.



                              "Brian" <nospam@prairie .lakes.com> wrote in message
                              news:01e001c3c5 0e$cd2b5910$a40 1280a@phx.gbl.. .[color=blue]
                              >
                              > If you do what codemonkey suggests and use owner drawn
                              > style then you'll have to paint the entire text box
                              > yourself. Do you know what that entails? You'll have to
                              > repaint the client area every time you receive a paint
                              > message. You have make sure that you have the correct
                              > lines displayed and if the some of the text is
                              > highlighted or not (there is much more to consider). This
                              > shouldn't be taken lightly. Microsoft makes getting the
                              > Onpaint method difficult for a reason, it's because
                              > everything must fit together in order to make a text box
                              > work properly. If you paint the text in the wrong
                              > position or with the wrong font size then it won't work.
                              >
                              > Lastly, if want a text box that will display only one
                              > line of text then you should create your own. It isn't
                              > that hard and you can use any background you want.
                              >
                              >[color=green]
                              > >-----Original Message-----
                              > >Brian, thanks for your comments. But, I still don't
                              > >understand. I know that hooking the paint event is
                              > >scary, but if it were disallowed altogether I would[/color]
                              > think[color=green]
                              > >that would be true for *any* control, and in that case
                              > >why would there even be an OnPaint accessible at the
                              > >control level?
                              > >[color=darkred]
                              > >>-----Original Message-----
                              > >>Basically, you're not allowed to paint on the textbox.[/color]
                              > >Imagine the chaos if[color=darkred]
                              > >>a user tries to highlight, cut or copy your text.
                              > >>You'll have to write your own textbox to do custom[/color]
                              > >drawing. That's what I[color=darkred]
                              > >>did.
                              > >>
                              > >>"BB" <anonymous@disc ussions.microso ft.com> wrote in[/color]
                              > >message[color=darkred]
                              > >>news:0b3001c3 c4c3$e88fc970$a 501280a@phx.gbl ...
                              > >>> Hello all,
                              > >>>
                              > >>> I am trying to override OnPaint in a custom textbox
                              > >>> control (so I can drawstring a caption, etc.). In the
                              > >>> code below, I get the "painting the form" message as
                              > >>> expected, but not the "painting the control". It also
                              > >>> has no effect to explicitly call txtTest.Invalid ate or
                              > >>> txtTest.Refresh . Am I missing something simple here?
                              > >>> Any push in the right direction is appreciated.
                              > >>>
                              > >>> BB
                              > >>>
                              > >>> Public Class Form1
                              > >>> Inherits System.Windows. Forms.Form
                              > >>>
                              > >>> #Region " Windows Form Designer generated code "
                              > >>> #End Region
                              > >>>
                              > >>> Private Sub Form1_Load(ByVa l sender As[/color]
                              > >System.Objec t,[color=darkred]
                              > >>> ByVal e As System.EventArg s) Handles MyBase.Load
                              > >>> Dim txtTest As New MyTextBox
                              > >>> Controls.Add(tx tTest)
                              > >>> End Sub
                              > >>>
                              > >>> Protected Overrides Sub onpaint(ByVal e As
                              > >>> PaintEventArgs)
                              > >>> MessageBox.Show ("Painting the form...")
                              > >>> End Sub
                              > >>>
                              > >>> End Class
                              > >>>
                              > >>> Public Class MyTextBox
                              > >>> Inherits TextBox
                              > >>>
                              > >>> Protected Overrides Sub onpaint(ByVal e As
                              > >>> PaintEventArgs)
                              > >>> MessageBox.Show ("Painting the textbox...")
                              > >>> End Sub
                              > >>>
                              > >>> End Class
                              > >>>
                              > >>
                              > >>
                              > >>.
                              > >>[/color]
                              > >.
                              > >[/color][/color]


                              Comment

                              Working...