user3808818
user3808818

Reputation: 11

C# Trying to access textbox on form through a new class

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.

Upvotes: 0

Views: 102

Answers (4)

Davio
Davio

Reputation: 4737

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! }
}

Upvotes: 1

bobah75
bobah75

Reputation: 3570

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";
      }

}

Upvotes: 0

fahadash
fahadash

Reputation: 3281

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";
          }
    }
}

Upvotes: 0

Rajeev Kumar
Rajeev Kumar

Reputation: 4963

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

Upvotes: 0

Related Questions