protected override void OnTouchDown(TouchEventArgs e) #endif { int x = 0; int y = 0; #if MF_FRAMEWORK_VERSION_V3_0 x = e.X; y = e.Y; #else e.GetPosition(this, 0, out x, out y); #endif try { // Figure out which section of the screen was clicked. if (y <= cy1) { // The button area was clicked. if (x <= cx1) { // The New File button was clicked. if (Directory.GetCurrentDirectory() == "\\") { Debug.Print("Cannot create a file at \\"); return; } // Get the next file name, by looping until the file // doesn't exist. int index = 0; string name = "File_0.txt"; while (File.Exists(name)) name = "File_" + (++index).ToString() + ".txt"; // Create the file using the standard .NET FileStream. FileStream file = new FileStream(name, FileMode.Create); // Write some dummy data to the file. for (int i = 0; i < (index + 5) * 2; i++) file.WriteByte((byte)i); // Close the file. file.Close(); // Refresh the list and invalidate. myApplication.mainWindow.RefreshList(); Invalidate(); } else if (x <= cx2) { // The New Directory button was clicked. if (Directory.GetCurrentDirectory() == "\\") { Debug.Print("Cannot create a directory at \\"); return; } // Get the next directory name, by looping until the // directory doesn't exist. int index = 0; string name = "Directory_0"; while (Directory.Exists(name)) name = "Directory_" + (++index).ToString(); // Create the directory. Directory.CreateDirectory(name); // Refresh the list, then re-draw the list. myApplication.mainWindow.RefreshList(); Invalidate(); } else if (x <= cx3) { // The Move button was clicked. if (Directory.GetCurrentDirectory() == "\\") { Debug.Print("Cannot move to or from \\"); return; } // If an item is selected, "move" it. if (_selectedItem != null) { // Get the sub-item that has the name. ListViewSubItem subItem = (ListViewSubItem)_selectedItem.SubItems[0]; if (subItem != null) { // If the name starts with [ and ends with ] // then it is a directory. This is only because // we put the [ and ] on our directory names. // There is nothing in the file system that // requires the [ and ]. if (subItem.Text[0] == '[' && subItem.Text[subItem.Text.Length - 1] == ']') { // Remove the [ and ] characters. string name = subItem.Text.Substring(1, subItem.Text.Length - 2); // Make sure the directory exists. if (Directory.Exists(name)) { // Move the directory to the same name + // .moved. Directory.Move(name, name + ".moved"); // Update the local name variable. name += ".moved"; // Update the name text. ((ListViewSubItem)_selectedItem.SubItems[0]).Text = '[' + name + ']'; // Get the index in Items of the // selected list view item. int index = Items.IndexOf(_selectedItem); // Remove the item, and then add the // item back in. Items.Remove(_selectedItem); Items.Insert(index, _selectedItem); // Re-draw the list. Invalidate(); } } else if (File.Exists(subItem.Text)) { // Without the [ and ] it is a file. // Move the file to the same name + .moved. File.Move(subItem.Text, subItem.Text + ".moved"); // Update the subitem text. subItem.Text += ".moved"; // Get the index in Items of the selected // list view item. int index = Items.IndexOf(_selectedItem); // Remove the item, then add the item back // in. Items.Remove(_selectedItem); Items.Insert(index, _selectedItem); // Re-draw the list. Invalidate(); } } } } else if (x <= cx4) { // The Delete button was clicked. if (Directory.GetCurrentDirectory() == "\\") { Debug.Print("Cannot delete from \\"); return; } // If an item is selected, delete the item. if (_selectedItem != null) { // Get the sub-item that has the name. ListViewSubItem subItem = (ListViewSubItem)_selectedItem.SubItems[0]; if (subItem != null) { // If the name starts with [ and ends with ], // then it is a directory. This is only because // we put the [ and ] on our directory names. // There is nothing in the file system that // requires the [ and ]. if (subItem.Text[0] == '[' && subItem.Text[subItem.Text.Length - 1] == ']') { // Remove the [ and ]. string name = subItem.Text.Substring(1, subItem.Text.Length - 2); // Make sure the directory exists. if (Directory.Exists(name)) { // Delete the directory. Directory.Delete(name); // Remove it from the list view. Items.Remove(_selectedItem); // Reset the selected item member. _selectedItem = null; } } else if (File.Exists(subItem.Text)) { // Without the [ and ], it is a file. // Delete the file. File.Delete(subItem.Text); // Remove it from the list view. Items.Remove(_selectedItem); // Reset the selected item member. _selectedItem = null; } // Re-draw the list view. Invalidate(); } } } else if (x <= cx5) { // The Format button was clicked. // Always go back to the root directory before // formatting. Directory.SetCurrentDirectory("\\"); // Format the volume and call it ROOT. Microsoft.SPOT.IO.VolumeInfo volInfo = new VolumeInfo("ROOT"); volInfo.Format(0); // Refresh the list, then re-draw the list. myApplication.mainWindow.RefreshList(); Invalidate(); } } else if (y <= cy2) { // Column. } else if (y >= cy3) { // The horizontal scrollbar was clicked. OnHorizontalScrollStylusDown(x); } else { if (x >= cx5) { // Vertical Scroll OnVerticalScrollStylusDown(y); } else { // Main section. // Calculate which item was clicked. int itemNumber = ((y - _columnHeaderHeight) + _sy) / _itemHeight; // If an item was clicked... if (itemNumber >= 0 && itemNumber < Items.Count) { // See if this item is already selected. if (_selectedItem == (ListViewItem)Items[itemNumber]) { // See if this is a directory. if (_selectedItem.SubItems.Count > 0) { string directoryName = ((ListViewSubItem)_selectedItem.SubItems[0]).Text; directoryName = directoryName.Substring(1, directoryName.Length - 2); // Check for special ".." name if (directoryName == "..") { directoryName = Directory.GetCurrentDirectory(); directoryName = Path.GetDirectoryName(directoryName); // directoryName.Substring(0, directoryName.LastIndexOf('\\')); } // If the directory exists... if (Directory.Exists(directoryName)) { // Set the current directory. Directory.SetCurrentDirectory( directoryName); // Refresh the list. myApplication.mainWindow.RefreshList(); } } } else // No item is selected, so select this one. _selectedItem = (ListViewItem)Items[itemNumber]; } else // No item is selected and we didn't click on one // either. _selectedItem = null; // Refresh the list view Invalidate(); } } } catch (IOException ex) { Debug.Print(ex.ToString()); } }
private void OnFormat(object sender, TouchEventArgs e) { if (Directory.GetCurrentDirectory() == "\\") { // Always go back to the root directory before formatting. if (_listView.SelectedItem != null) { Directory.SetCurrentDirectory("\\"); // Get the name of the selected volume. string volume = ((ListViewSubItem)_listView.SelectedItem.SubItems[0]).Text; volume = volume.Trim('[', ']', '\\'); // Format the selected volume. Microsoft.SPOT.IO.VolumeInfo volInfo = new VolumeInfo(volume); if (volInfo.FileSystem == null) { volInfo.Format("FAT", 0, volume + "FS", true); } else { volInfo.Format(0); } // Refresh the list, then re-draw the list. RefreshList(); _listView.Invalidate(); } } else { Debug.Print("Format can only be performed on a root directory."); } }