DTPicker.OnBeforeChange Event: Form Freezes - InfoPath Dev
in

InfoPath Dev

DTPicker.OnBeforeChange Event: Form Freezes

Last post 01-28-2011 06:04 PM by abcolson. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 01-13-2011 10:02 AM

    • abcolson
    • Not Ranked
      Male
    • Joined on 12-12-2007
    • San Diego
    • Posts 20

    DTPicker.OnBeforeChange Event: Form Freezes

    Hello All,

    This is a similar post to one I put in the Code forum.  I didn't get a response and I am thinking the post was mis-posted, so I am re-posting in this forum. 

    The scenario is that I have a DTPicker (drop down calendar date picker) and in its OnBeforeChange event I want to open a .Net form (C#).  Using the ShowDialog() method, the form opens but then InfoPath and the form freezes; I cannot drag/move the form window, click any controls, or click in the main InfoPath window.  Other .Net forms exhibit the same behavior in this event handler.  Interestingly, the .Net MessageBox.Show routine works just fine.  Also interestingly, when I minimize all applications (by clicking on the icon in the Windows toolbar) and then maximize, both InfoPath and my .Net form behave as expected; I can drag/move the form and click on controls.  When I open forms in other OnBeforeChange event handlers they also work as expected.

    Why would this be?

    I know that the InfoPath forms get put into a special state in the OnBeforeChange events but I don't know the technicals, nor do I know what magically happens when I minimize/maximize the application such that it resets whatever is causing me problems.

    The code looks like:

    public void Report_Date_OnBeforeChange(DataDOMEvent e)
    {
         DateChangeConfirmForm dcc =
    new DateChangeConfirmForm();
         DialogResult result = dcc.ShowDialog();
    }

    Thank you for any insight.

    Filed under: , ,
  • 01-15-2011 06:18 AM In reply to

    Re: DTPicker.OnBeforeChange Event: Form Freezes

    Hi ABColson,

    ShowDialog is a modal dialog box. You are interrupting the OnBeforeChange event and thereby preventing the InfoPath UI thread from getting any more UI input. You should not be putting ShowDialog() calls in your event handlers. That's definitely not a good practice since it blocks all UI from happening until the modal dialog box is filled out. If you have to use modal dialogs, use OnClick events instead where the users expect to be interrupted. Bottom line: you should be using code for your form and even if you do you shouldn't be using ShowDialog().

    Please explain what you are trying to achieve and I'm sure there is a better approach that doesn't involve going down the path you were on.

    :)

    Patrick Halstead
    Project Manager at Qdabra
  • 01-17-2011 09:36 AM In reply to

    • abcolson
    • Not Ranked
      Male
    • Joined on 12-12-2007
    • San Diego
    • Posts 20

    Re: DTPicker.OnBeforeChange Event: Form Freezes

    Hello Patrick,

    Thank you for helping.  Your response puzzles me a bit as I see nothing wrong with putting a modal dialog box in an event handler especially if it makes sense to the program (you mentioned the OnClick event but that is also an event handler).  Please explain your reasoning further.

    What I am trying to accomplish is a confirmation when the user changes the date on a form.  As stated, I have a DTPicker control which is bound to the Report Date on a data submission form.  When the user changes the Report Date (by clicking a different date) we want to confirm the intent and ask 3 questions: 1) Do you want to create a new form with the new date? 2) Do you want to change the date for the current form?  3) Do you want to cancel?  This is important as this is going to a database.  If they submit the form and then change the date on the current form to re-submit it will change the Report Date in the database for what was just submitted; hence, the question of creating a new form (for a new submission).

    Interestingly, if I change the report date manually (by typing in the new date into the DTPicker) and then hitting TAB everything works as expected, it is just when I point and click with the mouse.

    I suppose my alternative is to redo my program logic to use the AfterChange event handler instead, but, according to you, that is also bad as it is also an event handler.  Recall also that the MessageBox.Show() method (which is also a modal dialog box) works correctly in the BeforeChange event handler.  I also don't see any other appropriate Rule mechinisms.

    Please let me know if you see a viable alternative or what might be happening which is causing the mouse events to interfere.  Somehow, all is made righteous when using MessageBox.Show().

    Thank you,
    Tony

     

  • 01-17-2011 09:45 AM In reply to

    • abcolson
    • Not Ranked
      Male
    • Joined on 12-12-2007
    • San Diego
    • Posts 20

    Re: DTPicker.OnBeforeChange Event: Form Freezes

    I Forgot to re-mention...the process of minimizing/restoring the form also clears the interferece and all works as expected then.

  • 01-27-2011 03:14 PM In reply to

    • abcolson
    • Not Ranked
      Male
    • Joined on 12-12-2007
    • San Diego
    • Posts 20

    Re: DTPicker.OnBeforeChange Event: Form Freezes

    Hi Patrick,

    This is still an issue.  Have you had any thoughts?

    Tony

     :)

  • 01-27-2011 07:49 PM In reply to

    Re: DTPicker.OnBeforeChange Event: Form Freezes

    Hi Tony,

    Thanks for the re-ping. I was thinking that you were trying to show a modal dialog at the wrong time and that was causing the InfoPath windows thread to go into spasms because it couldn't complete a display task. But, sounds like you have proven that that's not the case and it's more of just a bug in InfoPath or some strange behavior.

    Anyway, don't bother with modal dialogs. Just have a section in your form that expands when the data value changes. Add a rule to the date field that sets a field that controls whether or not to show the three questions. The value of that field should default to false. Call that field "ShowConfirmation" or something. You want to add a formatting rule to a section that includes the three questions. Basically if ShowConfirmation is false you want to hide the section. Otherwise, you show it. You have 3 fields for your questions. Add two more for username and datetime. Add a button to the bottom of the section that sets the username and datetime and then sets ShowConfirmation to false again to hide it. The button label can be "Confirm". After you hide the section you can have another section that includes a calculated value that concatenates the username and date time when they confirmed it and displays it in a nice format underneath the datepicker, or whatever. Since it's a calculated value it's read-only.

    That's how I would do it. Much less work and easy to capture the data and uses InfoPath as it was intended.

    Patrick Halstead
    Project Manager at Qdabra
  • 01-28-2011 06:01 PM In reply to

    • abcolson
    • Not Ranked
      Male
    • Joined on 12-12-2007
    • San Diego
    • Posts 20

    Re: DTPicker.OnBeforeChange Event: Form Freezes

    Thanks Patrick,

    I really appreciate your approach.  I will keep this in mind for other projects.  My users really wanted a popup box, so ...

    To conclude (for everyone else) ...

    I wasn't able to pinpoint the exact cause of the problem.  I am led to believe that the errant behavior is due to a bug in the DTPicker control pertaining to [a looped cycle of] mouse events.  As the MessageBox.Show() example above shows, there is a way to circumvent this (as it works fine) but I couldn't figure it out without getting some serious help from Microsoft (or a good book) and digging into the Windows API.  So when all else fails...improvise!

    Patrick, your using of the phrase "windows thread" served to strengthen my suspicion that I was going to have to find a solution using threading.  I didn't want to, but that is what I did and it works.

    For others who have this problem, I am posting my code below.  I created a function (C#) in my InfoPath FormCode.cs file to spawn off a thread and run the form (as a dialog) from the other thread.  This code is called as follows:

                ConfirmChoiceEnum LaunchDateChangeForm()            {                  ConfirmChoiceEnum choice = ConfirmChoiceEnum.NONE;                  ThreadDialogDisplay tdd = new ThreadDialogDisplay();                  if(tdd.Display())                  {                        while(!tdd.ThreadFinished);                        choice = tdd.ConfirmChoice;                        tdd.Join();}                  tdd = null;                  return choice;            } 

    The class of interest is the ThreadDialogDisplay class:

          public class ThreadDialogDisplay
          {            private DateChangeConfirmForm dcc = null;             private bool a_threadFinished = false;             private Thread theThread = null;             public ThreadDialogDisplay()            {            }             public ConfirmChoiceEnum ConfirmChoice            {                  get                  {                        if( dcc == null ) return ConfirmChoiceEnum.NONE;                        return dcc.ConfirmChoice;                  }            }             public bool ThreadFinished            {                  get                  {                        return a_threadFinished;                  }            }              public bool Display()            {                  a_threadFinished = false;             theThread = new Thread(new ThreadStart(this.ThreadDisplay));            theThread.Name = "Thread to display DateChangeConfirmForm.";                                    try                  {                        theThread.Start();                        return true;                  }                  catch(Exception e)                  {                        return false;                  }            }             public void Join()            {                  theThread.Join();            }             private void ThreadDisplay()            {                  try                  {                        dcc = new DateChangeConfirmForm();                        dcc.ShowDialog();                  }                  catch(Exception e)                  {                  }                  finally                  {                        a_threadFinished = true;                  }            }      }
     I hope this helps someone.  And thank you Patrick for your help! Tony
  • 01-28-2011 06:04 PM In reply to

    • abcolson
    • Not Ranked
      Male
    • Joined on 12-12-2007
    • San Diego
    • Posts 20

    Re: DTPicker.OnBeforeChange Event: Form Freezes

    Sorry about the code.  When I previewed it it looked fine.  Don't know what happened.

    Is there an adminsitrator who can re-format this?

    And feel free to delete this reply :)

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