0

Still in the process of learning C#, but I'm a bit confused on something here.

For example, I have a textbox on my form and it has the name of testTXT. Based on the code below, I've created a new class outside of the public partial one that's there by default, and now I'm trying to access testTXT but I cannot. I'm going to also need to access several other textboxes and things later on as well.

Here's a snippet of the code I'm working with thus far:

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

        private void testButton_Click(object sender, EventArgs e)
        {
            GeneratedClass gc = new GeneratedClass();
            gc.CreatePackage("C:\\Users\\user\\Downloads\\output.docx");
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            var fsd = new FolderSelect.FolderSelectDialog();
            fsd.Title = "Select folder to save document";
            fsd.InitialDirectory = @"c:\";
            if (fsd.ShowDialog(IntPtr.Zero))
            {
                testTXT.Text = fsd.FileName;
            }
        }
    }

    public class GeneratedClass
    {
          **trying to access testTXT right here, but can't.**
    }
}

Any help would be greatly appreciated.

rogerdeuce
  • 1,471
  • 6
  • 31
  • 48
  • Hmm ok. Thanks for the help thus far guys. Going to try working it out based on the suggestions and post back/mark answer shortly. Thanks – user3808818 Jul 17 '14 at 09:36
  • Have you had a look at this post? Possible duplicate http://stackoverflow.com/questions/4928195/c-sharp-windows-forms-application-updating-gui-from-another-thread-and-class You still need to pass the form to the new class though – Xeon Jul 17 '14 at 09:36
  • I guess this is WinForms? – Uwe Keim Jul 17 '14 at 09:38
  • Uwe Keim: Correct. @Xeon I haven't taken a look at that post particularly, although I've looked at a few other similar ones. Thanks for mentioning this one. – user3808818 Jul 17 '14 at 09:42

4 Answers4

1

You could do this (see other answers), but you really shouldn't.

Nobody but the containing form has to know about the textboxes in it. Who knows, they might disappear, have their name changed, etc. And your GeneratedClass could become a utility class used by lots of forms.

The appropriate way of doing this, is to pass whatever you need from your textbox to your class, like so:

private void testButton_Click(object sender, EventArgs e)
{
    GeneratedClass gc = new GeneratedClass();
    gc.CreatePackage(this.testTxt.Text);
}

public class GeneratedClass
{
      public void CreatePackage(string name) { // DoStuff! }
}
Davio
  • 4,609
  • 2
  • 31
  • 58
  • Hmm, gotcha. Didn't think of this workaround. Thanks a lot man. Going to consider this for now, but I believe I'll probably want to get this working a bit better since I'll probably have a ton of textboxes that'll need to be accessed by the class. – user3808818 Jul 17 '14 at 09:41
0

This is because you have your TextBox type defined in Form1 class as private member. Thus can't be access with another class instance

Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83
0

Your question has little to do with C#, more to do with Object Oriented Concepts.

Instance of TextBox has to be given to 'GeneratedClass' somehow.

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

        private void testButton_Click(object sender, EventArgs e)
        {
            GeneratedClass gc = new GeneratedClass(testTXT);
            gc.DoSomething();
            gc.CreatePackage("C:\\Users\\user\\Downloads\\output.docx");
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            var fsd = new FolderSelect.FolderSelectDialog();
            fsd.Title = "Select folder to save document";
            fsd.InitialDirectory = @"c:\";
            if (fsd.ShowDialog(IntPtr.Zero))
            {
                testTXT.Text = fsd.FileName;
            }
        }
    }

    public class GeneratedClass
    {
          TextBox _txt;
          public GeneratedClass(TextBox txt)
          {
            _txt= txt;
          }

          public void DoSomething()
          {
            txt.Text = "Changed the text";
          }
    }
}
fahadash
  • 3,133
  • 1
  • 30
  • 59
0

You must make testTXT public. See Protection level (Modifiers) of controls change automaticlly in .Net.

And access to TextBox as

public class GeneratedClass
{
      GeneratedClass(Form1 form)
      {
         form.testTXT.Text = "1";
      }

}
Community
  • 1
  • 1
bobah75
  • 3,500
  • 1
  • 16
  • 25
  • Is there something in addition to setting the Modifier to public to get this to work? I switched to Public, but still can't see it pop up in the Form1. – user3808818 Jul 17 '14 at 09:40
  • You need set public Modifier to TextBox. It will appear in IntelliSense. – bobah75 Jul 17 '14 at 09:56
  • Correct. The Modifier is set to "Public" atm, but still can't see it in IntelliSense as of yet. Hmm... I'll keep digging. Thanks though man. – user3808818 Jul 17 '14 at 09:57