You are on page 1of 2

MapBasic: using a method from a .

NET assembly (OpenFileDialog)


In this post I will show an example on using a method from a .NET assembly in yo
ur MapBasic application. MapBasic programs can call functions and subroutines wr
itten using Microsoft's .NET development platform. You can write code in languag
es such as C# and VB.NET.
Some tasks that are difficult or even impossible in MapBasic are relatively easy
to do in .NET. So you may find it useful to write some parts of your applicatio
n using .NET, and then call those routines from your MapBasic application.
Please note: this example does not require you to have Visual Studio installed;
just a simple text editor and the presence of the .NET framework on your machine
will do (I'll explain this in further detail below.)
In this example we will work towards a file dialog that allows the user to open
multiple files. (The MapBasic function FileOpenDlg( ) only allows you to select
a single file.)
We will call our method OpenMultipleFilesDlg. And this is the C# code of the .NE
T assembly (*.dll):
using System;
using System.IO;
using System.Windows.Forms;
namespace MBExtensions
{
class MBExtensionsDlgs
{
public static string OpenMultipleFilesDlg(string sPath, string sFileType
, string sPrompt)
{
string mbSelectedFiles;
OpenFileDialog ofd = new OpenFileDialog();
mbSelectedFiles = "";

ofd.InitialDirectory = sPath;
ofd.Filter = sFileType;
ofd.Title = sPrompt;
ofd.Multiselect = true;
if(ofd.ShowDialog() == DialogResult.OK)
{
foreach (String file in ofd.FileNames)
{
if (mbSelectedFiles == "")
{
mbSelectedFiles = file;
}
else
{
mbSelectedFiles = mbSelectedFiles + ";" + file;
}
}
}
return mbSelectedFiles;
}
}
}
To create the .NET assembly (*.dll) you have to compile the file MBExtensionsDlg
s.cs, using csc, the Visual C# compiler which is part of the .NET Framework (and
the .NET framework 3.5 and/or 4.0 is present on your machine when you are runni
ng MapInfo Professional 9.5 or higher.)
Open your DOS box and run: csc /t:library MBExtensionsDlgs.cs
(To be able to call the compiler from any location, you should add the location
(folder) of the csc.exe to your Windows path.)
The result is a Dynamic Link Library (*.dll): MBExtensionsDlgs.dll.
Our method OpenMultipleFilesDlg will show an OpenFileDialog, allowing the user t
o select multiple files.
The return value is a string containing one or more file names separated by a se
micolon (or an empty string if the user clicks Cancel). Each file name includes
both the file path and the extension. Please note: the dialog does not actually
open any files; it will just return a string which can be used for further proce
ssing.
Now you have to declare the method in your MapBasic application:
Declare Method OpenMultipleFilesDlg
Class "MBExtensions.MBExtensionsDlgs"
Lib "MBExtensionsDlgs.dll"
(ByVal sPath As String 'sets the InitialDirectory displayed by the file d
ialog box
, ByVal sFileType As String 'filters file types
, ByVal sPrompt As String 'sets the title of the dialog box
) As String
And nouw you can use the method like in the sample below:
Dim sSelectedFiles, sPath, sFileType, sPrompt As String
sFileType = "MapInfo (*.tab)|*.tab" 'for TAB files only
sPrompt = "Open one or more files"
sSelectedFiles = OpenMultipleFilesDlg(sPath, sFileType, sPrompt)
Note sSelectedFiles
The return value of course fully depends on the files chosen by the user. But in
the example above the content of the variable sSelectedFiles might look like so
mething like this:
C:\MapInfo Data\Table1.TAB;C:\MapInfo Data\Table2.TAB;C:\MapInfo Data\Table3.TAB
To be able to process the files individually in your MapBasic application, you w
ill have to split the return string into individual file names. How? Please see
my recent post describing a bespoke function to split a string in MapBasic.
For the C# code, see this file: MBExtensionsDlgs.cs
For a sample MapBasic application using the method OpenMultipleFilesDlg, see
this file: CallOpenFileDialog.mb
Happy coding :-)

You might also like