MB open diag box how can I open more than 1 file at a time

162 views
Skip to first unread message

Glen

unread,
Aug 25, 2009, 12:49:56 PM8/25/09
to MapInfo-L
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?

G

Gentreau

unread,
Aug 25, 2009, 2:10:57 PM8/25/09
to mapi...@googlegroups.com

You can't do it in MapBasic, single file only.

Glen

unread,
Aug 25, 2009, 5:47:34 PM8/25/09
to MapInfo-L
I HATE map basic!

Jan S

unread,
Aug 25, 2009, 7:01:58 PM8/25/09
to MapInfo-L
Glen

You CAN program MapBasic to open as many FILES as you need/want, but
you can only open one (1) DIALOG, as dialog is modal (the open dialog
has 'focus' until it is closed).

If you want the user to be able to specify multiple files to open, you
can create multiple text boxes Control EditText or you could also use
a Control MultiListBox to allow the user to select from a list the
files to open.

InterRob

unread,
Aug 25, 2009, 9:11:07 PM8/25/09
to MapInfo-L
You can compose forms using some other win32 delopment environment
(e.g. Visual C++ or Delphi), building a DLL. dot-NET won't help you,
as only static functions can be called from MapInfo...

However; event-callbacks to MapInfo/MapBasic are quite a challenge...
Does any body have an idea on that last bit?


Rob

Driver, Greg 9434

unread,
Aug 26, 2009, 3:47:49 AM8/26/09
to mapi...@googlegroups.com
You can't do it using the standard MapBasic function FileOpenDlg(). But, you can do it using various other calls and I've attached some code/mbx to show my interpretation of how. The code uses examples from Jacques Paris's site -
http://www.paris-pc-gis.com/mb_r/fs/fs_start_a.htm plus other code that's been posted on MI-L over the years (sorry I can't reference everyone).

Okay, it's not very Windows-like and it's a bit cluncky, but hey you can't have everything.

Happy MapBasicing....

Greg Driver

NOT PROTECTIVELY MARKED


-----Original Message-----
From: mapi...@googlegroups.com [mailto:mapi...@googlegroups.com] On Behalf Of Glen
Sent: 25 August 2009 22:48
To: MapInfo-L
Subject: [MI-L] Re: MB open diag box how can I open more than 1 file at a time



I HATE map basic!


Information about this E-mail
This email and any files or attachments with it are intended solely for the use of the individual(s) or organisation(s) to whom it is addressed.
It may contain information that is confidential or subject to legal and/or professional privilege.
If you have received this email in error please notify the sender and delete it including any files or attachments from your e-mail account or computer.
Any opinions expressed in this email are those of the individual and not necessarily those of Surrey Police.
Surrey Police monitor incoming and outgoing e-mail.
alt-fileopen.MBX
alt-fileopen.MB

smart...@gmail.com

unread,
Aug 26, 2009, 12:30:47 PM8/26/09
to MapInfo-L

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


Uffe Kousgaard

unread,
Aug 26, 2009, 2:25:20 PM8/26/09
to mapi...@googlegroups.com
It is in fact the same thing you do when writing a DLL using Delphi for
instance: Wrapping a class instance inside a simple function call. And
global variables could be used for storing information between method calls.

Very much the same story.

Obviously full class/object support would be nice in mapbasic, but that
is probably beyond the mapbasic syntax. Would require an almost new
mapbasic.

Regards
Uffe Kousgaard

Glen

unread,
Aug 26, 2009, 6:12:00 PM8/26/09
to MapInfo-L
I converted this to VB.Net and import system.windows.forms is not
found?
anyone have an idea????

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports 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
Shared m_files As String()


' 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 Shared Function ChooseMultiFiles(ByVal
strInitialDirectory As String) As Integer
Dim dlg As New OpenFileDialog()
dlg.Multiselect = True
If strInitialDirectory IsNot Nothing AndAlso
strInitialDirectory.Length > 0 Then
dlg.InitialDirectory = strInitialDirectory
End If
Dim dr As DialogResult = dlg.ShowDialog()
If dr = DialogResult.Cancel Then
Return 0
End If
m_files = dlg.FileNames
If m_files Is Nothing OrElse m_files.Length = 0 Then
Return 0
End If


' For debugging purposes, show the selected filenames
Dim s As String = ""
For i As Integer = 0 To m_files.Length - 1
s += m_files(i) & " "
Next
MsgBox(".Net Debug message: You selected files: " & s)


Return m_files.Length
End Function


