Results 1 to 15 of 15

Thread: [RESOLVED] [2008] Override OnPaint of a Textbox and a Button

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [RESOLVED] [2008] Override OnPaint of a Textbox and a Button

    Hi!!

    In the Attachment I have a project very simple, just a form and a two classes: the TestTB and the TestB. The testTB class Inherits from the TextBox class and the TestB Inherits from the Button class. And the only sub that they have is this one:
    Code:
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            e.Graphics.DrawString("1", Me.Font, New SolidBrush(Me.ForeColor), 3, 1)
            e.Graphics.DrawString("2", Me.Font, New SolidBrush(Me.ForeColor), 13, 1)
            e.Graphics.DrawString("3", Me.Font, New SolidBrush(Me.ForeColor), 23, 1)
            e.Graphics.DrawString("4", Me.Font, New SolidBrush(Me.ForeColor), 3, 13)
            e.Graphics.DrawString("5", Me.Font, New SolidBrush(Me.ForeColor), 13, 13)
            e.Graphics.DrawString("6", Me.Font, New SolidBrush(Me.ForeColor), 23, 13)
            e.Graphics.DrawString("7", Me.Font, New SolidBrush(Me.ForeColor), 3, 25)
            e.Graphics.DrawString("8", Me.Font, New SolidBrush(Me.ForeColor), 13, 25)
            e.Graphics.DrawString("9", Me.Font, New SolidBrush(Me.ForeColor), 23, 25)
        End Sub
    In the form i have a TestTB control and a TestB control. Now my question is why in hell doesn't the TestTB control shows the little numbers like the TestB control??
    Attached Files Attached Files
    Last edited by Lasering; Sep 7th, 2008 at 03:24 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Override OnPaint of a Textbox and a Button

    Because by default the paint event is never fired for a textbox, as the painting is handled by the operating system directly. You will have to set owner drawn, but this will mean that you also have to manually draw the rest of the textbox as well.

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2008] Override OnPaint of a Textbox and a Button

    I cant find either OwnerDraw pr DrawMode in the textbox proprieties...
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Override OnPaint of a Textbox and a Button

    You'll have to do something like this:
    Code:
    Public Class MyTextBox
        Inherits TextBox
        
        Public Sub New()
            SetStyle(ControlStyles.UserPaint, True)
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            e.Graphics.DrawString("1", Me.Font, New SolidBrush(Me.ForeColor), 3, 1)
        End Sub
    End Class
    luckily, it turns out that you don't have to manually draw the rest of the textbox, at least not in vb.

  5. #5

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2008] Override OnPaint of a Textbox and a Button

    Thks a lot man!!!
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  6. #6

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [RESOLVED] [2008] Override OnPaint of a Textbox and a Button

    Just one thing I used ur code, and it works wonderfully, my problem is the following: I changed the size of the font to 20, but its like I havent changed it at all, I mean the fonts still stays with the 8.25 default value, how can I overcome this? Or why this happens? And where is the "real" value of the font he's using?
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  7. #7
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] Override OnPaint of a Textbox and a Button

    JMC posted a nice post about that I think. He talked about how a textbox is text only, whereas a RTF (rich textbox) contains text + metadata about the text (ie, the "HTML" for it).

    Try switching to a rich text control.

  8. #8

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2008] Override OnPaint of a Textbox and a Button

    Didnt work with the richtextbox, anyone got any ideias how to solve this?
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  9. #9
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Override OnPaint of a Textbox and a Button

    What exactly are you trying to accomplish here? Coloured text? If so, the richtextbox is the way to go.

  10. #10
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461

    Re: [2008] Override OnPaint of a Textbox and a Button

    anyone got any ideias how to solve this?
    If also a stupid idea is valid...
    I have not tried this, but I'm considering that a non multiline Textbox height is tied to its font height. I don't know if it could work in a bidirectional way. The first thing I'd try is to use a multiline textbox with a sufficient height to display the desired font. Only to begin to understand if the problem is in that direction. Anyway it should be an absolutely stupid suggestion!
    Live long and prosper (Mr. Spock)

  11. #11

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2008] Override OnPaint of a Textbox and a Button

    Nice idea. The problem is that I have from the start a multiline textbox with a sufficient height to display the desired font. So I cant be cause of that.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  12. #12
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2008] Override OnPaint of a Textbox and a Button

    Given how this thread got started (from another thread and a PM), I would suggest that what you're trying to force a textbox to do could be done with another control, of which you do mention the button control here too.

    What you're trying to do is show small "available" numbers on the Textbox control and also have the TB accept the numeric input, however the TB simply wasn't designed to allow this type of interaction without a huge amount of re-coding of the Textbox. IE you're going to end up with a whole new control in the end and have put in who knows how many overrides to get this result. Now what I would suggest is for you to step back, look at how much work is involved here and take a look at how much work it would be to just use a different control for this, a button is an example.
    I simply used a button for this since, a button has the physical space for custom drawing, it also has all of the click event stuff set up and it's easy to capture key presses and when a valid key is pressed all the "available" numbers don't need to be displayed anymore.

    Just some things to think about.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  13. #13
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: [2008] Override OnPaint of a Textbox and a Button

    you could try looking at this, dont know if its what you need :-/ ?

    a cstm textbox i made, not the final version but it shows how to draw on it
    http://www.vbforums.com/showthread.php?t=537317

  14. #14

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2008] Override OnPaint of a Textbox and a Button

    Quote Originally Posted by JuggaloBrotha
    What you're trying to do is show small "available" numbers on the Textbox control and also have the TB accept the numeric input, however the TB simply wasn't designed to allow this type of interaction without a huge amount of re-coding of the Textbox. IE you're going to end up with a whole new control in the end and have put in who knows how many overrides to get this result.
    Well, I dont give up, so I got what I wanted!! Yes I had to make a new control, but I only had to make 5 overrides, with very few lines of code each.
    I read in the forum here to fix the bug I had to do add this line
    Code:
    Me.Font = Me.Font
    I changed to
    Code:
    Me.Font = New Font("Verdana", 20, FontStyle.Regular, GraphicsUnit.Point)
    and still work so my problem was solved.

    This font bug was probably something microsoft didnt saw, cause if you Inherit a Textbox and Userpaint it then the Font property becomes useless and doesnt matter what value you give it in design mode cause it will keep default value (Microsoft Sans Sherif 8.25)
    Last edited by Lasering; Sep 10th, 2008 at 08:23 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  15. #15
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2008] Override OnPaint of a Textbox and a Button

    Quote Originally Posted by Lasering
    I read in the forum here to fix the bug I had to do add this line
    Code:
    Me.Font = Me.Font
    I changed to
    Code:
    Me.Font = New Font("Verdana", 20, FontStyle.Regular, GraphicsUnit.Point)
    and still work so my problem was solved.

    This font bug was probably something microsoft didnt saw, cause if you Inherit a Textbox and Userpaint it then the Font property becomes useless and doesnt matter what value you give it in design mode cause it will keep default value (Microsoft Sans Sherif 8.25)
    If you're doing the custom drawing in the paint even, you should use the control's Font property anyways, unless you provide an additional font property for them. By doing it this way you give the developer that's using your control the ability to change the font in your control which provides additional flexibility.

    I'm also curious to see this new control of yours, perhaps you could provide it in a new thread in the CodeBank part of the forum.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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