Welcome!

PowerBuilder Authors: Chris Pollach, Yeshim Deniz, Jayaram Krishnaswamy, Kevin Benedict, Avi Rosenthal

Related Topics: PowerBuilder

PowerBuilder: Article

Highlighting Selected Text in PowerBuilder DataWindow

Techniques for simulating text formatting

Let's suppose that you have a search window where a user can enter a search string and get the list of text items matching this pattern. Ideally, the portions of text that meet the search criteria should be highlighted, for example, by text formatting. It could look like Figure 1.

In PowerBuilder we have a great tool for searching and displaying information - the DataWindow. But how can we do text formatting inside a DataWindow column? Unfortunately, there's no way in the DataWindow object to do that. This article describes a technique that will let you simulate this.

A Sample DataWindow
To explain, let's say that we have a tabular DataWindow with a one-string column called text. We'll only discuss selecting matching text patterns, not filtering data, so our sample DataWindow can have an external data source. The result set description for this DataWindow is very simple and is shown in the Figure 2.

Then we can either add some text data in our DataWindow through the Rows/Data menu command in the DataWindow Painter, or load data from some external source in script.

Selecting Part of the Text
Now we have a DataWindow containing the text we want to match against a string pattern. Next we want to select the portions of text in our DataWindow that meet this pattern, say, by highlighting the text in red.

If there are three rows in the DataWindow:

PowerBuilder rules the world.
It is a rule with us.
This is the rule of the game.

And the string pattern is the 'rule,' we want our rows to look like this:

PowerBuilder rules the world.
It is a rule with us.
This is the rule of the game.

We can't just highlight a portion of the DataWindow column, but we can break the column into three parts: a part before the selection, the selection, and the part after the selection. Let's add another string columnsearch to the result set of our DataWindow to store the string pattern. Instead of the original column text, add three computed fields. The expression for the first computed field is:

if( isNull(search), text,
if( pos(text, search, 1) = 0, text, left(text,
pos(text, search, 1) - 1) )
)

Let's call it "prefix". The expression for the second computed field is:

if(pos(text, search, 1) = 0, '', search)

Let's call it "matched." The expression for the third computed field is:

if( isNull(search), '',
if( pos(text, search, 1) = 0, '', mid(text,
pos(text, search, 1) + len(search) ) )
)

We call it "suffix." Put them side-by-side and size them to the width of original text column. Set the font color for the "matched" to red. Now you can delete the text column.

As a result we get the picture shown in Figure 3 in preview mode. Well done, they're red! But there are nasty gaps between our computed fields and they don't look like one DataWindow column. To properly size and place our computed fields we check on the Slide left property for the matched and suffix computed fields (click the left mouse button on a computed field, Properties, Position tab, Slide groupbox, check the left checkbox). Put your DataWindow in preview:

And here it is - text formatting inside a DataWindow column! So, we have simulated the selection of a portion of text in a DataWindow item just by adding an extra column to the DataWindow result set, creating three computed fields instead of the original column. No script at all! (see Figure 4)

Selecting Several Parts
What if we want to select several parts of the text in a DataWindow item, say two, three, or even more? Then we'll have to create more computed fields for parts of the original column. Let's go further and break ourtext column into the smallest parts - the characters. Every computed field will contain just one character of the text column: the first computed field will contain the first character, the second computed field the second character, and so on. The expression for such a computed field looks like this:

mid( text, , 1)

Then we'll be able to select any characters of the text column by setting the font color of the desired computed fields. We could get a string such as:

PowerBuilder rules the world.

To properly set the font color for our computed fields we can use a technique described by Buck Woolley in his article "Not Your Father's DataWindow" (PBDJ volume 8 issue 7). In short, the essence of this technique is packing parameters of many DataWindow objects in string columns. In our case we have one such parameter for every computed field: selected or not. In our example DataWindow the length of the text column is 50 characters so we'll have 50 computed fields.

Let's add a string column called "select" to the result set of our DataWindow where the packed parameters will be placed. It also has a length of 50.

For the colorful text mentioned above, the "select" column will contain the string:

"0101010101010010100101001010000000<...>"

and so on to the end of string.

OK, so far so good, but how can we create our computed fields? Can you imagine creating 50 (or even more) computed fields manually? This would be madness! Well, it's time for coding.

Selection Service Object
Let's make an object that does all the tedious work for us. First we have to initialize or prepare our DataWindow by creating as many computed fields as the length of the original column. To do this we have to know the DataWindow and the name of the original column. The code for the function of_init() is shown in Listing 1. All visual parameters for the created computed fields are taken from the original column. At the end of this function an instance variable is initialized with a string of 0s.

Second, we need a function that selects the given portion of the text. The arguments for this function are the number of the first selected character, the length of the selection, and the row number in the DataWindow. Let's call it of_select(). The code for this function is shown in Listing 2.

Finally, we need a function that clears a selection in a particular row. The code for this function of_clear() is shown in listing 3.

Conclusion
In PowerBuilder there are no direct ways for text formatting inside a DataWindow column. But by using the techniques described you can simulate text formatting of portions of the text in a DataWindow column. You can use any text formatting you want: change the font color, change the background color, make it bold or italic, or even change the font.

More Stories By Konstantin Goldobin

Konstantin Goldobin is a senior developer living in Voronezh, Russia. He has been working with PowerBuilder since 1995 version 4.0. Visit his web site at www.vsi.ru/~kgold.

Comments (3) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
SYS-CON Australia News Desk 08/17/06 09:03:09 AM EDT

Let's suppose that you have a search window where a user can enter a search string and get the list of text items matching this pattern. Ideally, the portions of text that meet the search criteria should be highlighted, for example, by text formatting.

SYS-CON Italy News Desk 08/16/06 03:37:08 PM EDT

Let's suppose that you have a search window where a user can enter a search string and get the list of text items matching this pattern. Ideally, the portions of text that meet the search criteria should be highlighted, for example, by text formatting.

SYS-CON Brazil News Desk 08/16/06 08:30:32 AM EDT

Let's suppose that you have a search window where a user can enter a search string and get the list of text items matching this pattern. Ideally, the portions of text that meet the search criteria should be highlighted, for example, by text formatting.