I'm still learning C# and this is really annoying me.
I need to modify the contents of a text box on a windows app from a class.
(Using windows app template on VS 2005)
I have created a property on Form1 (SourcePath is a textbox)
Code: ( text )
-
public string SourceDir
-
{
-
get { return SourcePath.Text; }
-
set { SourcePath.Text = value; }
-
}
I then have this method on a class called dirinfo.
Code: ( text )
-
public void dirreader(FolderBrowserDialog folderBrowserDialog, ListBox listBox)
-
{
-
folderBrowserDialog.ShowDialog();
-
-
Form1.SourceDir = folderBrowserDialog.SelectedPath;
-
-
listBox.Items.Clear();
-
-
DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog.SelectedPath);
-
-
DirectoryInfo[] subdirectories = dir.GetDirectories();
-
-
FileInfo[] directoryFiles = dir.GetFiles();
-
-
foreach (FileInfo str in directoryFiles)
-
{ listBox.Items.Add(str); }
-
}
This will not work, I get this
Quote:
Originally Posted by
Error An object reference is required for the nonstatic field, method, or property 'Ras.Form1.SourceDir.get' |
The thing is that I don't know what the instance for Form1, as far as I can tell there does not appear to an instance of Form1, which does not sound right.
I tried creating an instance of Form1, substituted bold line by this
Code: ( text )
-
Form1 newform = new Form1();
-
newform.SourceDir = folderBrowserDialog.SelectedPath;
but unsurprisingly while it compiles, it does not show the path on the textbox.
I know i can make it work by putting
Code: ( text )
-
SourceDir = folderBrowserDialog.SelectedPath;
on the Form1 class, but I would like to learn how to do it without resorting to this.
Any ideas?
TIA