' 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 Shared Sub GetChosenFileNames(ByVal names As String())
If m_files Is Nothing OrElse m_files.Length = 0 OrElse
names Is Nothing OrElse names.Length <> m_files.Length Then
MsgBox("GetChosenFileNames: cannot proceed. Check your
array size.")
Exit Sub
End If


For i As Integer = 0 To m_files.Length - 1
names(i) = m_files(i)
Next
End Sub


End Class


End Namespace

smart...@gmail.com

unread,
Aug 26, 2009, 6:50:39 PM8/26/09
to MapInfo-L
My project's "References" includes a reference to
System.Windows.Forms.dll
-- maybe you just need to add that Reference to your project?

InterRob

unread,
Aug 27, 2009, 3:51:03 AM8/27/09
to MapInfo-L
Hi Dave,

Thanks a lot for you posting. I was the one who said you couldn't do
this... This was because I presumed that for every call to a class
member function, the assembly would be loaded... Apparently, this is
not the case.

Greetz, Rob

On 26 aug, 18:30, "smartgro...@gmail.com" <smartgro...@gmail.com>
wrote:

Glen

unread,
Aug 27, 2009, 3:43:19 PM8/27/09
to MapInfo-L
mapinfo10 message >>> could not find class

Dll is in dir with mbx

any other hints????

smart...@gmail.com

unread,
Aug 28, 2009, 8:36:21 AM8/28/09
to MapInfo-L
Two things come to mind:

First, it looks like the converter gave you a
ByVal argument in the GetChosenFileNames function.
Try changing that in your VB to by-reference, not by value.
MapInfo won't consider the function to be a match
unless the argument list is an exact match of what the
MB "Declare" statement specified.

If it isn't that, maybe it's a namespace issue.
You said you converted the sample code to VB, correct?
I think VB handles namespaces differently than C#.
If memory serves, I think VB allows you to declare your
namespaces explicitly in the source code, like C#,
but you don't have to in VB because the VB project has a
project property that declares a default namespace or somesuch.

SO since you converted C# code and ended up with an explicit
namespace declaration in your .VB code, maybe you need to
clear the VB project default namespace? This is discussed
briefly at least in the MB User Guide.

Glen

unread,
Aug 28, 2009, 8:45:41 AM8/28/09
to MapInfo-L
"VB because the VB project has a
project property that declares a default namespace or somesuch."


you are correct, you must leave it blank

Peter Horsbøll Møller

unread,
Aug 31, 2009, 7:25:42 AM8/31/09
to mapi...@googlegroups.com
One obvious thing that you might want to extent this method with is the filter/file types.

///============================================================
///C#
   public static int OpenFilesDlg(string startFolder, string fileTypes)
   {

       OpenFileDialog dlg = new OpenFileDialog();
       dlg.Multiselect = true;
       if (startFolder != null && startFolder.Length > 0)
       {
           dlg.InitialDirectory = startFolder;
       }
       if (fileTypes != null && fileTypes.Length > 0)
       {
           dlg.Filter = fileTypes;

       }

       DialogResult dr = dlg.ShowDialog();
       if (dr == DialogResult.Cancel)
       {
           return 0;
       }
       mselectedFiles = dlg.FileNames;
       if (mselectedFiles == null || mselectedFiles.Length == 0)
       {
           return 0;
       }

       return mselectedFiles.Length;
   }
///============================================================

The filter/file types is a string of file types that the user can choose from.
The string could look like this:

Allows the user only to pick one file type (*.txt). The text before | is shown in the drop down list. The value after is the filter.
"Text Files (*.txt)|*.txt"

Here the user can choose between two types. Note that the two types also is separated by a |

"Tables (*.tab)|*.tab|Workspaces (*.wor)|*.wor"

And here the user can select one item in the drop down list that will show several file types here *.tab and *.wor)
"MapInfo files (*.tab,*.wor)|*.tab;*.wor"


Peter Horsbøll Møller
Pitney Bowes Business Insight - MapInfo


2009/8/28 Glen <Glen_...@ossman-cg.net>

Gentreau

unread,
Nov 27, 2009, 4:43:47 PM11/27/09
to mapi...@googlegroups.com

You can do it in a Windows stylee too, but you need to be able to write a
dll which calls the Windows API.
There is a windows function called GetOpenFileName() which will return a
single filepath or a delimited list
of filenames, depending upon whether the user selected one or many.

I have been round this loop myself, and had to write my own dll to make it
work, but now it looks just like Windaz :)

Hth
Gentreau.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"MapInfo-L" group.
To post to this group, send email to mapi...@googlegroups.com
To unsubscribe from this group, send email to
mapinfo-l+...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mapinfo-l?hl=en
-~----------~----~----~----~------~----~------~--~---


Information about this E-mail
This email and any files or attachments with it are intended solely for the
use of the individual(s) or organisation(s) to whom it is addressed.
It may contain information that is confidential or subject to legal and/or
professional privilege.
If you have received this email in error please notify the sender and delete
it including any files or attachments from your e-mail account or computer.
Any opinions expressed in this email are those of the individual and not
necessarily those of Surrey Police.
Surrey Police monitor incoming and outgoing e-mail.

--

You received this message because you are subscribed to the Google Groups
"MapInfo-L" group.
To post to this group, send email to mapi...@googlegroups.com.
To unsubscribe from this group, send email to
mapinfo-l+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/mapinfo-l?hl=en.



Reply all
Reply to author
Forward
0 new messages