Hello,
you can´t use rules because there´s no function available in XPATH to calculate a random number. So you have to write code in your "FormEvents_Loading" procedure.
To use code to load data when an InfoPath form opens:
- On the Tools menu, select Programming, and then click Loading Event.
- Write code in the Loading event to set the values of chosen InfoPath form fields.
You can set the value of a textbox withe the following code in c#:
XPathNavigator nav = MainDataSource.CreateNavigator();
nav.SelectSingleNode("//my:Name_of_Textbox", NamespaceManager).SetValue("myValue_Textbox");
Now if you want to set the textbox with a random number you only have to use the class for generating a random number which is in the .Net framework. You can even set the upper and lower limits so you end up with a few lines of code...
private int RandomNumber(int
min, int
max)
{
Random random = new Random();
return random.Next(min, max);
}
So your whole code in the editor looks like the following snippet --- you only have to change your fieldname:
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
namespace Form1
{
public partial class FormCode
{
public void InternalStartup()
{
EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
}
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
XPathNavigator nav = MainDataSource.CreateNavigator();
nav.SelectSingleNode("/my:myFields/my:field1", NamespaceManager).SetValue(RandomNumber(1,100));
}
private string RandomNumber(int min, int max)
{
Randonm random = new Random();
return random.Next(min, max).ToString();
}
}
}
Regards,