 |
 |

02-08-2011, 08:49 AM
|
|
Centurion
|
|
Join Date: Apr 2006
Posts: 100
|
|
Custom Drawn Multiline TextBox
|
I'm trying to create a textbox that handles multiline text and a custom imported font face. Right now I'm working on the multiline, but I imagine that I'll run into a similar problem when I go to implement the font face.
Two things are happening that I'm not sure how to deal with.
First, I'm making a custom OnPaint event so I can have a gradient background (which works well) and eventually I'll want to draw the font manually too. For now I'm using a system font and I need to know how to programmatically split the string to a new line. I thought of injecting a new line at the position in front of the character that won't fit if the text gets wider than the control, but I'm not sure how to do that. PaintEventArgs.Graphics.MeasureString() can do a string width with a given font and string. When I do that the width of the text gets reported as wider than it is. How does the vanilla textbox do it?
Secondly, while I'm editing text, a box appears over my control (I'm guessing a texteditbox or something) that covers over what my control should be displaying. It also shows text in a system standard font. I don't want this to overlay my control. How do I stop it? What invokes it?
|
Last edited by NFITC1; 02-08-2011 at 09:04 AM.
|

02-09-2011, 09:49 AM
|
|
Centurion
|
|
Join Date: Apr 2006
Posts: 100
|
|
I don't want to double-post, but I don't see an edit or modify button.
I fixed the newline thing. What I WAS doing was:
Code:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim nltext As String = ""
Dim nl As Short = 0
Dim textwidth As SizeF = New Size(0, 0)
e.Graphics.FillRectangle(pgb, Me.DisplayRectangle)
For Each Text As Char In Me.Text
nltext += Text
textwidth += e.Graphics.MeasureString(nltext, Me.Font)
If textwidth.Width > Me.Width Then
Debug.Print(textwidth.Width & " : " & Me.Width)
e.Graphics.DrawString(nltext, Me.Font, textBrush, 0, Me.Font.Size * nl)
nltext = ""
nl += 1
textwidth = New Size(0, 0)
End If
Next
End Sub
but what I changed was
Code:
textwidth += e.Graphics.MeasureString(nltext, Me.Font)
to
Code:
textwidth = e.Graphics.MeasureString(nltext, Me.Font)
and it worked perfectly.
This doesn't do line breaks per word, but I'll add that functionality later.
The second question still stands though.
|
|

02-09-2011, 10:03 AM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
|
|
|
I think the answer to the second question is that the TextBox control was not designed for you to provide custom painting. I feel like I tried this one time and gave up because of that weird edit box behavior you're describing. If you want to make a custom text control in WinForms, you have to start from scratch.
In WPF it's not so bad; all controls support retemplating, which lets you completely change their visual appearance.
I don't really follow why you feel you have to manually draw the font; if you've got a Font instance why not just set the text box's font to that font?
|
|

02-10-2011, 03:42 PM
|
|
Centurion
|
|
Join Date: Apr 2006
Posts: 100
|
|
Quote:
Originally Posted by AtmaWeapon
I don't really follow why you feel you have to manually draw the font; if you've got a Font instance why not just set the text box's font to that font?
|
Because in this particular project, the user will be allowed to import a graphic of their own font face. This is because of what they are editing, space might be important so they might want to double-up on some letter combos like st, ng, th, etc. So I want to be able to allow a user to define a character in that way. This is also with the intention of allowing other types of foreign characters like Korean or kanji.
I noticed that you suggested WPF in another topic a while back. I didn't look into that because I thought WinForms were customizable enough. It appears that I was wrong in assuming that WinForms had enough control so I'll have to look into WPF after I play with the other Key_ events trying to prevent that stupid edit box.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|