views:

87

answers:

3

I have so many issues with my project, i really don't know where to begin. First off, i get an error "an object reference is required for non-static field, method or property". It underlines retPath (the line: DriveRecursion_results.DriveRecursion(retPath);). I have no idea how to fix this.

THe other thing i'm still stumped on is how to populate a listview on my Windows Form. What i want is a list of files that need to be renamed (versus a list of all files in my list.)

Can anybody help? I have been struggling miserably with this for several hours now.

Here is my code:

Form1.cs:

namespace FileMigration
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        FolderSelect("Please select:");
    }
    public string FolderSelect(string txtPrompt)
    {
        //Value to be returned
        string result = string.Empty;

        //Now, we want to use the path information to population our folder selection initial location
        string initialCheckoutPathDir = (@"C:\");
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(initialCheckoutPathDir);
        FolderBrowserDialog FolderSelect = new FolderBrowserDialog();
        FolderSelect.SelectedPath = info.FullName;
        FolderSelect.Description = txtPrompt;
        FolderSelect.ShowNewFolderButton = true;

        if (FolderSelect.ShowDialog() == DialogResult.OK)
        {
            string retPath = FolderSelect.SelectedPath;
            if (retPath == null)
            {
                retPath = "";
              }
            DriveRecursion_Results ds = new DriveRecursion_Results();
           ds(retPath);
            result = retPath;
            //Close this form.

        } 
        return result;
    }



   }
}

Here is DriveRecursion_Results.cs:

namespace FileMigration
{
public partial class DriveRecursion_Results : Form
{
    public DriveRecursion_Results()
    {
        InitializeComponent();
    }

    private void fileOutput_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    public void DriveRecursion(string retPath)
    {

       // string[] files = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);

        string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
        string replacement = "";
        Regex regEx = new Regex(pattern);

        string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);
        List<string> filePath = new List<string>();

        foreach (string fileNames in fileDrive)
        {
            if (regEx.IsMatch(fileNames))
            {
                filePath.Add(fileNames);
                //I tried adding my listview (fileOptions) here but I cannot for some reason
            }
        }

        }





        }




    }

ANY help would really be appreciated :( Does anybody have any ideas on how to change my code so it actually works?

+2  A: 

Issue 1: your function is static. If it stops being such, this will work. This is because a static function does not have this hidden 'this' argument - the reference to object it acts upon. So, it can only access static data members, not regular ones.

Pavel Radzivilovsky
i got rid of static -- so both methods now are non-static. however, i still get the "an object reference is required for the non-static field, method, or property) in the same exact place
My guess is that you left the rest of the code alone so you still have DriveRecursion_Result.DriveRecursion(retPath). You now need a specific DriveRecursion_Result to call DriveRecursion on. You will need to have an instance of a DriveRecursion_Result class and call DriveRecursion on that.
Daniel Joseph
+2  A: 

You can't add the items to your listview from that level because the listview is non-static and the method DriveRecursion is static. I would start by changing the DriveRecursion method to be non-static or return a list of file paths.

Jerod Houghtelling
A: 

You are not able to add items to your list view because you are trying to add them from a static method.

Since it is static there is no ListView because there isn't actually a Form to add things to. You will need to make DriveRecursion() not static in order to add things to the ListView.

Additionally, when you make DriveRecursion() not static, you will need a way to let Form1 know which DriveRecursion_Results class to populate.

Another approach you could take is having Form1 return retPath to DriveRecursion_Results.

Edit

Removed irrelevant parts of my original answer

I have copied your code exactly how you posted it. And then made the following changes to FolderSelect() in Form1.cs When I run this code. I can get the second window to pop up, but not the other window to close, because that will cause the application to quit.

Please make sure you have ds.Show() and do at some point call ds.DriveRecursion(retPath)

Modified FolderSelect(string) in Form1.cs:

    private void FolderSelect( string txtPrompt )
    {
        //Value to be returned
        string result = string.Empty;

        //Now, we want to use the path information to population our folder selection initial location
        string initialCheckoutPathDir = ( "C:\\" );
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo( initialCheckoutPathDir );
        FolderBrowserDialog FolderSelect = new FolderBrowserDialog();
        FolderSelect.SelectedPath = info.FullName;
        FolderSelect.Description = txtPrompt;
        FolderSelect.ShowNewFolderButton = true;

        if( FolderSelect.ShowDialog() == DialogResult.OK )
        {
            string retPath = FolderSelect.SelectedPath;
            if( retPath == null )
            {
                retPath = "";
            }
            DriveRecursion_Results ds = new DriveRecursion_Results();
            ds.DriveRecursion( retPath );
            ds.Show();
            result = retPath;
            //Close this form.

        }
        return;
    }
Daniel Joseph
i'm still having trouble getting my initial Form1 to close. I want it so that once the user presses 'ok' on the filebrowserdialog, the form closes and moves onto the next form (the List of files needing to be renamed).
I am guessing that you are encountering the same problem I am when trying to close the form with this.Close() -- that the entire application quits. This is because the form which started the application Form1 is now closed, so the application quits before you get a chance to see DriveRecursion_Results. I will post an edit in a few seconds.
Daniel Joseph
Please see my latest edit. I have copied your code exactly from what you have given. I made some assumptions on the layout in the form designer. But when I run this code I can get the DriveRecursion_Results window to show up. Like I said the first window doesn't close, but that's because closing it causes the application to quit.
Daniel Joseph
ok now i see the listview...however it's empty. I tried putting my listview inside my foreach loop (the last one where it's populating the FileNames) but it's still not displaying any output.
Could you please edit your question with your updated code. I have not had any problems viewing items in my list view.
Daniel Joseph
actually i figured out what the problem was -- it had some property set on it that made it not populate. THANK YOU FOR THE HELP. MUCH appreciated!!!
My pleasure, glad it works!
Daniel Joseph