Question

Im using GTK# FileChooserDialog widget.The problem is that even though the widget is named file chooser you can select folders with it and it returns folder names also. Is there any way by which i can restrict it to choosing only files? I have checked almost class all properties i could not find any.

Was it helpful?

Solution

you can restrict the action by defining its Action-Property in the constructor

private void OpenOFD()
{
    Gtk.FileChooserDialog filechooser =
        new Gtk.FileChooserDialog("Choose the file to open",
            this,
            FileChooserAction.Open,
            "Cancel",ResponseType.Cancel,
            "Open",ResponseType.Accept);

    if (filechooser.Run() == (int)ResponseType.Accept) 
    {
        System.IO.FileStream file = System.IO.File.OpenRead(filechooser.Filename);
        file.Close();
    }

    filechooser.Destroy();
}

There are 4 FolderChooserActions:

  1. CreateFolder: Indicates a mode for creating a new folder. The chooser will let the user name an existing or new folder
  2. Open: Will only pick an existing file
  3. Save: Will pick an existing file or type in a new filename
  4. SelectFolder: Pick an existring folder

OTHER TIPS

According to the documentation, that behavior depends on the Action property:

  • If it is set to FileChooserAction.Open or FileChooserAction.Save, only files can selected.
  • If it is set to FileChooserAction.SelectFolder or FileChooserAction.CreateFolder, only folders can be selected.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top