Hello,
I've created a custom control inherited from the Windows.Forms.Textbox control. Basically it's a textbox that pulses in color when it has the focus. It uses a bitmap (gradient) to paint the background of the textbox, therefore I have to override the method to paint the text to make it visible.
Everything works how I want it, except for the cursor position in the textbox. When I type something into the textbox (runtime), the blinking cursor doesn't position correctly. This also depends on the font(size) I use. For example: with a larger font the cursor appears 2 or 3 letters in front of where it's supposed to be and with smaller fonts it appears farther to the back than actually should be. This is only a visual problem because backspace, delete, etc. work correctly. Does anyone have a clue of which method/property to override or shadow?
This is the code I use to draw the text into the textbox:
From top to bottom: it first clears the background with the backcolor of the parent control, then draws the background with a tiled picture and last draws the text from the Text property over the background image.
Any help is much appreciated.
Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
I've created a custom control inherited from the Windows.Forms.Textbox control. Basically it's a textbox that pulses in color when it has the focus. It uses a bitmap (gradient) to paint the background of the textbox, therefore I have to override the method to paint the text to make it visible.
Everything works how I want it, except for the cursor position in the textbox. When I type something into the textbox (runtime), the blinking cursor doesn't position correctly. This also depends on the font(size) I use. For example: with a larger font the cursor appears 2 or 3 letters in front of where it's supposed to be and with smaller fonts it appears farther to the back than actually should be. This is only a visual problem because backspace, delete, etc. work correctly. Does anyone have a clue of which method/property to override or shadow?
This is the code I use to draw the text into the textbox:
Code:
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
g.Clear(Parent.BackColor)
Draw(g)
End Sub
Protected Sub Draw(ByVal g As Graphics)
DrawBackground(g)
DrawText(g)
End Sub
Protected Sub DrawBackground(ByVal g As Graphics)
If Focused Then
DrawTextboxState(g, iaDefault)
Else
DrawTextboxState(g, iaNormal)
End If
End Sub
Protected Sub DrawTextboxState(ByVal g As Graphics, ByVal ia As ImageAttributes)
Dim tb As New TextureBrush(imgFill, New Rectangle(0, 0, imgFill.Width, imgFill.Height), ia)
tb.WrapMode = WrapMode.Tile
g.FillRectangle(tb, 0, 0, Me.Width, Me.Height)
tb.Dispose()
End Sub
Protected Sub DrawText(ByVal g As Graphics)
Dim layoutrect As New RectangleF(0, 0, Me.Width, Me.Height)
Dim fmt As New StringFormat
fmt.Alignment = StringAlignment.Near
fmt.LineAlignment = StringAlignment.Near
Dim textBrush As New SolidBrush(ForeColor)
g.DrawString(Text, Font, textBrush, layoutrect, fmt)
textBrush.Dispose()
End Sub
From top to bottom: it first clears the background with the backcolor of the parent control, then draws the background with a tiled picture and last draws the text from the Text property over the background image.
Any help is much appreciated.
Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?