Creative Commons License
Drekendrop | Blog of Tutorial by Mei Pakpahan is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at
Permissions beyond the scope of this license may be available at

Tuesday, March 15, 2011

Infopath 2010: Get and Set Value of TextBox Field Using Visual C# onEvent Button Click

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!


Umair Feroze said...

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.

Mei said...

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?

ehaze said...

thank you!

quick and simple!

Anonymous said...

Same error - no messagebox in infopath. Maybe you referenced something.

Anonymous said...

Thank you very helpful

Anonymous said...

Yes, you have to add reference of System.Windows.Forms.dll

NT said...

Thanks for useful writeup. I had to do Project>Add Reference> System.Windows.Forms then also include the 'using System.Windows.Forms' statement.

Anis Ben Khiroun said...

Thanks Mei it helps.

Anonymous said...

good stuff. thank you :)

Post a Comment

Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms