This message contains graphics. If you do not see the graphics: Graphic Version.
 
Creating a TX Text Control inherited control

If you have worked with TX Text Control for any extended period of time, you will no doubt have asked yourself: "Why doesn't TX Text Control have this property?" or "I'm fed up of coding this time and time again. Isn't there a better way?"

This week, we are going to show you how to answer these questions by using inheritance in .NET to create your own, customized TX Text Control.

We are going to create a control that changes TX Text Control's behavior to a browser-like control. It offers a simple method to insert hyperlinks. Upon clicking a hyperlink, the browser is launched and the URL is opened. The advantage (to you as the programmer) is that every time you need a browser control, you simply create an instance of this class and you are done.

So, first of all, let us see how a class can inherit from TX Text Control. Here is the basic layout of such a class:

Imports TXTextControl Public Class TXHtml Inherits TXTextControl.TextControl Public Sub New() MyBase.New() MyBase.PageMargins.Left = 0 MyBase.PageMargins.Right = 0 MyBase.PageMargins.Top = 0 MyBase.PageMargins.Bottom = 0 End Sub End Class

The New() method is what is called the constructor in object orientated programming. Calling MyBase.New() creates the base class TextControl. After that, we can set some default values. In this case, we set the page margins to zero.

Now, how do we use the control in our form? Simply drag a TX Text Control to your form as you would do it usually. Now find the location in your form's code where TextControl is created. It should look like this:

Me.TextControl1 = New TXTextControl.TextControl()

Simply change it to your new class' name:

Me.TextControl1 = New TXHtml.TXHtml()

Now, it's time to add some life to our class. In the sample, we want change the control's behavior when it is resized. To achieve this, we need to override the OnResize event.

Protected Overrides Sub OnResize(ByVal e As System.EventArgs) Dim pagesize As System.Drawing.Size pagesize.Width = MyBase.Width pagesize.Height = MyBase.Height MyBase.PageSize = pagesize End Sub

What happens here is that the page width is set to the new width of the resized control, thus the text in the control always fills the control completely in horizontal direction. This is how web pages are displayed in browsers.

The code is very straight forward, but you can imagine that you can totally change TX Text Control's behavior by overriding events.

The same technique is used to launch the default browser when a hyperlink is clicked.

In addition to the overriding of events, you can also override methods and properties. You can also add functionality with new methods or properties. That is what the new method InsertHyperlink does:

Public Sub InsertHyperlink(ByVal linkText As String, ByVal linkTarget As String) Dim link As TXTextControl.HypertextLink Dim oldUnderline As FontUnderlineStyle Dim oldColor As System.Drawing.Color ' Store current values oldUnderline = MyBase.Selection.Underline oldColor = MyBase.Selection.ForeColor MyBase.Selection.Underline = FontUnderlineStyle.Single MyBase.Selection.ForeColor = System.Drawing.Color.Blue link = New TXTextControl.HypertextLink(linkText, linkTarget) MyBase.HypertextLinks.Add(link) ' Restore old values MyBase.Selection.Underline = oldUnderline MyBase.Selection.ForeColor = oldColor End Sub

It takes the hyperlink's text and it's target as parameters and inserts a hyperlink that is underlined and blue - all in one step.

You can see that you can create a powerful control by putting complicated calls into a single method of your inherited control.

We wish you a lot of success with this sample application. As always, we are delighted to help you with any matter relating to TX Text Control. Call us or e-mail us! Full contact details at:


Best regards

The Newsletter Team

Text Control GmbH respects your online time and privacy. We only send this newsletter to TX Text Control customers and people who have signed up to receive it. However, if you would prefer not to receive future issues of the newsletter, you may unsubscribe at any time. If you received this newsletter forwarded from a colleague or friend, you may wish to subscribe directly.

Sent to: N/A.

Imprint | Unsubscribe | Subscribe

© 2003 Text Control GmbH. All Rights Reserved.