Perform a button click through code - InfoPath Dev
in

InfoPath Dev

Perform a button click through code

Last post 07-02-2012 08:27 AM by Doreen. 13 replies.
Page 1 of 1 (14 items)
Sort Posts: Previous Next
  • 06-28-2012 08:39 AM

    • Doreen
    • Top 50 Contributor
      Female
    • Joined on 08-11-2010
    • Federal Way, WA
    • Posts 291

    Perform a button click through code

    In Windows forms there is a method to perform a button click called PerformClick() described by msdn article here: http://msdn.microsoft.com/en-us/library/hkkb40tf(VS.90).aspx

    This didn't work in Infopath 2007 for me. I set it up like the example in the article referencing the Infopath button control ID like this: CTRL71_11.PerformClick();  Is there a way to do this in InfoPath?  I assume I have to use some xml type coding which I am not yet familiar with how to code  

    Example code from msdn article:

    In button1 click event:

    MessageBox.Show("button1.Click was raised.");

    -----

    In button2 click event:
    // Call the Click event of button1.
    button1.PerformClick();

    I want to have a print button with code (this.print()),  that calls a submit button with rules (rules will name the form, submit it to SharePoint library and close the form).

    Can anyone help me code this?

  • 06-29-2012 03:43 AM In reply to

    Re: Perform a button click through code

    Doreen:
    In Windows forms there is a method to perform a button click called PerformClick() described by msdn article here: http://msdn.microsoft.com/en-us/library/hkkb40tf(VS.90).aspx

    This didn't work in Infopath 2007 for me. I set it up like the example in the article referencing the Infopath button control ID like this: CTRL71_11.PerformClick();  Is there a way to do this in InfoPath?  I assume I have to use some xml type coding which I am not yet familiar with how to code  

    Example code from msdn article:

    In button1 click event:

    MessageBox.Show("button1.Click was raised.");

    -----

    In button2 click event:
    // Call the Click event of button1.
    button1.PerformClick();

    I want to have a print button with code (this.print()),  that calls a submit button with rules (rules will name the form, submit it to SharePoint library and close the form).

    Can anyone help me code this?

     

    IMO, this your cited method is unrelated to coding in Infopath form since in the former the button is defined statically and known at compile time, an in the latter (in case of Infopath form) the code is for add-on and for objects constructed dynamically at run-time (the objects there, at run-time ebvironment,  can even be added, removed or re-defined).  

    If you want to do the same in compatible with Infopath approaches without trying to overcomplicate and make impossible the simple things then it is not the problem.

    Infopath form is just XML (msotly XML) data for a form that is to be used by external tools which Infopath form is not even aware of.

     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 06-29-2012 07:49 AM In reply to

    • Doreen
    • Top 50 Contributor
      Female
    • Joined on 08-11-2010
    • Federal Way, WA
    • Posts 291

    Re: Perform a button click through code

    I realize what I sited is related to Windows forms only - I was using this as an example of a functionality I was looking to acheive in InfoPath form code. If it can't be done, then that is all I want to know. If it can be done I'd like help on making it happen.

  • 06-29-2012 08:19 AM In reply to

    Re: Perform a button click through code

    Are you asking about Infopath 2007?
    Are asking about performing the same action by clicking both on one button and another?

    I am on Infopath 2010 and can post you the code for it if you are interested but I am still uncertain what you are asking about

    On 1st and 2nd button's properties in button's code click "Edit code..."  button (under General tab), having in its "Action:" drop-down list:: Rules and custom code chosen

     Inline image 1

    These (2 clicks) will generate:

    public void InternalStartup()

    {

           ((ButtonEvent)EventManager.ControlEvents["CTRL2_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked);

           ((ButtonEvent)EventManager.ControlEvents["CTRL3_5"]).Clicked += new ClickedEventHandler(CTRL3_5_Clicked);

    }


    In oder to perform the same method, for example CTRL2_5_Clicked() on clicking both buttons, change it to 

    public void InternalStartup()

    {

         ((ButtonEvent)EventManager.ControlEvents["CTRL2_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked);

         ((ButtonEvent)EventManager.ControlEvents["CTRL3_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked);

    }

     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 06-29-2012 11:22 AM In reply to

    • Doreen
    • Top 50 Contributor
      Female
    • Joined on 08-11-2010
    • Federal Way, WA
    • Posts 291

    Re: Perform a button click through code

    Gennady Vanin,

    Yes I am using InfoPath 2007, and my question is: I want to know how to fire off a 2nd button programmatically when user clicks the 1st button.

    Example: there are two buttons.  User clicks 1st button, it fires off (no rules, just code behind), the code behind of 1st button causes 2nd button to fire off (2nd button has rules, no code). The user never physically clicks the 2nd button.

    THANK YOU for submitting code to try.  I had no idea I could do this with the InternalStartup button code...I haven't tryed it yet but I definately will and follow up. Sorry for the unclarity of my first message.

    Doreen

  • 06-29-2012 11:27 AM In reply to

    • Doreen
    • Top 50 Contributor
      Female
    • Joined on 08-11-2010
    • Federal Way, WA
    • Posts 291

    Re: Perform a button click through code

    Rocksandmountains,

    I feel your pain.

  • 06-29-2012 11:37 AM In reply to

    Re: Perform a button click through code

    Rocksandmountains:

     Oh, there's a god out there.

    You saved us. Thanks for the "secret formula", I'm having a bit of issues with coding in InfoPath. So not user-friendly... 

     

    Thanks, I am what I am 

    I am to agree. This "codeless" approach is a just deceptiive trap even for seasoned developers and next-to-impossible to explain to employer what's the problems with "too easy" but in reality impossible tasks. I've just lost employment due to this without being paid for 6-week work

    That is, it is easier to code than to overcome all pitfalls, traps, bugs, steep learning curves of this "codeless"  (Infopath) approach

     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 06-29-2012 11:41 AM In reply to

    Re: Perform a button click through code

    My Self The Enemy:
    public void InternalStartup()
            {
                ((ButtonEvent)EventManager.ControlEvents["CTRL2_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked);
                ((ButtonEvent)EventManager.ControlEvents["CTRL3_5"]).Clicked += new ClickedEventHandler(CTRL3_5_Clicked);
            }

    In oder to perform the same method, for example CTRL2_5_Clicked() on clicking both buttons, change it to
            public void InternalStartup()
            {
                ((ButtonEvent)EventManager.ControlEvents["CTRL2_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked);
                ((ButtonEvent)EventManager.ControlEvents["CTRL3_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked);
            }

    Perhaps I'm confused, but does that accomplish anything different from doing this (which would be preferable to tampering with the InternalStartup method):

    void PerformSubmit()
    {
       // Do stuff
    }

    void CTRL2_5_Clicked(object sender, EventArgs e)
    {       
        // Print page
        PerformSubmit();
    }

    void CTRL3_5_Clicked(object sender, EventArgs e)
    {
         PerformSubmit();
    }

    I would have suggested this sooner, but I thought Doreen was looking for a way to run the Submit button's rules after printing the page, which would be more complicated.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 06-29-2012 01:39 PM In reply to

    Re: Perform a button click through code

    Jimmy:
    Perhaps I'm confused, but does that accomplish anything different from doing this (which would be preferable to tampering with the InternalStartup method):
     

    No, it does not differ
    I even did not think much

    My approach consists in changing one symbol in code.
    Your approach requires a lot more typing

     "Tampering" sounds to me synonymous to "cracking".
    Isn't user tampering when he presses "Edit Form Code..." button inserting:

    ((ButtonEvent)EventManager.ControlEvents["CTRL2_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked);

     into  InternalStartup method?

    Me too - confused what's wrong in what I have done

     

     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 06-29-2012 02:06 PM In reply to

    Re: Perform a button click through code

    Doreen:
    Gennady Vanin,

    Yes I am using InfoPath 2007

     

    I am using 2010 but "code behind" in IPD2010 is created by antiquated Visual Studio 2005 Tools for Applications with .NET 2.0
    So, my guess is that manual and oral "coding" in IP2010 should not differ much  

    Doreen:
    and my question is: I want to know how to fire off a 2nd button programmatically when user clicks the 1st button

    Me doesn't know.
    I am not magician yet I am just studying to become him.

    And I do not even want to know sincу I do not see much sense in it

    Doreen:
    Example: there are two buttons.  User clicks 1st button, it fires off (no rules, just code behind), the code behind of 1st button causes 2nd button to fire off (2nd button has rules, no code). The user never physically clicks the 2nd button

    Never never? ever?
    Why do you need such a button?

     Why don't you simply copy the same rules from 2nd button to 1st one?

    Doreen:
    THANK YOU for submitting code to try.  I had no idea I could do this with the InternalStartup button code...I haven't tryed it yet but I definately will and follow up. Sorry for the unclarity of my first message.

    Doreen

    It is not "InternalStartup button code"
    And you do not need to think anything

    Clicking "Edit Form Code..." button in the properties of one CTRL2_5 button inserts

    • ((ButtonEvent)EventManager.ControlEvents["CTRL2_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked); 

    into InternalStartup()

    And  Clicking "Edit Form Code..." button in the properties of one CTRL3_5 button inserts

    • ((ButtonEvent)EventManager.ControlEvents["CTRL3_5"]).Clicked += new ClickedEventHandler(CTRL3_5_Clicked); 

    into InternalStartup()

     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 06-29-2012 07:55 PM In reply to

    Re: Perform a button click through code

    My Self The Enemy:
    "Tampering" sounds to me synonymous to "cracking".
    Isn't user tampering when he presses "Edit Form Code..." button inserting:

    ((ButtonEvent)EventManager.ControlEvents["CTRL2_5"]).Clicked += new ClickedEventHandler(CTRL2_5_Clicked);

     into  InternalStartup method?

     Causing changes to happen in InternalStartup() via the InfoPath interface, and manually editing the method are two different things.  Directly above the method, there's this auto-generated comment:

     // NOTE: The following procedure is required by Microsoft InfoPath.
    // It can be modified using Microsoft InfoPath.

    Now, it doesn't explicitly say anything about not manually editing it, but I would interperet that as a warning against manually editing it, and if there's a simple alternative, I would use the alternative.

    Doreen, have you found a solution yet to your issue?  There isn't a way to invoke a button's rules from code, but I can think of two alternatives:

     First alternative:

    1. Create a secondary XML file data connection with a field called Print.  Add an OnChanged handler to that Print field, and have that event handler print the form.
    2. Change your print button so its rules are like this:
      Set Print field to "go"
      (Then duplicate all of the rules from the Submit button)

    Second alternative:

    1. Create a method in your code called SubmitAction() that does all of the same stuff as your Submit button, but in code instead of rules.
    2. Add an event handler to your Submit button, and have it call SubmitAction()
    3. Modify your Print button's event handler to call SubmitAction() after it does the printing.

    I believe that those will work, but please give them a try.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 07-01-2012 01:20 AM In reply to

    Re: Perform a button click through code

    Jimmy:
    Causing changes to happen in InternalStartup() via the InfoPath interface, and manually editing the method are two different things.  Directly above the method, there's this auto-generated comment:

     // NOTE: The following procedure is required by Microsoft InfoPath.
    // It can be modified using Microsoft InfoPath.

    Now, it doesn't explicitly say anything about not manually editing it

     

    as well as it does not explicitly refer to any specific method, be it InternalStartup or another .

    "Following procedure" is really all methods below (="following") these comments

    I would have believed your opinion if that  InfoPath interface not only inserted but also cleaned the code after itself.
    For example, if to delete 2nd or 3d button for which a code was generated by clicking "Edit Form Code..." in their button's props such code is not removed and there is no way to clean it except manually.

    In Infopath the mortal developer is not only unable to tamper but even see the code which is essential for editing but nevertheless is kept hidden and locked from manual (custom) coding/development.

     

    Gennady Vanin (Novosibirsk) --- Геннадий Ванин (Новосибирск)
  • 07-01-2012 08:56 AM In reply to

    Re: Perform a button click through code

    My Self The Enemy:
    as well as it does not explicitly refer to any specific method, be it InternalStartup or another .

    The comment is directly above the InternalStartup() method, so yes, it is referring to InternalStartup(), even if it doesn't mention it by name.

    "Following procedure" is really all methods below (="following") these comments

    No, that's incorrect.  Since procedure is singular, "following procedure" can only mean "the procedure right after this."

    In Infopath the mortal developer is not only unable to tamper but even see the code which is essential for editing but nevertheless is kept hidden and locked from manual (custom) coding/development.

    I don't know what this is supposed to mean.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 07-02-2012 08:27 AM In reply to

    • Doreen
    • Top 50 Contributor
      Female
    • Joined on 08-11-2010
    • Federal Way, WA
    • Posts 291

    Re: Perform a button click through code

    Jimmy,

    Thank you. I actually applied your second alternative on my own prior to the recent "commentary" in this forum. Only I used FormEvents_Submit() instead of SubmitAction() (hmmm?). It worked well with one exception - the form did not close once it was submitted.  To remedy that, I put Application.Quit () method in button click event after FormEvents_Submit()method (Note: Microsoft says you cannot put Application.Quit() method in the FormEvents_Submit() method).


    Application.Quit () method never ran  because of an error (not verbatim): "method does not use zero parameters" . It needed values for the missing parameters; 'sender' and 'e' but I didn't know what those values should be.   I had added the FormEvents_Submit()method manually without those pararmeters.  When I added the FormEvents_Submit() method properly; from InfoPath Tools menu, choose submit options and checked "Perform custom action using code"  which in turn adds InfoPath generated code to the InternalStartup method and thus gives it two parameters,  through debugging found code issued print then submit but then jump back to button click at FormEvents_Submit() break point then reopen the form (and never hit the Application.Quit() method which was last. 

    It got too time-consuming to troubleshoot so ultimately I ended up with two buttons; a 1) print buttons with code behind and no rules, and a 2) submit button with rules only and no code behind. Now that I've explained all this, I feel  need to try this with the SubmitAction() you method you recommended to see if it behaves differently, hmm again?

      

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