Windows Develop Bookmark and Share   
 index > Windows Forms General > modify the contents of a text box on a windows app from a class
• Windows Forms General
• Windows Forms Designer
• ClickOnce and Setup & Deployment Projects
• Windows Forms Data Controls and Databinding
• Windows Forms Sample Applications

modify the contents of a text box on a windows app from a class

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 )
  1. public string SourceDir
  2. {
  3. get { return SourcePath.Text; }
  4. set { SourcePath.Text = value; }
  5. }


I then have this method on a class called dirinfo.

Code: ( text )
  1. public void dirreader(FolderBrowserDialog folderBrowserDialog, ListBox listBox)
  2. {
  3. folderBrowserDialog.ShowDialog();
  4. Form1.SourceDir = folderBrowserDialog.SelectedPath;
  5. listBox.Items.Clear();
  6. DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog.SelectedPath);
  7. DirectoryInfo[] subdirectories = dir.GetDirectories();
  8. FileInfo[] directoryFiles = dir.GetFiles();
  9. foreach (FileInfo str in directoryFiles)
  10. { listBox.Items.Add(str); }
  11. }


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 )
  1. Form1 newform = new Form1();
  2. 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 )
  1. SourceDir = folderBrowserDialog.SelectedPath;
on the Form1 class, but I would like to learn how to do it without resorting to this.

Any ideas?

TIA
a1pro  Monday, October 15, 2007 8:16 AM

Ok, I got it now, but the same line of thought applies. You will have to change your code to look like this:

  1. public void dirreader(FolderBrowserDialog folderBrowserDialog, ListBox listBox, Form theForm)
  2. {
  3. folderBrowserDialog.ShowDialog();
  4. theForm.SourceDir = folderBrowserDialog.SelectedPath;
  5. listBox.Items.Clear();
  6. DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog.SelectedPath);
  7. DirectoryInfo[] subdirectories = dir.GetDirectories();
  8. FileInfo[] directoryFiles = dir.GetFiles();
  9. foreach (FileInfo str in directoryFiles)
  10. { listBox.Items.Add(str); }
  11. }

and you will call it like this:

  1. dirreader(new FolderBrowserDialog, myListBox, this);

this way you are telling your class which Form1 (out of unlimted Form1 that can

be created) is calling the dirreader function, so it will know whichSourceDir propertyto

update.

You should reasearch a little about static methods and also about Reference types

and Value types and the differences between them. And how to properly use them.

Fábio Franco  Monday, October 15, 2007 1:17 PM

Replace Form1.SourceDir with this.SourceDir

this way you are pointing to the correct reference of Form1. Form1 is an object not necessarily the current

Form you see in front of you. You can create as many Form1 as you want but you need the correct instance

of the Form1 to be able to work with it. And you get it using the "this" keyword.

Hope this helps you understand why you are having this problem.

Regards,

Fábio

Fábio Franco  Monday, October 15, 2007 10:38 AM

Hi a1pro,

You declaredSourceDir property on Form1 class whichis not static.

On line 5 You want to update it but in a static context using type name.propety name. This is the line that is raising exception during compile time.

Removing type name gave You instance context and since SourceDir property is an instance property there is no more compile-time exception.

You can read about Static Classes and Static Class Members here.

Regards,

Oleh.

Oleh Svintsitskyy  Monday, October 15, 2007 10:46 AM
Fabio,

I probably did not explain this too well.

trying [code]this.SourceDir[/code] will not work as dirinfo has no definition of SourceDir.

SourceDir is a property in Form1, where Form1 is the form that one gets when using VS 2005 win app template.

[code]
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

}

public string SourceDir
{
get { return SourcePath.Text; }
set { SourcePath.Text = value; }
}



[/code]

Oleh,

I don't quite follow what you mean

Thanks for your replies anyway
a1pro  Monday, October 15, 2007 1:03 PM

Ok, I got it now, but the same line of thought applies. You will have to change your code to look like this:

  1. public void dirreader(FolderBrowserDialog folderBrowserDialog, ListBox listBox, Form theForm)
  2. {
  3. folderBrowserDialog.ShowDialog();
  4. theForm.SourceDir = folderBrowserDialog.SelectedPath;
  5. listBox.Items.Clear();
  6. DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog.SelectedPath);
  7. DirectoryInfo[] subdirectories = dir.GetDirectories();
  8. FileInfo[] directoryFiles = dir.GetFiles();
  9. foreach (FileInfo str in directoryFiles)
  10. { listBox.Items.Add(str); }
  11. }

and you will call it like this:

  1. dirreader(new FolderBrowserDialog, myListBox, this);

this way you are telling your class which Form1 (out of unlimted Form1 that can

be created) is calling the dirreader function, so it will know whichSourceDir propertyto

update.

You should reasearch a little about static methods and also about Reference types

and Value types and the differences between them. And how to properly use them.

Fábio Franco  Monday, October 15, 2007 1:17 PM
That works fine.

I don't know why I didn't think about using this as a parameter for dirreader to ensure that the running instance of form1 would be used.

At any rate, thanks for your help
a1pro  Monday, October 15, 2007 1:55 PM

You can use google to search for other answers

 

More Threads

• Inconsistency in System.Drawing.Color?
• How to set windows to open an application with one click
• MDI Forms
• How to Set Tooltip to Combobox Item at Binding Time in vb.net
• Vertical Line like in Installshield wizard
• Regex and datagrids..
• searching a rtb for a complete word
• Reference Using both word 2003 and 2007.
• Update UserControl's width inside FlowLayoutPanel automatically
• More RichText Measuring