Random generated textbox field InfoPath 2010 - InfoPath Dev
in

InfoPath Dev

Random generated textbox field InfoPath 2010

Last post 03-21-2013 11:49 AM by RSmith. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 08-30-2011 08:11 AM

    Random generated textbox field InfoPath 2010

    Hello,
              I have List infopath form 2010 in which one textbox field needed to show randomly generated integeter  (auto number) number. Few months ago i came across Clayton Cobbs article about generating random number which is created when form has been submitted. But can we show random generated number when form is opened? Thanks

  • 09-01-2011 12:48 PM In reply to

    Re: Random generated textbox field InfoPath 2010

    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:

    1. On the Tools menu, select Programming, and then click Loading Event.
    2. 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,

    Karsten
    Filed under:
  • 09-01-2011 11:00 PM In reply to

    Re: Random generated textbox field InfoPath 2010

    If you don't want to write code, try the qRules trial. It includes a command to calculate Random numbers. :)
    Patrick Halstead
    Project Manager at Qdabra
  • 09-02-2011 06:33 AM In reply to

    Re: Random generated textbox field InfoPath 2010

    Hey Karsten Brocksieper ,
                                           Thank you so much for you reply!!

  • 03-21-2013 11:49 AM In reply to

    • RSmith
    • Not Ranked
    • Joined on 03-21-2013
    • Posts 1

    Re: Random generated textbox field InfoPath 2010

    Very fresh to C# programming and basically pulled your code out for a specific case. However, I have a question - the min and max that i want to set for the random number generation are field values, how would i incorporate this?

    Thanks

Page 1 of 1 (5 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.