Using the Actions Pane

You will now convert your sample from showing a modal dialog box to showing an actions pane that will enable the user to update the bookmarks. As described in Chapter 5, the actions pane is docked and does not obstruct users' view of the document as a Windows Form does. This helps the users understand the context of the fields they are adding.

Converting a Windows Form to a User Control

It is super easy to make this conversion. A user control can be placed in other controls or forms. In the example, you will put your user control onto the actions pane. You need to change the type that your form derives from. It's now System.Windows.Forms.Form, and you need to change it to System.Windows.Forms.UserControl.

To do this, click on the Show All Files button at the top of Solution Explorer. Expand the MainForm.vb node in Solution Explorer to see the code-behind files. Open the MainForm.Designer.vb class, and change the type that the form inherits from, as shown in Listing 12.6. Save and close the MainForm.Designer.vb class, and click the Show All Files icon again to hide the code-behind files. Open the MainForm.vb class, and notice that it is now a user control and does not have a title bar.

Listing 12.6. Change the Windows Form to a user control type

<Global.Microsoft.VisualBasic.CompilerServices. _ DesignerGenerated()> _ Partial Class MainForm

Inherits System.Windows.Forms.UserControl

Let's look at how to show the MainForm UserControl inside of the ActionsPane when the document is started.

1. Right-click the ThisDocument.vb file in Solution Explorer, and click View Code.

2. Change the New event handler to the code in Listing 12.7. The only change is the way you open the form. You are creating a new instance and adding it to the actions pane's Controls collection in one line, shown in Listing 12.7 in bold.

Listing 12.7. Displaying a user control in the actions pane when a new document is created

Public Class ThisDocument

Private Sub ThisDocument_New() Handles Me.New Me.SpellingChecked = True Me.GrammarChecked = True Me.ActiveWindow.Caption = _

Me.ActiveWindow.Caption & " - Memo" Me.ActiveWindow.View.ShowFieldCodes = False

' Show the user form.

ActionsPane.Controls.Add(New MainForm())

End Sub

Private Sub ByVal e

End Sub

Private Sub ByVal e

End Sub

End Class

3. Change the MainForm control code to the code in Listing 12.8. The only line that changes is the line Me.Visible = False. This code hides the form after the fields are inserted into the document.

Listing 12.8. Populating bookmark view controls on an OK button Click event Public Class MainForm

Private Sub btnOK_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles OkButton.Click

' Insert the fields into the bookmarks. With Globals.ThisDocument

.ToBookmark.Text = ToTextBox.Text .FromBookmark.Text = FromTextBox.Text .CCBookmark.Text = CCTextBox.Text .REBookmark.Text = ReTextBox.Text .SubjectBookmark.Text = SubjectTextBox.Text End With

'Hide the form. Me.Visible = False

End Sub End Class

ThisDocument_Startup(ByVal sender As Object, As System.EventArgs) Handles Me.Startup

ThisDocument_Shutdown(ByVal sender As Object, As System.EventArgs) Handles Me.Shutdown

4. Run the solution by pressing F5. You now have an actions pane docked to the right side of the document that contains the MainForm user control, as shown in Figure 12.7.

Figure 12.7. VSTO application with actions pane
0 0

Post a comment