Posts from July 2008

Jul
21
2008

C#: File Dialog for the Compact Framework



Posted in Programming

When Microsoft were deciding what to include in the .NET Compact Framework, they decided to restrict the OpenFileDialog and SaveFileDialog to the My Documents directory. There are plenty of reasons you’d need to choose a file outside of My Documents, so I coded a new file chooser from other components.

FileDialog for .NET Compact Framework

The dialog is just a form with a ComboBox which shows the current directory and all of it’s parents, a ListView which shows the files and directories in the current directory, and a text box to show the selected file.

Instantiate it with either the default constructor or a directory and file to show on startup.

FileInfo dbFile = new FileInfo("\\Storage Card\\Somefile.txt");
Lime49.OpenFileDialog dlg = new Lime49.OpenFileDialog(dbFile.DirectoryName, dbFile.Name);
dlg.Filter = "*.txt";
dlg.ShowDirectory(dbFile.DirectoryName);
dlg.ShowDialog();
MessageBox.Show(dlg.SelectedFile);

The selected file is available through the SelectedFile property, and the filter doesn’t work the same way as standard FileDialogs, it uses a standard wildcard pattern to list files.

Download Lime49.OpenFileDialog
Also published on CodeProject

Jul
19
2008

C#: Drag and Drop – Part 2



Posted in Programming

Last week I posted about drag and drop in C#. The post covered pre-built controls (TreeView and ListView). There are a few things the post didn't cover, such as user controls and anything which doesn't raise the ItemDrag event.

To be able to drag a user control, the control has to call the DoDragDrop method. The logical way to do this is by tracking the state of the left mouse button. If the left button is pressed and the mouse moves, start the drag/drop.

private bool isDragging = false;
...
private void userControl_MouseDown(object sender, MouseEventArgs e) {
    this.isDragging = true;
};
private void userControl_MouseUp(object sender, MouseEventArgs e) {
    this.isDragging = false;
};
private void userControl_MouseLeave(object sender, EventArgs e) {
    isDragging = false;
};
private void userControl_MouseMove(object sender, MouseEventArgs e) {
    if(isDragging) {
        DoDragDrop(userControl, DragDropEffects.Move);
    }
};

Read more »

Jul
7
2008

C#: Drag and Drop – Part 1



Posted in Programming

Drag and drop is one of the things that seems easier in C# than Java. In OnTime, the project I'm currently working on, I've used it in several places to make the UI easier to use.

The first example I want to cover is dragging between a ListView and TreeView. This could be used to make re-arranging things easier. Eg: If you're displaying categories in a tree and items in a list.

Read more »