Beispiel #1
0
        private void dgvFiles_CellValidating( object sender, DataGridViewCellValidatingEventArgs e )
        {
            if( !editing ) return;

              int afi = (int)dgvFiles.Rows[e.RowIndex].Tag;
              string newFilename = (string)e.FormattedValue;
              string prevFilename = activeFiles[afi].Name;

              // cancel if new value empty or unchanged

              if( string.IsNullOrEmpty( newFilename ) || newFilename == activeFiles[afi].Name )
              {
            dgvFiles.CancelEdit();
            return;
              }

              // get new name/path

              if( itmOptionsPreserveExt.Checked )
            newFilename += activeFiles[afi].Extension;
              string newFullpath = Path.Combine( activePath, newFilename );

              // validate

              string errorMessage = ValidateFilename( newFilename, false );
              if( errorMessage != null )
              {
            MessageBox.Show( errorMessage, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error );
            dgvFiles.CancelEdit();
            return;
              }

              Regex regex = new Regex( "^[ .]" );
              if( regex.IsMatch( newFilename ) && !regex.IsMatch( activeFiles[afi].Filename ) )  // now starts with [ .]
              {
            errorMessage = "Dieser " + strFilename + " beginnt mit einem Leerzeichen oder Punkt. Das ist zwar technisch möglich, Windows\n"
                     + "lässt dies aber normalerweise nicht zu, weil es zu Problemen mit anderen Programmen führen könnte.\n"
                     + "\n"
                     + "Sie Sie sicher, dass Sie fortfahren möchten?";

            if( MessageBox.Show( errorMessage, "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning )
            == DialogResult.Cancel )
            {
              dgvFiles.CancelEdit();
              return;
            }
              }

              // rename

              try
              {
            if( RenameFolders )
              Directory.Move( activeFiles[afi].Fullpath, newFullpath );
            else
              File.Move( activeFiles[afi].Fullpath, newFullpath );
              }
              catch( Exception exception )
              {
            MessageBox.Show( exception.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error );
            dgvFiles.CancelEdit();
            return;
              }

              // update icon (if file)

              FileInfo fi = new FileInfo( newFullpath );

              if( !RenameFolders && fi.Extension != activeFiles[afi].Extension )
              {
            try  // add image (keyed by extension)
            {
              string ext = fi.Extension.ToLower();
              if( !icons.ContainsKey( ext ) )
            icons.Add( ext, ExtractIcons.GetIcon( newFullpath, false ) );

              dgvFiles.Rows[e.RowIndex].Cells[0].Value = icons[ext];
            }
            catch  // default = no image
            {
              dgvFiles.Rows[e.RowIndex].Cells[0].Value = new Bitmap( 1, 1 );
            }
              }

              // update RRItem

              if( RenameFolders )
            activeFiles[afi] = new RRItem( new DirectoryInfo( fi.FullName ), activeFiles[afi].Hidden, activeFiles[afi].PreserveExt );
              else
            activeFiles[afi] = new RRItem( fi, activeFiles[afi].Hidden, activeFiles[afi].PreserveExt );

              // update folder tree (if folder)

              if( RenameFolders )
              {
            foreach( TreeNode node in tvwFolders.SelectedNode.Nodes )
            {
              if( node.Text == prevFilename )
              {
            node.Text = activeFiles[afi].Name;
            Shell32.Folder folder = new Shell32.ShellClass().NameSpace( activePath );
            node.Tag = folder.ParseName( activeFiles[afi].Filename );
            break;
              }
            }
              }

              // workaround for exception when ending edit by pressing ENTER in last cell

              dgvFiles.CommitEdit( DataGridViewDataErrorContexts.Commit );
              e.Cancel = true;

              // update preview

              editing = false;  // prevent recursion: dgvFiles.Sort() in UpdatePreview() causes dgvFiles.CellValidating
              UpdatePreview();
        }