none
Setting Focus to Outlook Form Region RRS feed

  • Question

  • Using VS2012 with Office 2010.

    We have an Outlook AddIn that presents a custom FormRegion when a Ribbon button is clicked.  We are trying to set the focus on the first control of my form when the form is displayed so users may begin entering data into the form.  But when the form displays, users must still click the form to set the focus before they can begin entering data.

    We are creating a new instance of my form using the Click() event of a ribbon button:

        Public Sub Button1_Click(ByVal control As Office.IRibbonControl)

            Dim Items As Outlook.Items = _

                Globals.ThisAddIn.Application.ActiveExplorer.CurrentFolder.Items

            Dim Item As Object = Items.Add("IPM.MyMessageClass")

            Item.Display()

        End Sub

     

    In the Form Region Showing event, we set the focus using:

        Private Sub FormRegion1_FormRegionShowing(ByVal sender As Object, _

                  ByVal e As System.EventArgs) Handles MyBase.FormRegionShowing

            TextBox1.Focus()

        End Sub

     

    When the form displays, we don’t see a blinking carat in TextBox1 until we click somewhere on the form.  What are we doing wrong?



    Monday, December 9, 2013 6:44 PM

All replies

  • Hello Peter,

    Please try to use the Select method of the System.Windows.Forms.TextBox. If the Select method doesn't help you need to activate the form region first. 

    Monday, December 9, 2013 7:41 PM
  • Thanks for the reply, Eugene.

    The TextBox1.Select() method doesn’t set the focus to the control when the form displays.  Further, during execution of FormRegion1_FormRegionShowing() the form isn’t visible.  Is it possible we are setting the focus too early?  How do we activate the form when the window displays?

    Maybe we're trying too hard? :)


        Private Sub FormRegion1_FormRegionShowing(ByVal sender As Object, _

            ByVal e As System.EventArgs) Handles MyBase.FormRegionShowing

     

            Try

     

                ' set focus to first control

                Me.OutlookFormRegion.Select()

                Me.Select()

                Me.TextBox1.Select()

                Me.TextBox1.Focus()

     

            Catch ex As Exception

                Windows.Forms.MessageBox.Show(ex.ToString, My.Application.Info.Title)

            End Try

     

        End Sub



    Tuesday, December 10, 2013 5:39 PM
  • Hello Peter,

    You are right. The FormRegionShowing event occurs before the form region is displayed. Here is what MSDN states:

    Occurs after an instance of the form region is created but before the form region appears. 
    You can try to use a timer for waiting a bit when the form is displayed. Or you can try handling the Activate event of the Inspector class. Also please take a look at the NewInspector event of the Inspectors class.

    Wednesday, December 11, 2013 8:47 AM
  • Thank, again, for your reply Eugene.  We finished some tests, and it seems to me Activate() is called before the form is visible.  We placed Debug.Print() calls in various functions, and this is what we find:

    ThisAddIn_Startup()
    Inspectors_NewInspector() IPM.MyMessageClass
    FormRegion1Factory_FormRegionInitializing()
    NewInspector_Activate()
    FormRegion1_FormRegionShowing()
    FormRegion1_FormRegionClosed()

    We're back where we started.  How do we set the focus to the first control of the form when it's displayed?  The Window or Inspector in the foreground has the focus, and pressing the Tab key activates the form and we see the blinking carat in the first control.  But can we not do this automatically for the user?

    Thursday, December 12, 2013 8:57 PM
  • Hello Peter,

    Unfortunately the Outlook Object Model doesn't provide any event or method to get the job done (i.e. there is no trivial way for doing this). 

    As a workaround, you can try to use a timer and in the timer's tick event set focus (a form should be displayed to a user at that time) to the textbox.  

    Friday, December 13, 2013 8:20 AM