I've been working on InfoPath 2010 since a week ago (i'm totally new on it). This was something that i needed to do, and after few hours testing and googling it, finally i could figure out how it works in InfoPath.
1st step---
Right click on your button and choose Button Properties. Click on Edit Form Code... button.
2nd step---
Find the function for the button event. In this case mine is as shown below.
public void updateButton_Clicked(object sender, ClickedEventArgs e)
{
//write your code here
}
3rd step---
Put this codes below
//scripts to get value of Infopath field
XPathNavigator Period1 = this.MainDataSource.CreateNavigator().SelectSingleNode("//my:myFields/my:period_1", this.NamespaceManager);
String P1 = Period1.Value;
//test if it works
MessageBox.Show(P1 + " " + P2, "Warning", MessageBoxButtons.YesNo);
//scripts to set value of Infopath field
XPathNavigator P2 = this.MainDataSource.CreateNavigator().SelectSingleNode("//my:myFields/my:period_2", this.NamespaceManager);
P2.SetValue("This is sample of String");
Let's focus on the bold that i highlighted above.
/my:myFields/my:period_1 and /my:myFields/my:period_2, to get this XPath, go back to your InfoPath designer, and right click on the field name at the right sidebar. And choose Copy XPath.
That's all i want to share today. Happy learning!
9 comments:
Very concise, to the point and very nice article. It saved alot of my time.
The following line is throwing an error:
MessageBox.Show(P1 + " " + P2, "Warning", MessageBoxButtons.YesNo);
It seems that Message Boxes are not allowed.
Glad if it helps you Umair. The MessageBox is supposed to be working because i've run this before i posted. Probably the problem was with P1 and P2?
thank you!
quick and simple!
Same error - no messagebox in infopath. Maybe you referenced something.
Thank you very helpful
Yes, you have to add reference of System.Windows.Forms.dll
Thanks for useful writeup. I had to do Project>Add Reference> System.Windows.Forms then also include the 'using System.Windows.Forms' statement.
Thanks Mei it helps.
good stuff. thank you :)
Post a Comment