Next add the code in Listing to the User Control class You can copy the code from Chapter but note that some changes will be necessary

Listing 14.30. Code for user control

Private Sub UserControl1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load GetCurrentStyles() End Sub

Private Sub GetCurrentStyles()

' Add style names from document to the list box. Dim CurrentStyle As Excel.Style

For Each CurrentStyle In Globals.ThisAddIn.Application _ .ActiveWorkbook.Styles

Me.ListBox1.Font = New Font("Times New Roman", 14, _

FontStyle.Regular) Me.ListBox1.Items.Add(CurrentStyle.Name)

Next End Sub

Private Sub ListBox1_MouseClick(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles ListBox1.MouseClick

' Set the active cell to the style selected in the list box, ' and then call ResetStyle to assign style name and ' formatting to label.

Globals.ThisAddIn.Application.ActiveCell.Style = _

Me.ListBox1.SelectedItem ResetStyle() End Sub

Private Sub ResetStyle()

' Retrieve format information from Selected style.

Dim CurrentStyle As Excel.Style = _

Globals.ThisAddIn.Application.ActiveWorkbook. _ Styles(Me.ListBox1.SelectedItem.ToString) Dim FontName As String = CurrentStyle.Font.Name Dim FontSize As Single = CurrentStyle.Font.Size Dim FontBold As Boolean = CurrentStyle.Font.Bold Dim FontItalic As Boolean = CurrentStyle.Font.Italic Dim FontAttribute As FontStyle = FontStyle.Regular

If FontBold = True Then

FontAttribute = FontStyle.Bold End If

If FontItalic = True Then

FontAttribute = FontStyle.Italic End If

If CurrentStyle.Font.Underline = _

Excel.XlUnderlineStyle.xlUnderlineStyleSingle Then

FontAttribute = FontStyle.Underline

End If

' Set the text of the label to the style and formatting ' selected in the list box.

Me.SelectionFont.Text = Me.ListBox1.SelectedItem.ToString() Me.SelectionFont.Font = New Font(FontName, _ FontSize, FontAttribute)

End Sub

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

Me.ListBox1.Items.Clear() GetCurrentStyles()

0 0

Post a comment