-
Dec 17th, 2014, 05:14 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Justifying text
Hi,
Over many days I've trolled through as many pages of FAQ as I thought relevant but haven't found a definitive answer as yet.
It's the question of text justification within a Label. We currently have the 'Text Alignment' options of: -
Left: Top, Middle or bottom. Likewise for Right and Centre. What we don't seem to have anywhere is an option for 'Justified' Text.
The argument in most of the answers to similar questions is that it's not possible because it's controlled by the font. Like many others I'm using Microsoft's Visual Studio, in my case, the Express 2012 version. Microsoft also market the very successful 'Word' word processor, this product can select exactly the same set of widely available fonts, yet it can also use justified text with those same fonts, seemingly with ease, certainly with a single button click, and has been able to do so since the very first version.
This being so, and I don't believe there'll be many who disagree with the paragraph above, I would've thought that an option would be possible in VB.NET to select justified text, or if not to select it, then at least produce it by an inbuilt function. Sadly I'm still looking without success.
At one time, while I was still using version 2005, I started to build a subroutine to justify text, I've mapped out a 'look-up-list' for the widths of each keyboard character, and measured the widths of the several 'spacer' characters but this was very time consuming and I only did it for one specific font. By the time I'd done that at the time, the task of working out a algorithm to pack-out the individual spaces between words became too daunting, and, in view of my opinion stated above, it was shelved for a while in the expectation that such a function would become available soon and here I am several years further on asking the question. "How do we get justified text in a Label on a form" ?
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 17th, 2014, 06:20 AM
#2
Re: Justifying text
I'd say that a Label isn't designed for the sort of text that you'd justify, but rather short descriptions to place next to input controls, and so on. If you have a large block of text to render then you should probably be using a different sort of control. Does a TextBox have a justify option? Placing one of those in a read-only mode is my usual method to display large blocks of text, on the few occasions I've needed to do so in WinForms.
-
Dec 17th, 2014, 07:12 AM
#3
Re: Justifying text
I'd say that a Label isn't designed for the sort of text that you'd justify
Yep your problem is the control your trying to use;
Use a Textbox, set it to flat, set the back colour to the same as your form, set the border to nothing, and set enabled = false.
Then it wont look like you using a Textbox BUT you will be able to left / right or centre justify the text.
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 17th, 2014, 07:31 AM
#4
Re: Justifying text
as already noted, the label is the wrong type of control: there is no need for justified text in a label.
Justified text is used for readability of large quantities of text (as well as the serifs), and requires correct text block widths to work correctly. As such, most controls are not used for this type of application.
I would think the RichTextBox would be the control to use if you need justified text (I'm not sure a TextBox has justified text as an option, as it's an input control, primarily).
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Dec 17th, 2014, 08:12 AM
#5
Re: Justifying text
I'm not sure a TextBox can justify text either, but an RTB should be able to. Make it flat, read-only and it should look just like a label, and you'll even get the added benefit of scrollbars if you need them.
-tg
-
Dec 17th, 2014, 08:18 AM
#6
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by Evil_Giraffe
I'd say that a Label isn't designed for the sort of text that you'd justify, but rather short descriptions to place next to input controls, and so on. If you have a large block of text to render then you should probably be using a different sort of control. Does a TextBox have a justify option? Placing one of those in a read-only mode is my usual method to display large blocks of text, on the few occasions I've needed to do so in WinForms.
Originally Posted by NeedSomeAnswers
Yep your problem is the control your trying to use;
Use a Textbox, set it to flat, set the back colour to the same as your form, set the border to nothing, and set enabled = false.
Then it wont look like you using a Textbox BUT you will be able to left / right or centre justify the text.
Thanks guys, I take the point about using a TextBox, but the problem still remains, in fact with a TextBox we get even less choice of Text Alignment, just Left, Right and Centre as NeedSomeAnswers says.
That still doesn't answer the question though, just changes it from a label to a textbox. I know we can select from these three options, but I would like to be able to use 'Justify'. i.e. Have Both edges aligned.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 17th, 2014, 08:24 AM
#7
Re: Justifying text
Have you checked the RTB's options as tg suggested?
-
Dec 17th, 2014, 09:24 AM
#8
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by Evil_Giraffe
Have you checked the RTB's options as tg suggested?
Yes, I've been trying RTB I've taken longer than you might expect because I get bogged down with trivial details... As in this case with the RTB, neither the Forecolor nor BackColor appear as selected. I've compromised on that, but as I say it wasted time.
I think I've carefully looked at all the RTB properties as listed, and they seems to have no 'Text Alignment' options at all !!
Neither do they allow me to use Pixel as a Font Unit, it seems to insist on Point which is a bit of a nuisance because I wish to be able to resize the form and the font size by the same factor, and as far as I can tell 'Point' isn't linear.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 17th, 2014, 09:32 AM
#9
Re: Justifying text
take a look at this Extending RichTextbox
In the article the guys specifically extends it to allow Justification.
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 17th, 2014, 09:38 AM
#10
Re: Justifying text
Huh... I coulda sworn it did... a search showed that wasn't the case... BUT... I did find this in our codebank - http://www.vbforums.com/showthread.p...in-RichTextBox may it be of some use.
-tg
-
Dec 17th, 2014, 09:41 AM
#11
Re: Justifying text
Ungh... I came across that one too, but for some reason I thought we were looking for a VB6 solution here, so I skipped over it. I haven't had any coffee yet, can I blame that? Ignore my previous link too then, as it's a VB6 solution.
what was I thinking?
bleh.
-tg
-
Dec 17th, 2014, 11:01 AM
#12
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by techgnome
Ungh... I came across that one too, but for some reason I thought we were looking for a VB6 solution here, so I skipped over it. I haven't had any coffee yet, can I blame that? Ignore my previous link too then, as it's a VB6 solution.
what was I thinking?
bleh.
-tg
Now he tells me !!!
Actually, I'd seen it was for VB6 and was about to ask...
Can it not be reworked for VB.NET ?
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 17th, 2014, 11:27 AM
#13
Re: Justifying text
WEll ,the one NSA dug up (which I meant to quote) is for .NET... so his is likely closer to what you want than what I posted.
-tg
-
Dec 17th, 2014, 11:48 AM
#14
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by Poppa Mintin
I think I've carefully looked at all the RTB properties as listed, and they seems to have no 'Text Alignment' options at all !!
Neither do they allow me to use Pixel as a Font Unit, it seems to insist on Point which is a bit of a nuisance because I wish to be able to resize the form and the font size by the same factor, and as far as I can tell 'Point' isn't linear.
Poppa.
I've done a little work on this and for anyone that may be interested: -
I'm trying to code up a project which I thought I'd completed some time ago, with the idea of making the forms, which have to be printed out, adjustable in size so that they will display fully on a machine with a smaller screen.
So, my basic premise is to read-in the height of the screen, divide it by the height of the original form height (subtract 10%) and use the result as a factor for adjusting the form size.
When it comes to resizing text on resizeable forms (previously to enlarge them) I've always used 'Pixel' for the Font Size Unit, in the belief that a size of (say) 15 pixels would be 15 pixels high from the top of (Say) an upper case T, to the bottom of characters with descenders, (e.g. j or g). So I've figured that a factor of (Say) 0.75 would resize a character of 20 pixels high to one of 15 pixels high.
This doesn't work when the font's Unit parameter is 'Read only' and set to 'Point'. But I've found that multiplying by the Factor Squared seems to give about the right result, with Arial Narrow anyway.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 17th, 2014, 11:51 AM
#15
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by techgnome
WEll ,the one NSA dug up (which I meant to quote) is for .NET... so his is likely closer to what you want than what I posted.
-tg
That's true, but I know virtually nothing about C#.NET
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 17th, 2014, 12:04 PM
#16
Re: Justifying text
Originally Posted by Poppa Mintin
That's true, but I know virtually nothing about C#.NET
Poppa.
You should really know about code converters.
Here is an online one: http://converter.telerik.com/
-
Dec 17th, 2014, 06:20 PM
#17
Thread Starter
PowerPoster
Re: Justifying text
Thanks TnTinMN, I tried your suggestion... and I now have the VB version of that C code... discovering how to use it is my next task, but after spending the evening in the bar, I think it would be wisest to wait until the morning... Thanks for your help, I'm going to put that URL on my Bookmarks Bar. I thought I'd tried that before but since it's an on-line converter I don't believe I could've.
Thanks again.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 17th, 2014, 07:15 PM
#18
Thread Starter
PowerPoster
Re: Justifying text
I just took a quick look at adding the first of those two 'Converted' codes into my project and it's absolutely full of error messages... In several cases the automatic correction facility suggested 'Change this into that' and I followed it's advice...
That still left a good few errors in the list such as these: -
Type 'System.Windows.Forms.DataFormats' has no constructors.
There are several instancies of the option of: -
[Generate Field stub] for : -'EM_GETPARAFORMAT', 'Collection', 'StringAlignment' in...
or: -
[Generate Method stub] for : -'EM_GETPARAFORMAT', 'Collection', 'StringAlignment' in...
or: -
[Generate Property stub] for : -'EM_GETPARAFORMAT', 'Collection', 'StringAlignment' in...
I also get: -
'cbSize' Is not a member of 'System.Windows.Forms.DataFormats'. (Despite this having no constructors)
'dwMask' Is not a member of 'System.Windows.Forms.DataFormats'. (Ditto)
'wAlignment' Is not a member of 'System.Windows.Forms.DataFormats'. (Ditto)
'Left' Is not a member of 'System.Windows.Forms.ToolBarTextAlignment'. (Ditto)
'Message' Is a type and cannot be used as an expression.
Some of these occur more than once and I have no idea what choices to make or what to do about them.
Maybe I should've left it till morning
Although I doubt I'll understand it any better then.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 17th, 2014, 08:06 PM
#19
Re: Justifying text
I don't know what you did, but try again. I copied the code from NSA's link and made sure that it converted properly before I posted a link to that converter. The converted code works as advertised.
-
Dec 18th, 2014, 04:06 AM
#20
Re: Justifying text
try this -
Code:
Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
<Serializable()> _
Public Class AdvRichTextBox
Inherits RichTextBox
Enum TextAlign
Left = 1
Right = 2
Center = 3
Justify = 4
End Enum
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As HandleRef, _
ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As HandleRef, _
ByVal msg As Integer, ByVal wParam As Integer, ByVal lp As PARAFORMAT) As Integer
End Function
<StructLayout(LayoutKind.Sequential)> _
Private Structure PARAFORMAT
Public cbSize As Integer
Public dwMask As UInteger
Public wNumbering As Short
Public wReserved As Short
Public dxStartIndent As Integer
Public dxRightIndent As Integer
Public dxOffset As Integer
Public wAlignment As Short
Public cTabCount As Short
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
Public rgxTabs() As Integer
' PARAFORMAT2 from here onwards.
Public dySpaceBefore As Int32
Public dySpaceAfter As Int32
Public dyLineSpacing As Int32
Public sStyle As Short
Public bLineSpacingRule As Byte
Public bOutlineLevel As Byte
Public wShadingWeight As Short
Public wShadingStyle As Short
Public wNumberingStart As Short
Public wNumberingStyle As Short
Public wNumberingTab As Short
Public wBorderSpace As Short
Public wBorderWidth As Short
Public wBorders As Short
End Structure
Private updating As Int32 = 0
Private oldEventMask As Int32 = 0
'Constants from the Platform SDK.
Private Const EM_SETEVENTMASK As Integer = 1073
Private Const EM_GETPARAFORMAT As Integer = 1085
Private Const EM_SETPARAFORMAT As Integer = 1095
Private Const EM_SETTYPOGRAPHYOPTIONS As Integer = 1226
Private Const WM_SETREDRAW As Integer = 11
Private Const TO_ADVANCEDTYPOGRAPHY As Integer = 1
Private Const PFM_ALIGNMENT As Integer = 8
Private Const SCF_SELECTION As Integer = 1
Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
MyBase.OnHandleCreated(e)
'Enable support for justification.
SendMessage(New HandleRef(Me, Handle), _
EM_SETTYPOGRAPHYOPTIONS, _
TO_ADVANCEDTYPOGRAPHY, _
TO_ADVANCEDTYPOGRAPHY)
End Sub
Public Overloads Property SelectionAlignment() As TextAlign
Get
Dim fmt As PARAFORMAT = New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
' Get the alignment.
SendMessage(New HandleRef(Me, Handle), _
EM_GETPARAFORMAT, _
SCF_SELECTION, fmt)
' Default to Left align.
If ((fmt.dwMask & PFM_ALIGNMENT) = 0) Then
Return TextAlign.Left
End If
If fmt.wAlignment = 1 Then
Return TextAlign.Left
ElseIf fmt.wAlignment = 2 Then
Return TextAlign.Right
ElseIf fmt.wAlignment = 3 Then
Return TextAlign.Center
ElseIf fmt.wAlignment Then
Return TextAlign.Justify
End If
End Get
Set(ByVal value As TextAlign)
Dim fmt As PARAFORMAT = New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
fmt.dwMask = PFM_ALIGNMENT
fmt.wAlignment = CShort(value)
' Set the alignment.
SendMessage(New HandleRef(Me, Handle), _
EM_SETPARAFORMAT, _
SCF_SELECTION, fmt)
End Set
End Property
End Class
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 18th, 2014, 05:53 AM
#21
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by TnTinMN
I don't know what you did, but try again. I copied the code from NSA's link and made sure that it converted properly before I posted a link to that converter. The converted code works as advertised.
Thanks TnTinMN, I think when I tried it last night I placed the whole thing right at the end of my code with the inevitable result.
I'll try again later.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 18th, 2014, 06:12 AM
#22
Re: Justifying text
Here is the fully converted code -
Code:
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
''' <summary>
''' Represents a standard <see cref="RichTextBox"/> with some
''' minor added functionality.
''' </summary>
''' <remarks>
''' AdvRichTextBox provides methods to maintain performance
''' while it is being updated. Additional formatting features
''' have also been added.
''' </remarks>
Public Class AdvRichTextBox
Inherits RichTextBox
''' <summary>
''' Maintains performance while updating.
''' </summary>
''' <remarks>
''' <para>
''' It is recommended to call this method before doing
''' any major updates that you do not wish the user to
''' see. Remember to call EndUpdate when you are finished
''' with the update. Nested calls are supported.
''' </para>
''' <para>
''' Calling this method will prevent redrawing. It will
''' also setup the event mask of the underlying richedit
''' control so that no events are sent.
''' </para>
''' </remarks>
Public Sub BeginUpdate()
' Deal with nested calls.
updating += 1
If updating > 1 Then
Return
End If
' Prevent the control from raising any events.
oldEventMask = SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, 0)
' Prevent the control from redrawing itself.
SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 0, 0)
End Sub
''' <summary>
''' Resumes drawing and event handling.
''' </summary>
''' <remarks>
''' This method should be called every time a call is made
''' made to BeginUpdate. It resets the event mask to it's
''' original value and enables redrawing of the control.
''' </remarks>
Public Sub EndUpdate()
' Deal with nested calls.
updating -= 1
If updating > 0 Then
Return
End If
' Allow the control to redraw itself.
SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 1, 0)
' Allow the control to raise event messages.
SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, oldEventMask)
End Sub
''' <summary>
''' Gets or sets the alignment to apply to the current
''' selection or insertion point.
''' </summary>
''' <remarks>
''' Replaces the SelectionAlignment from
''' <see cref="RichTextBox"/>.
''' </remarks>
Public Shadows Property SelectionAlignment() As TextAlign
Get
Dim fmt As New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
' Get the alignment.
SendMessage(New HandleRef(Me, Handle), EM_GETPARAFORMAT, SCF_SELECTION, fmt)
' Default to Left align.
If (fmt.dwMask And PFM_ALIGNMENT) = 0 Then
Return TextAlign.Left
End If
Return CType(fmt.wAlignment, TextAlign)
End Get
Set(value As TextAlign)
Dim fmt As New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
fmt.dwMask = PFM_ALIGNMENT
fmt.wAlignment = CShort(value)
' Set the alignment.
SendMessage(New HandleRef(Me, Handle), EM_SETPARAFORMAT, SCF_SELECTION, fmt)
End Set
End Property
''' <summary>
''' This member overrides
''' <see cref="Control"/>.OnHandleCreated.
''' </summary>
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
' Enable support for justification.
SendMessage(New HandleRef(Me, Handle), EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)
End Sub
Private updating As Integer = 0
Private oldEventMask As Integer = 0
' Constants from the Platform SDK.
Private Const EM_SETEVENTMASK As Integer = 1073
Private Const EM_GETPARAFORMAT As Integer = 1085
Private Const EM_SETPARAFORMAT As Integer = 1095
Private Const EM_SETTYPOGRAPHYOPTIONS As Integer = 1226
Private Const WM_SETREDRAW As Integer = 11
Private Const TO_ADVANCEDTYPOGRAPHY As Integer = 1
Private Const PFM_ALIGNMENT As Integer = 8
Private Const SCF_SELECTION As Integer = 1
' It makes no difference if we use PARAFORMAT or
' PARAFORMAT2 here, so I have opted for PARAFORMAT2.
<StructLayout(LayoutKind.Sequential)> _
Private Structure PARAFORMAT
Public cbSize As Integer
Public dwMask As UInteger
Public wNumbering As Short
Public wReserved As Short
Public dxStartIndent As Integer
Public dxRightIndent As Integer
Public dxOffset As Integer
Public wAlignment As Short
Public cTabCount As Short
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
Public rgxTabs As Integer()
' PARAFORMAT2 from here onwards.
Public dySpaceBefore As Integer
Public dySpaceAfter As Integer
Public dyLineSpacing As Integer
Public sStyle As Short
Public bLineSpacingRule As Byte
Public bOutlineLevel As Byte
Public wShadingWeight As Short
Public wShadingStyle As Short
Public wNumberingStart As Short
Public wNumberingStyle As Short
Public wNumberingTab As Short
Public wBorderSpace As Short
Public wBorderWidth As Short
Public wBorders As Short
End Structure
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wParam As Integer, lParam As Integer) As Integer
End Function
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wParam As Integer, ByRef lp As PARAFORMAT) As Integer
End Function
End Class
''' <summary>
''' Specifies how text in a <see cref="AdvRichTextBox"/> is
''' horizontally aligned.
''' </summary>
Public Enum TextAlign
''' <summary>
''' The text is aligned to the left.
''' </summary>
Left = 1
''' <summary>
''' The text is aligned to the right.
''' </summary>
Right = 2
''' <summary>
''' The text is aligned in the center.
''' </summary>
Center = 3
''' <summary>
''' The text is justified.
''' </summary>
Justify = 4
End Enum
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 18th, 2014, 06:17 AM
#23
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by NeedSomeAnswers
try this -
Public Overloads Property SelectionAlignment() As TextAlign
Get...
End Get
Thanks NeedSomeAnswers, I'm trying your code, and will shortly try to work out how to use it...
I've placed the whole thing 'as is' before the first line of my code (Public Class Form1), in a test piece I've set up for the purpose. I hope that's right, at least that hasn't thrown up any errors.
Now... Everything looks ok except, under the 'End Get' in the above section of code there's a green (I think the technical name for it is) Wriggly Line.
It gives me the warning, rather than an error,
"Property 'Selection Alignment' doesn't return a value on all code paths.
Are you missing a 'Return' statement ?"
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 18th, 2014, 06:32 AM
#24
Re: Justifying text
hmm that is strange as it clearly does have a return value -
Code:
Return CType(fmt.wAlignment, TextAlign)
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 18th, 2014, 09:20 AM
#25
Re: Justifying text
Seems like he might still be using the code from post 20, instead of 22. Post 20 didn't have a case for if none of the If ElseIf cases were met.
Last edited by passel; Dec 18th, 2014 at 11:26 AM.
-
Dec 18th, 2014, 09:24 AM
#26
Re: Justifying text
Aha yes that makes sense, the code in post 20 is incomplete, use the Post 22 code, thanks Passel !
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 18th, 2014, 11:43 AM
#27
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by passel
Seems like he might still be using the code from post 20, instead of 22. Post 20 didn't have a case for if none of the If ElseIf cases were met.
Hi, Yes you're right Passel, I believe post 22 arrived whilst I was typing up my post 23, there's only 5 minutes between 'em. I just tried the new version and I have no problems... I still have to work out how to use it though but that's half the fun of coding.
Thanks guys.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 18th, 2014, 05:02 PM
#28
Thread Starter
PowerPoster
Re: Justifying text
Some questions for NeedSomeAnswers.
I have your code in my Form1.vb. All of it is above Public Class Form1. I trust that's correct ?
Now I have to create an instance of the class, but where do I do that ? In fact, How do I do that ? I've been trying to work out what part of that code I have to use and indeed how to use it.
Once I have that sorted out, I hope to use it in my project, but only in Form2.
So, do I have to place it all Form2 ?
And as a general question in regard to that, if I wanted to use it on several Forms in a project is it all accessible from Form1 ?
Poppa.
Oh, yes one more question... My RTB is inside a TLP is that going to cause me a problem ?
Pop.
Last edited by Poppa Mintin; Dec 18th, 2014 at 05:07 PM.
Reason: Extra question...
Along with the sunshine there has to be a little rain sometime.
-
Dec 18th, 2014, 06:46 PM
#29
Re: Justifying text
I think I would just select under the Project menu in the IDE "Add Class...", and name the file "AdvRichTextBox.vb", and paste the code in there.
Then, in Form2, where you need it, in the declarations area, do something like
Private JustifiableTextBox as New AdvRichTextBox
But then again, perhaps NSA means it to be build as a new control, so you can get it from the toolbox in the IDE and place it where you need it.
I tried it out the first way, creating a class module.
Then in my form1, I declared an instance, and added it to a flow panel that was already on that form. Entered some text, then aligned it.
It looks like you have to select the text to align, but I didn't look at it in detail. When I selected all, and set the SelectionAlignment, it did justify the text.
Code:
Private justifiableTextBox As AdvRichTextBox = New AdvRichTextBox
'in the form_load event
flpChars.Controls.Add(justifiableTextBox)
justifiableTextBox.Text = "This is a test to see if the text that is selected will be justified if we choose to."
justifiableTextBox.SelectAll()
justifiableTextBox.SelectionAlignment = TextAlign.Justify
justifiableTextBox.SelectionLength = 0
Last edited by passel; Dec 18th, 2014 at 07:08 PM.
-
Dec 19th, 2014, 03:52 AM
#30
Re: Justifying text
I think I would just select under the Project menu in the IDE "Add Class...", and name the file "AdvRichTextBox.vb", and paste the code in there.
Then, in Form2, where you need it, in the declarations area, do something like
Private JustifiableTextBox as New AdvRichTextBox
But then again, perhaps NSA means it to be build as a new control, so you can get it from the toolbox in the IDE and place it where you need it.
Either way works, but i was thinking of your first method.
Poppa - Yes dont add it to your existing Form code, you need a new class (right click on your project file in the solution explorer, add - Class - Then rename it AdvRichTextBox.vb - and copy over any existing code with the code posted in #22.
Then Follow Passels code in #29 in the first instance.
You could create as a Control so you could drop it on a form as design time, but there is no real need.
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 19th, 2014, 05:15 AM
#31
Thread Starter
PowerPoster
Re: Justifying text
Thanks guys, I'm getting there...
Ok I still have problems, I tried to add the code as a new class, but it wouldn't accept all of it as it stands. All the code between 'Public Class AdvRichTextBox' and 'End Class' is ok, (yeah, I know, pretty obvious) but that leaves me wondering what to do with these three parts...
vb.net Code:
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
''' <summary>
''' Represents a standard <see cref="RichTextBox"/> with some
''' minor added functionality.
''' </summary>
''' <remarks>
''' AdvRichTextBox provides methods to maintain performance
''' while it is being updated. Additional formatting features
''' have also been added.
''' </remarks>
'''
and
vb.net Code:
''' <summary>
''' Specifies how text in a <see cref="AdvRichTextBox"/> is
''' horizontally aligned.
''' </summary>
'''
They look more like C# to me anyway, and to me they look like comments rather than functional code. But I'm sure
vb.net Code:
Public Enum TextAlign
''' <summary>
''' The text is aligned to the left.
''' </summary>
Left = 1
''' <summary>
''' The text is aligned to the right.
''' </summary>
Right = 2
''' <summary>
''' The text is aligned in the center.
''' </summary>
Center = 3
''' <summary>
''' The text is justified.
''' </summary>
Justify = 4
End Enum
is required.
Passel, I can't figure out what ' flpChars.Controls.Add(justifiableTextBox) ' is ? The four line after that are ok, and yes they're in... Oh! you say 'Form Load' Event... that's not the Form Load Method ! I'll look at that...
(After breakfast)
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 19th, 2014, 05:32 AM
#32
Re: Justifying text
The Stuff in Green is just comments, and will not cause you any errors. You can get rid if you want or not its up to you!
What errors do you get still with your new class? and can you post the whole class as you have it now?
Passel, I can't figure out what ' flpChars.Controls.Add(justifiableTextBox)
flpChars is a flow panel, Passel did say in his earlier post, add a Flow panel to your form and rename it 'flpChars', then Passels code will add the extended Richtextbox to the Flow Panel.
h! you say 'Form Load' Event... that's not the Form Load Method !
Oh i would say what you think of as the Form Load method IS the Form Load Event!
Events run after an Action, so something happens like a user clicks on a button, and the click event runs. When the Form Loads the Form Load Event runs.
You can tell an Event in VB.Net as they have the Handles key word at then end - e.g. -
Code:
Private Sub SearchDialog_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 19th, 2014, 06:04 AM
#33
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by NeedSomeAnswers
flpChars is a flow panel, Passel did say in his earlier post, add a Flow panel to your form and rename it 'flpChars', then Passels code will add the extended Richtextbox to the Flow Panel.
Oh I would say what you think of as the Form Load method IS the Form Load Event!
Ah... I don't know how I missed that, thanks.
And yes, I took that the be what he meant, but only noticed the word 'Event' as I was writing.
I've had a problem with the Form1.vb[design] in my trial project, but I've started a replacement test so now I can get back to trying it all out again.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 19th, 2014, 06:46 AM
#34
Thread Starter
PowerPoster
Re: Justifying text
Originally Posted by NeedSomeAnswers
What errors do you get still with your new class? and can you post the whole class as you have it now?
Here is the code I have for ' AdvRichTextBox ' directly from the tab ' AdvRichTextBox.vb* ': -
vb.net Code:
Public Class AdvRichTextBox
Inherits RichTextBox
Public Sub BeginUpdate()
' Deal with nested calls.
updating += 1
If updating > 1 Then
Return
End If
' Prevent the control from raising any events.
oldEventMask = SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, 0)
' Prevent the control from redrawing itself.
SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 0, 0)
End Sub
Public Sub EndUpdate()
' Deal with nested calls.
updating -= 1
If updating > 0 Then
Return
End If
' Allow the control to redraw itself.
SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 1, 0)
' Allow the control to raise event messages.
SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, oldEventMask)
End Sub
Public Shadows Property SelectionAlignment() As TextAlign
Get
Dim fmt As New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
' Get the alignment.
SendMessage(New HandleRef(Me, Handle), EM_GETPARAFORMAT, SCF_SELECTION, fmt)
' Default to Left align.
If (fmt.dwMask And PFM_ALIGNMENT) = 0 Then
Return TextAlign.Left
End If
Return CType(fmt.wAlignment, TextAlign)
End Get
Set(value As TextAlign)
Dim fmt As New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
fmt.dwMask = PFM_ALIGNMENT
fmt.wAlignment = CShort(value)
' Set the alignment.
SendMessage(New HandleRef(Me, Handle), EM_SETPARAFORMAT, SCF_SELECTION, fmt)
End Set
End Property
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
' Enable support for justification.
SendMessage(New HandleRef(Me, Handle), EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)
End Sub
Private updating As Integer = 0
Private oldEventMask As Integer = 0
' Constants from the Platform SDK.
Private Const EM_SETEVENTMASK As Integer = 1073
Private Const EM_GETPARAFORMAT As Integer = 1085
Private Const EM_SETPARAFORMAT As Integer = 1095
Private Const EM_SETTYPOGRAPHYOPTIONS As Integer = 1226
Private Const WM_SETREDRAW As Integer = 11
Private Const TO_ADVANCEDTYPOGRAPHY As Integer = 1
Private Const PFM_ALIGNMENT As Integer = 8
Private Const SCF_SELECTION As Integer = 1
' It makes no difference if we use PARAFORMAT or
' PARAFORMAT2 here, so I have opted for PARAFORMAT2.
<StructLayout(LayoutKind.Sequential)> _
Private Structure PARAFORMAT
Public cbSize As Integer
Public dwMask As UInteger
Public wNumbering As Short
Public wReserved As Short
Public dxStartIndent As Integer
Public dxRightIndent As Integer
Public dxOffset As Integer
Public wAlignment As Short
Public cTabCount As Short
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
Public rgxTabs As Integer()
' PARAFORMAT2 from here onwards.
Public dySpaceBefore As Integer
Public dySpaceAfter As Integer
Public dyLineSpacing As Integer
Public sStyle As Short
Public bLineSpacingRule As Byte
Public bOutlineLevel As Byte
Public wShadingWeight As Short
Public wShadingStyle As Short
Public wNumberingStart As Short
Public wNumberingStyle As Short
Public wNumberingTab As Short
Public wBorderSpace As Short
Public wBorderWidth As Short
Public wBorders As Short
End Structure
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wParam As Integer, lParam As Integer) As Integer
End Function
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wParam As Integer, ByRef lp As PARAFORMAT) As Integer
End Function
End Class
There are far more errors listed this time... 19 Errors it says, and 2 warnings ! In my previous project there was only the one, it didn't like the final 'End Function'. This time I removed 'all the green stuff' including the 'stuff' between < > even though it wasn't actually green, maybe that's the problem ?
I still don't know where to put the ' Public Enum TextAlign ' I'm sure that's part of what's supposed to be there.
Poppa.
I'm in a hotel in Portugal, and my purchased internet connection expired just as I clicked to 'Preview' this reply, so I've had to re-do the lot ! Typical !
Pop
Along with the sunshine there has to be a little rain sometime.
-
Dec 19th, 2014, 07:00 AM
#35
Re: Justifying text
I'm in a hotel in Portugal
Its alright for some
Here is the code I have for ' AdvRichTextBox ' directly from the tab ' AdvRichTextBox.vb* ': -
have you got -
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
at the very top of your form ?
To be honest i am surprised, i can load the code form post #22 into a new class and it works fine for me!
I have stripped all the comments out, so try this code - Delete everything you currently have in your class and paste this in AS IS instead and let me know how that goes !
If u still get errors, can you post a couple of them so we can see what they are?
Code:
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class AdvRichTextBox
Inherits RichTextBox
Public Sub BeginUpdate()
' Deal with nested calls.
updating += 1
If updating > 1 Then
Return
End If
' Prevent the control from raising any events.
oldEventMask = SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, 0)
' Prevent the control from redrawing itself.
SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 0, 0)
End Sub
Public Sub EndUpdate()
' Deal with nested calls.
updating -= 1
If updating > 0 Then
Return
End If
' Allow the control to redraw itself.
SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 1, 0)
' Allow the control to raise event messages.
SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, oldEventMask)
End Sub
Public Shadows Property SelectionAlignment() As TextAlign
Get
Dim fmt As New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
' Get the alignment.
SendMessage(New HandleRef(Me, Handle), EM_GETPARAFORMAT, SCF_SELECTION, fmt)
' Default to Left align.
If (fmt.dwMask And PFM_ALIGNMENT) = 0 Then
Return TextAlign.Left
End If
Return CType(fmt.wAlignment, TextAlign)
End Get
Set(value As TextAlign)
Dim fmt As New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
fmt.dwMask = PFM_ALIGNMENT
fmt.wAlignment = CShort(value)
' Set the alignment.
SendMessage(New HandleRef(Me, Handle), EM_SETPARAFORMAT, SCF_SELECTION, fmt)
End Set
End Property
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
' Enable support for justification.
SendMessage(New HandleRef(Me, Handle), EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)
End Sub
Private updating As Integer = 0
Private oldEventMask As Integer = 0
' Constants from the Platform SDK.
Private Const EM_SETEVENTMASK As Integer = 1073
Private Const EM_GETPARAFORMAT As Integer = 1085
Private Const EM_SETPARAFORMAT As Integer = 1095
Private Const EM_SETTYPOGRAPHYOPTIONS As Integer = 1226
Private Const WM_SETREDRAW As Integer = 11
Private Const TO_ADVANCEDTYPOGRAPHY As Integer = 1
Private Const PFM_ALIGNMENT As Integer = 8
Private Const SCF_SELECTION As Integer = 1
' It makes no difference if we use PARAFORMAT or
' PARAFORMAT2 here, so I have opted for PARAFORMAT2.
<StructLayout(LayoutKind.Sequential)> _
Private Structure PARAFORMAT
Public cbSize As Integer
Public dwMask As UInteger
Public wNumbering As Short
Public wReserved As Short
Public dxStartIndent As Integer
Public dxRightIndent As Integer
Public dxOffset As Integer
Public wAlignment As Short
Public cTabCount As Short
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
Public rgxTabs As Integer()
' PARAFORMAT2 from here onwards.
Public dySpaceBefore As Integer
Public dySpaceAfter As Integer
Public dyLineSpacing As Integer
Public sStyle As Short
Public bLineSpacingRule As Byte
Public bOutlineLevel As Byte
Public wShadingWeight As Short
Public wShadingStyle As Short
Public wNumberingStart As Short
Public wNumberingStyle As Short
Public wNumberingTab As Short
Public wBorderSpace As Short
Public wBorderWidth As Short
Public wBorders As Short
End Structure
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wParam As Integer, lParam As Integer) As Integer
End Function
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wParam As Integer, ByRef lp As PARAFORMAT) As Integer
End Function
End Class
Public Enum TextAlign
Left = 1
Right = 2
Center = 3
Justify = 4
End Enum
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 19th, 2014, 07:24 AM
#36
Thread Starter
PowerPoster
Re: Justifying text
ok... now we're cooking. First I added the...
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
at the top of the form, then I added the 'Enum' at the end and looked at the errors list again, this time there were only 4 occurrences of the same error, it didn't like ' TextAlign ' after a good long search I couldn't find where it should be declared, I thought it might be the 'Enum' coding... So, I deleted the whole of my code and replaced it with the code from your latest post, checked the errors list again and all's well.
So, now I might stand a chance when I get back to the rest of the exercise...
Thanks yet again...
I'll keep you posted.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 19th, 2014, 08:51 AM
#37
Re: Justifying text
Sorry for all the issues.
Perhaps NSA and I were not explaining everything in great enough detail, not understanding how new this must be to you. After doing this a while you can forget explaining things to the nth degree for tasks that have become something you do without having to think about it.
Some of the issues make me wonder just what version of .Net you're using, I typically use VS 2010 Pro.
And, secondly, I wonder if you put the class code in the main form originally, and now deleted the code, but are trying to put the class in a class module, that in some of the VB auto-generated code files some remnant of info about the class in the previous location is causing an issue.
I'm not saying that is a problem, its just the struggling has to make me wonder.
So, in the interest of hopefully being fully clear of a full example process, I'll start a new project and detail the steps taken.
Also, the only reason I added the AdvRichTextbox to a flow panel is because you mentioned adding it to a TLP, so my adding it to a Flow panel was suppose to be analogous, since I already had the flow panel on a form for another project, I just added the AdvRichTextBox class to that project. I assumed you would replace that line with adding it to your TLP.
Here is a fairly minimal test project
1. Starting a new project (Windows Forms Application), and naming it TestAdvRichTextBox, and letting the IDE "Create a directory for solution".
2. When the IDE is ready (created and showing the Form's design window), select Project from the menu at the top of the IDE and select "Add Class...".
3. Change the name of the file from "Class1.vb" to "AdvRichTextBox.vb" and hit the "Add" button.
4. Delete all the lines generated (3 lines, Public Class AdvRichTextBox, blank line, End Class) in the Class module edit window that appears.
5. Paste in the complete code from post #22 into the module.
6. Return to the "Form1.vb [Design]" window and double click on the form to generate the Form's Class template ("Form1.vb") with Form1_Load event handler snippet.
7. Between the "Public Class" line and the Form1_Load line, add a line to create an instance of the AdvRichTextBox class.
8. In the Form1_Load event handler, add code to add the instance to the forms controls, and test it.
The complete form code should look something like this.
Code:
Public Class Form1
Private justifiableTextBox As AdvRichTextBox = New AdvRichTextBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(justifiableTextBox)
justifiableTextBox.Text = "This is a test to see if the text that is selected will be justified if we choose to."
justifiableTextBox.SelectAll()
justifiableTextBox.SelectionAlignment = TextAlign.Justify
justifiableTextBox.SelectionLength = 0
End Sub
End Class
10. Run the project and you should see a form with a textbox on it with justified text in it.
I just ran the project created above with these steps and it ran without any errors or warnings.
Last edited by passel; Dec 19th, 2014 at 09:00 AM.
-
Dec 19th, 2014, 09:42 AM
#38
Thread Starter
PowerPoster
Re: Justifying text
Hi,
Thank you Passel for the Topic Review, I was just about to post my latest problems.
However I'll now start a fresh project and try to follow exactly what you've said...
I'd like to mention also that yes, you're correct about not all of us being totally au fait with this stuff, in my case you can add poor short term memory due to advancing years, one thing I might suggest is that whenever you use an acronym you might consider spelling the whole thing out for the first time... Although I ought to know what the IDE is, and I think I do know what it is, I have no idea what IDE actually stands for.
Thanks again for your help, it really is appreciated...
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Dec 19th, 2014, 09:56 AM
#39
Re: Justifying text
IDE = Integrated Development Environment = Visual Studio
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Dec 19th, 2014, 10:46 AM
#40
Thread Starter
PowerPoster
Re: Justifying text
Thank you.
What does this line from 'post #22' mean ?
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
Is it VB.NET ?
Poppa.
Along with the sunshine there has to be a little rain sometime.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|