none
VB Add-in VSTO Creating and using an opening file dialog box RRS feed

  • Question

  • Hi

    I am currently upgrading my knowledge on word add-ins from VBA to VB. So my set up is using NetOffice to create the VSTO project files, and using Visual Studio Express 2012 to code. 

    I have a custom task pane and want to link the button on the custom task pane to the open file dialog box, I then want the ability for the user to select image files. In the custom pane I want to list the file names using lables (creating a new label for each file name).

    I understand how to code the custom pane properties however I do not know how to use open file dialog boxes in VB (I did in VBA).

    If you require me to explain anything further, let me know. Any help on this matter would be much appreciated. Thanks

    • Moved by Quist Zhang Tuesday, February 19, 2013 6:07 AM Relate to WinForm controls
    Sunday, February 17, 2013 12:34 PM

Answers

  • Hi tfitzhardinge,

    Quist has provided the link of OpenFileDialog class. The properties and methods are listed there.

    For example, if you want user to select image files, you can set the Filter property to filter the format of files.

    dialog.Filter = "image files (*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff)|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff"

    Set Multiselect property to True when you want to select multiple files.

    The most common method may be ShowDialog(). To get all the selected files, you can look into FileNames property.

    Here is an example to add labels to display the selected file names. I use a FlowLayoutPanel in the example.

            Dim dialog As New OpenFileDialog()
            dialog.Filter = "image files (*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff)|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff"
            dialog.Multiselect = True
            dialog.RestoreDirectory = True
            FlowLayoutPanel1.Controls.Clear()
            If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                For Each file In dialog.FileNames
                    Dim lbl As New Label
                    With lbl
                        .Text = file
                    End With
                    FlowLayoutPanel1.Controls.Add(lbl)
                Next
            End If
    

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 19, 2013 7:55 AM
    Moderator

