Posts Tagged ‘monobjc’

File Open Dialog in Cocoa

The Objective-C code from Rick’s World,

int i; // Loop counter.

// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];

// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];

// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:YES];

// Display the dialog. If the OK button was pressed,
// process the files.
if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
// Get an array containing the full filenames of all
// files and directories selected.
NSArray* files = [openDlg filenames];

// Loop through all the files and process them.
for( i = 0; i < [files count]; i++ )
{
NSString* fileName = [files objectAtIndex:i];

// Do something with the filename.
}
}

I was actually messing around with Monobjc and wondering how to work with NSOpenPanel, the code above provided a bit of a starting point for me. Here’s my code, which is not a translation of what’s above, but gives an example of how things work with C# and Monobjc,

NSOpenPanel openPanel = new NSOpenPanel();
openPanel.AllowsMultipleSelection =
false;
openPanel.CanChooseFiles =
true;
openPanel.CanChooseDirectories =
false;
openPanel.SetContentSize(
new NSSize(600, 600));

int dialogResult = openPanel.RunModal();
if (dialogResult == NSPanel.NSOKButton)
{
picFileName = openPanel.Filename.ToString();
}

The properties are self-explanatory and the code is simple enough, so I won’t bother with any sort explanation.