Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Interface and Graphics > Custom Drawn Multiline TextBox


Reply
 
Thread Tools Display Modes
  #1  
Old 02-08-2011, 08:49 AM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 100
Default 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.
Reply With Quote
  #2  
Old 02-09-2011, 09:49 AM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 100
Default

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.
Reply With Quote
  #3  
Old 02-09-2011, 10:03 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

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?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 02-10-2011, 03:42 PM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 100
Default

Quote:
Originally Posted by AtmaWeapon View Post
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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->