Modify textbox from class other than Form1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ezrollers
    New Member
    • Oct 2007
    • 1

    Modify textbox from class other than Form1

    I'm still learning C# and this is really annoying me.

    I need to modify the contents of a text box on a windows program.

    (Using windows app template on VS 2005)

    I have created a property on Form1 (SourcePath is a textbox)

    Code:
    public string SourceDir
            {
                get { return SourcePath.Text; }
                set { SourcePath.Text = value; }
            }
    I then have this method on a class called dirinfo.

    Code:
    public void dirreader(FolderBrowserDialog folderBrowserDialog, ListBox listBox)
            {
                folderBrowserDialog.ShowDialog();
                
               [B] Form1.SourceDir = folderBrowserDialog.SelectedPath;[/B]
                
                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

    Error An object reference is required for the nonstatic field, method, or property 'Ras.Form1.Sour ceDir.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:
    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:
    SourceDir = folderBrowserDialog.SelectedPath;
    on the Form1 class, but I would like to learn how to do it without resorting to this.

    Any ideas?

    TIA
Working...