On Aug 25, 12:49 pm, Glen <Glen_Oss...@Ossman-CG.net> wrote:
> In map basic open diag box how can I open more than 1 file at a time.
>
> or does any have an example of window lib call to multi file diag box?
>
Contrary to what someone else said, you can in fact do this by
calling .Net. Yes, MB can only call static .Net methods, but that
doesn't make the task impossible, it just means it's more work:
You would write a class in .Net, with a static method that shows
the file open dialog. Then call that static method from MB.
If you need to you can store information in static member
variables, between method calls.
Here is a demo C# class that I wrote, with 2 methods designed to be
called from MapBasic. The same thing could be done from VB.Net.
(I'm sure this code could be made more elegant, but my
lunch hour only lasts so long...)
// Begin C# syntax example /////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace MBtoDotNet
{
// Class of methods designed to be callable from MapBasic (i.e.
Static)
public class MBWrappers
{
// Use a static var to store the list of selected filenames
static string[] m_files;
// Call this method to let the user choose multiple files.
// You might want to add more arguments to specify more
// options e.g. types of files to select.
// Method returns the number of files selected -- might be
zero.
public static int ChooseMultiFiles(string
strInitialDirectory)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = true;
if (strInitialDirectory != null &&
strInitialDirectory.Length > 0)
{
dlg.InitialDirectory = strInitialDirectory;
}
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.Cancel) {
return 0;
}
m_files = dlg.FileNames;
if (m_files == null || m_files.Length == 0)
{
return 0;
}
// For debugging purposes, show the selected filenames
string s = "";
for (int i = 0; i < m_files.Length; i++)
{
s += m_files[i] + " ";
}
MessageBox.Show(".Net Debug message: You selected files: "
+ s);
return m_files.Length;
}
// Call this method AFTER calling ChooseMultiFiles,
// to retrieve the list of filenames the user selected.
// Note that you will need to create a MapBasic string array,
// and size it manually, before calling this method.
public static void GetChosenFileNames(string[] names)
{
if (m_files == null || m_files.Length == 0 ||
names == null || names.Length != m_files.Length)
{
MessageBox.Show("GetChosenFileNames: cannot proceed.
Check your array size.");
return;
}
for (int i = 0; i < m_files.Length; i++)
{
names[i] = m_files[i];
}
}
}
}
// End C# syntax example ////////////
And here is a MapBasic program that uses those two .Net methods
to display a multi-selection File Open dialog:
' Begin .MB syntax example
' Demonstration of how to call .Net to display a File Open dialog
' that allows the user to choose multiple files.
Declare Method ChooseMultiFiles Class "MBtoDotNet.MBWrappers"
Lib "MBDotNetWrappers.dll" (byval str As String) As Integer
Declare Method GetChosenFileNames Class "MBtoDotNet.MBWrappers"
Lib "MBDotNetWrappers.dll" (str() As String)
Declare Sub Main
Sub Main
Dim str, msg As String
Dim filenames(1) As String
Dim counter, i As Integer
str = "C:\"
' Call .net method to let user choose multiple files
counter = ChooseMultiFiles(str)
If counter = 0 Then
Note "In MB: No files selected"
Else
' Resize the MB array to match the number of files selected
Redim filenames(counter)
' Call .net method to retrieve all the selected files
Call GetChosenFileNames(filenames)
' For debugging purposes, display the files
msg = "MB debug message: You selected files: "
For i = 1 to Ubound(filenames)
msg = msg + filenames(i) + " "
Next
Note msg
End If
End Sub
' End .MB syntax example
Obviously, this isn't a one-line solution, but, it works.
And there's so much that you can do in .Net, I think
it's worth while for any MB code author to become
familiar with calling .Net methods from MB. There
may be a learning curve but it's worthwhile.
Caveat: Although I am a PB employee, this code has
no warrantees or guarantees -- this is just a syntax
example, to demonstrate how to call from MB to .Net.
If you want to use this code in your app you'll need to
modify it and test it yourself.
With some additional polishing, this might be a good
example to include in the MapBasic documentation,
as I'm sure others have also wanted to do similar tasks....
- Dave