Form field place holders in strings?

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

If I have the following text in a dataset table column: "This agreement
establishes that {BandName} will play on {EventDate} between {StartTime} and
{EndTime}..." How would I then take the field names {BandName}, {StartTime},
{EndTime} and {EventDate}, match them up with actual dataset table column
names, and show the actual values instead of the placeholders on the final
page output? I know this would probably require some string parsing and
stuff, but I need some good direction here. The other thing is that this
whole thing is totally dynamic. I wont know any of the text or placeholder
names (or table column names) in advance. These forms are being creating
from a wizard control (including what form fields are used). Any ideas on
where to get started?
 
Lets' start with the assumption that you have 'extracted' the 'display' text
into a string variable called, say, _displaytext.

There are 2 things that you know.

1. There may be 1 or more tokens that startwith { and end with }.

2. The inner portion of each token is the name of a column in your datarow.

First, extract inner portion of the tokens into a String array.

Then, remove all the unwanted text from each element and replace the token
in _displaytext with the text from the relevant column from your datarow.
Ignore any elements that do not contain a token:

Dim _ss As String() = _displaytext.Split("{"c)

For _i As Integer = 0 To _ss.Length - 1
If _ss(_i).Contains("}") Then
_ss(_i) = _ss(_i).Substring(0, _ss(_i).IndexOf("}"))
_displaytext = _displayText.Replace("{" & _ss(_i) & "}",
_datarow(_ss(_i)).ToString())
End if
Next

After the split, the ellements of _ss are:
This agreement establishes that
BandName} will play on
EventDate} between
StartTime} and
EndTime}...

In the loop, the first element will be ignored because it doesn't contain a
token. The other 4 elements each startwith a token because they contain a }.

The first statement in the If ... Then ... End If block strips the } and all
the following text from the token so that the 4 elements of interest become:
BandName
EventDate
StartTime
EndTime

The second statement in the If ... Then ... End If block replaces the token
in _displaytext with the required value from your datarow. Note that the {
and } must be included in the replace for it to work properly.

There are some issues that you will have to deal with, some of which are:

Case-sensitivity: The name for the token MUST match the name of
the column EXACTLY including casing.

What if the value from the datarow required further formatting,
e.g., dates, times, numbers etc.?

What if the value from the datarow is blank or DbNull?

These are rhetorical questions but they are things you must consider.

What I have presented in not the only way of doing it by any means but it
gives you a starting point.
 
Andy,

As it was my problem and it was as simple like you show it, then I would
make 3 replace statements.

TheText = TheText.Replace("{BandName}",TheBandName)
etc, for the other two.

Cor
 
Read Andy's penultimate sentence again!

Cor Ligthert said:
Andy,

As it was my problem and it was as simple like you show it, then I would
make 3 replace statements.

TheText = TheText.Replace("{BandName}",TheBandName)
etc, for the other two.

Cor
 
Stephany,

It is not worse then yours, if you don't know were New Zealand is, you
probably can go to Holland, Danmark, those two isles 2000 km east of
Australia or other place in the world.

Andy tells that he does not know anything, therefore I gave him a start as I
would start.

Although I often used IndexOf (kind of javascript background), I would not
use it here.

Cor
 
You must have got some really bad grass today!

Since when have Holland, Danmrak (sic) been isles and since when have they
been 2000 Km east of Australia.

He stated very clearly that he does NOT know, at design-time, what the
tokens will be nor does he know, at design-time, the names of the columns in
the datatable that relate to the tokens.
 
Ignoring your bad humor to day, he does not even know it there any {} in it,
so your solution does not work as well.

Otherwise it is of course Regex.

Cor
 
Hi.

I have a question related to your example steeve. How would you actually
determine if there are any {token} elements in the text of a particular text
string? What I have to do, is to go through all of the text sections and
determine if there are any token elements. If there are token elements,
replace them with the actual column values. We have this part covered so
far. If there are no token elements in the text strings, just append the
"form fields" that have been created already at the end of the string values
(like an attached form for a contract for service). Do you think this is a
good way of doing things? Or should I just create a whole other creation
wizard just for this type of form? I don't really know if there would be a
ton of overhead, since only 2 people will have access to this feature set
and it isn't expected to be used a whole lot as it is anyways. Tnx for the
help already given and the help in advance...
 
Back
Top