All replies

  • Hi

    I am currently upgrading my knowledge on word add-ins from VBA to VB. So my set up is using NetOffice to create the VSTO project files, and using Visual Studio Express 2012 to code.

    I have a custom task pane and want to link the button on the custom task pane to the open file dialog box, I then want the ability for the user to select image files. In the custom pane I want to list the file names using lables (creating a new label for each file name).

    I understand how to code the custom pane properties however I do not know how to use open file dialog boxes in VB (I did in VBA).

    If you require me to explain anything further, let me know. I posted this also in Word for developers however it appears that this forum is more appropriate. Any help on this matter would be much appreciated. Thanks

    Monday, February 18, 2013 1:03 AM
  • Hi Tfitzhardinge,

    Thank you for posting in the MSDN Forum.

    To begin with, I think you'll need add reference to System.Windows.Forms.

    Then you need to add code in the task pane button click event handler.

    The sample code of OpenFileDialog can be seen in this page.

    Hope it helps.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 18, 2013 9:50 AM
  • Thanks for that.

    I believe I can also use the Me keyword use the VBA in the MS Word Object model. Is it possible for the open file dialog box to use the FileDialog object and its properties and methods. I don't believe that using the OpenFileDialog object in VB will provide any advantage to me.

    Feel free to comment

    Tuesday, February 19, 2013 3:52 AM
  • Hi tfitzhardinge,

    Quist has provided the link of OpenFileDialog class. The properties and methods are listed there.

    For example, if you want user to select image files, you can set the Filter property to filter the format of files.

    dialog.Filter = "image files (*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff)|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff"

    Set Multiselect property to True when you want to select multiple files.

    The most common method may be ShowDialog(). To get all the selected files, you can look into FileNames property.

    Here is an example to add labels to display the selected file names. I use a FlowLayoutPanel in the example.

            Dim dialog As New OpenFileDialog()
            dialog.Filter = "image files (*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff)|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff"
            dialog.Multiselect = True
            dialog.RestoreDirectory = True
            FlowLayoutPanel1.Controls.Clear()
            If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                For Each file In dialog.FileNames
                    Dim lbl As New Label
                    With lbl
                        .Text = file
                    End With
                    FlowLayoutPanel1.Controls.Add(lbl)
                Next
            End If
    

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 19, 2013 7:55 AM
    Moderator
  • Chester

    Thanks for that, however I was trying to get a better understanding of how the Me keyword works. That has however help me understand the OpenFileDialog object in VB.

    Correct me if I am wrong, the Me keyword allows VBA (eg the Word model object and their libraries) to be used in visual basic? Having some knowledge in VBA and not too much in VB it made sense for me to implement as much VBA as possible. Only using VB where it is not possible in VBA.

    Thanks again for your help

    Tuesday, February 19, 2013 9:11 PM
  • Another question about user forms.

    Creating a usercontrol in visual studio, is this creating a task pane (eg similar to that of the navigation pane in Word 2010) or in order to create a custom task pane I need to create a windows form. I am aiming to place a list box buttons etc on a pane and make it look similar to the Word 2010's navigation pane. Do I use the usercontrol or am I creating a new form.

    Thanks

    Tuesday, February 19, 2013 9:19 PM
  • Hi tfitzhardinge,

    Me keyword in VB stands for the current instance of the class when code is executed.

    http://msdn.microsoft.com/en-us/library/7e58sbke(v=VS.80).aspx

    http://msdn.microsoft.com/en-us/library/20fy88e0.aspx

    If there is a a UserControl class named UserControl1, a TextBox named TextBox1 in the UserControl.

    Usually it is not necessary to use Me keyword. For example, In the UserControl1 class, Me.TextBox1.Text = "test"  is equals to  TextBox1.Text = "test".

    For some other scenarios, if there is a method Foo(control As UserControl) has a UserControl parameter, you can use Foo(Me).

    In add in class, you can use CustomTaskPanes.Add() method to add UserControl. CustomTaskPanes is a CustomTaskPaneCollection and it is generated automatically when you create a add-in project. You don't need to create a Windows Form. In fact, a UserControl and a Form behaves similarly and they are both derived from ContainerControl.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 20, 2013 9:16 AM
    Moderator
  • Chester

    That's really helpful thanks. So the Me keyword will create an instance of the VBA object class then. Correct me if I am wrong.

    That helps my understanding of the usercontrols. How do I in VB add (not in design mode) a textbox or listbox for example to the usercontrol/taskpane.

    Thursday, February 21, 2013 1:18 AM
  • Hi tfitzhardinge,

    After creating the controls, we can add them to the Controls collection. For example, the following code snippet allow us to add a textbox to a usercontrol:

    Dim tb As New TextBox()
    With tb
        .Location = New Point(10, 10)
        .Text = "new TextBox"
    End With
    Controls.Add(tb)

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 21, 2013 4:27 AM
    Moderator
  • Thanks again Chester

    So you have to declare a new text box ie you are creating an instance for the text box, give it properties and then use the add method to add it to the custom pane.

    With the location is this a relative location to the custom pane, ie if the custom pane moves around by the user the next box will move with the custom pane?

    Thursday, February 21, 2013 4:55 AM
  • Hi tfitzhardinge,

    Yes, the Location is relative to usercontrol. And when you move the custom panel the textbox will also move.

    One more information: a control's Anchor property is Top and Left by default. That means the control is bound to top and left edges of the usercontrol. You can set the property to see different behavior when resizing the usercontrol.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 21, 2013 5:39 AM
    Moderator
  • Chester

    Thanks for adding that.

    The control that I am wanting to add will be a list box so I can use the example code above and change accordingly.

    What I want to do with the list box is allow the user (in runtime) to drag and drop the list entries. Ultimately changing the order (via drag and drop) of this list box.

    Upon researching on the MSDN forum I read up that there is a drag and drop method but was unsure how to code it.

    Do you know how to do this?

    Thursday, February 21, 2013 5:50 AM
  • Hi tfitzhardinge,

    Yes, that can be done. To allow drag & drop, listbox's AllowDrop property should be set to True first.

    There are several events to handle drag & drop features: DragEnter, DragOver, DragLeave and DragDrop. Usually we call DoDragDrop() method in MouseDown event handler to begin drag drop.

        Dim itemIndex As Integer
        Dim dropIndex As Integer
        Dim itemText As String
        Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
            itemIndex = ListBox1.IndexFromPoint(e.X, e.Y)
            If (itemIndex >= 0) Then
                itemText = ListBox1.Items(itemIndex).ToString()
                ListBox1.DoDragDrop(ListBox1.Items(itemIndex), DragDropEffects.Move)
            End If
        End Sub

    DragEnter, DragOver, DragLeave events are used when we are dragging something into, over, out the listbox's bounds respectively. We can use DragOver to remove the item temporarily and detect the index to insert the item. DragLeave event can be used to insert the temporarily deleted item.

    Note that in DragOver event handler, e.X, e.Y is the location in screen coordinates so we need to convert it to client coordinates using PointToClient() method.

    Private Sub ListBox1_DragEnter(sender As Object, e As DragEventArgs) Handles ListBox1.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub
    Private Sub ListBox1_DragOver(sender As Object, e As DragEventArgs) Handles ListBox1.DragOver
        If (e.Effect = DragDropEffects.Move) Then
            ListBox1.Items.Remove(e.Data.GetData(DataFormats.Text))
        End If
        Dim pt = ListBox1.PointToClient(New Point(e.X, e.Y))
        dropIndex = ListBox1.IndexFromPoint(pt.X, pt.Y)
    End Sub
    Private Sub ListBox1_DragLeave(sender As Object, e As EventArgs) Handles ListBox1.DragLeave
        ListBox1.Items.Insert(itemIndex, itemText)
    End Sub

    DragDrop event is used when we drop the item:

    Private Sub ListBox1_DragDrop(sender As Object, e As DragEventArgs) Handles ListBox1.DragDrop
        If (e.Data.GetDataPresent(DataFormats.StringFormat)) Then
            If (dropIndex >= 0 And dropIndex < ListBox1.Items.Count) Then
                ListBox1.Items.Insert(dropIndex, e.Data.GetData(DataFormats.Text))
            Else
                ListBox1.Items.Add(e.Data.GetData(DataFormats.Text))
            End If
        End If
    End Sub
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 21, 2013 8:46 AM
    Moderator
  • Chester

    Thanks for that you have been more than helpful. I will play with the code so I can get a better understanding of it.

    Question, does this allow multiple files to be selected (via the Shift and Ctrl keys), then dragged and dropped to its new location?

    Friday, February 22, 2013 12:00 AM
  • Hi tfitzhardinge,

    Sorry for late response.

    I think it might be not very easy to drag multiple files because when clicking left mouse button, the previous selection will be lost. I changed to use right mouse buttom to move the items.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.drageventargs.keystate.aspx

    Also it seems we have to save the selected items into a list collection since it might be difficult to use e.Data.GetData() method.

        Dim dropIndex As Integer
        Dim itemsList As New List(Of Object)
        Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
            Dim itemIndex = ListBox1.IndexFromPoint(e.X, e.Y)
            If ListBox1.SelectedIndices.Contains(itemIndex) Then
                itemsList.Clear()
                For Each item As String In ListBox1.SelectedItems
                    itemsList.Add(item)
                Next
                Dim itemText = ListBox1.Items(itemIndex).ToString()
                ListBox1.DoDragDrop(itemText, DragDropEffects.Move)
            End If
        End Sub
        Private Sub ListBox1_DragOver(sender As Object, e As DragEventArgs) Handles ListBox1.DragOver
            If (e.KeyState And 2) <> 0 Then
                ' the right mouse button
                e.Effect = DragDropEffects.Move
                For Each item As String In itemsList
                    ListBox1.Items.Remove(item)
                Next
            Else
                e.Effect = DragDropEffects.None
            End If
            Dim pt = ListBox1.PointToClient(New Point(e.X, e.Y))
            dropIndex = ListBox1.IndexFromPoint(pt.X, pt.Y)
        End Sub
        Private Sub ListBox1_DragDrop(sender As Object, e As DragEventArgs) Handles ListBox1.DragDrop
            If e.Effect = DragDropEffects.Move Then
                If (e.Data.GetDataPresent(DataFormats.StringFormat)) Then
                    If (dropIndex >= 0 And dropIndex < ListBox1.Items.Count) Then
                        Dim i = 0
                        For Each item As String In itemsList
                            ListBox1.Items.Insert(dropIndex + i, item)
                            i = i + 1
                        Next
                    Else
                        For Each item As String In itemsList
                            ListBox1.Items.Add(item)
                        Next
                    End If
                End If
            End If
        End Sub

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 26, 2013 6:20 AM
    Moderator
  • Chester thanks for that.

    So does this allow multiple items to be selected via the right mouse click?

    I wanted to create similar functionality to say when you are selecting files in windows explorer. ie using shift and ctrl to allow multi-select. Are you saying it is difficult to do?

    Monday, March 11, 2013 5:42 AM
  • Hi tfitzhardinge,

    In this sample we can use Shift / Ctrl and left mouse button to allow multi select, and drag the items by right mouse button.

    I mean it may be difficult to drag the selected items using left mouse button. All selected items except the current one will be unselected when clicking left mouse button. I think that is the default behavior in WinForms ListBox control.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 12, 2013 5:23 AM
    Moderator
  • Chester thanks for clearing that up.

    With the default behaviour of the list box it will only allow you to drag and drop a single item. Can these behaviours be modified or is it just easier to use another type of object in showing all the listed picture file names and allowing multi-select with a drag and drop behaviour.

    Monday, March 18, 2013 11:46 PM