Sure, I can give you a quick example of how to do this. Note that this example will only work with InfoPath 2007, so I hope you have that version :-)
1. Make sure that you have Visual Studio Tools for Applications installed. This is available as part of the Office 2007 installation but you might not have it installed yet.
2. Create a form with two fields: my:number, and my:spelledNumber. Represent my:number on your form as a text box, and my:spelledNumber as an expression box.
3. Go to Tools->Form Options...->Programming and select C# for the form template programming language.
4. Right-click my:number's text box, and under Programming, choose Changed event... You might be prompted to save your template, and after that, Visual Studio Tools for Applications will open.
5. Download the file I linked to in my previous post, save it anywhere, and change the extension so it is called "SpellNum.cs"
6. Go back to Visual Studio Tools for Applications, and under the Project menu, select Add Existing Item... and add SpellNum.cs, which you just downloaded.
7. Go to the top of your FormCode.cs file, which should be the only one open, and under the several lines that begin with using, add the line:
using
Spell;
This tells your FormCode.cs file that it should be able to access the functions inside SpellNum.cs
8. Now go to the number_Changed event handler at the bottom of FormCode.cs, and add code so that it ends up looking like this. The green lines explain what each line of code does:
public
void number_Changed(object sender, XmlEventArgs e)
{
// create an object to navigate the main data source
XPathNavigator nav = MainDataSource.CreateNavigator();
// create an object to access the my:number node
XPathNavigator number = nav.SelectSingleNode("/my:myFields/my:number", NamespaceManager);
// create an object to access the my:spelledNumber node
XPathNavigator spelledNumber = nav.SelectSingleNode("/my:myFields/my:spelledNumber", NamespaceManager);
// enclose the number handling code in a try block, to catch any error from parsing the number
try
{
// parse the value of the my:number node and store it in a variable
long numVar = Int64.Parse(number.Value);
// convert the number into its spelled form and store it in a variable
string spelledNumVar = SpellNumbers.SpellNum(numVar);
// store the value in the my:spelledNumber node
spelledNumber.SetValue(spelledNumVar);
}
// catch any parsing error (will happen if my:number contains something other than an integer
catch (FormatException)
{
// Show an error to the user
MessageBox.Show("Please enter a valid number!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
It may look confusing now, but once you make sense of it, you can reuse it with little modification.
9. Now under the Debug menu of Visual Studio Tools for Applications, select Start Debugging. This should bring up the InfoPath form. Try typing a number into the text box and then clicking off the text box. If all went well, the expression box should show the spelled number.