How do I access controls on a form from a Class (Winforms)

I have a UserControl with a textbox, and then added the UserControl to a form. When I click the find button on the windows form, I want a Class to get the data from the textbox. I tried to access the textbox from the Class, but it does not show in the intellisense, whether I make the textbox private or public. I tried to make the UserControl public also, but that does not show up in the intellisense either.

Best regards,
Dave
kalittaairAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
This uses a class with a static method, and that method takes 1 parameter which is a reference to a "Form1" object.
The userControl1 on the form, and the textBox1 on the UserControl are both public.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

		private void button1_Click(object sender, EventArgs e)
		{
			MessageBox.Show(OtherClass.GetTextBoxValue(this));
		}
	}

	public class OtherClass
	{
		public static string GetTextBoxValue(Form1 form)
		{
			return form.userControl11.textBox1.Text;
		}
	}
}

Open in new window

0
Todd GerbertIT ConsultantCommented:
Alternatively, you can create an instance of OtherClass and pass it's contructor a reference to the user control...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

		private void button1_Click(object sender, EventArgs e)
		{
			OtherClass oc = new OtherClass(this.userControl11);
			MessageBox.Show(oc.GetTextBoxValue());
		}
	}

	public class OtherClass
	{
		private UserControl1 _uc;
		public OtherClass(UserControl1 TheUserControl)
		{
			_uc = TheUserControl;
		}
		public string GetTextBoxValue()
		{
			return _uc.textBox1.Text;
		}
	}
}

Open in new window

0
Todd GerbertIT ConsultantCommented:
It would help to see what code you've got so far.
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

kalittaairAuthor Commented:
Putting the class in the form will work however, i would like to put class in a seperate folder and access the class.cs from the form.
Thanks,
Dav
0
Todd GerbertIT ConsultantCommented:
The class is not in the form, I just put all the code together in one file to make it easier to read. Whether the OtherClass code is in it's own .cs file or not is entirely irrelevant, and in no way shape or form affects the code's behavior.
The point is that OtherClass needs to have a reference of some sort that points back to Form1 or the User Control on Form1.
Everything in C# is a class, including your forms - so the interaction between a form and some other class is the same as the interaction between two classes of any type.

/*******************
***** Form1.cs *****
*******************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

		private void button1_Click(object sender, EventArgs e)
		{
			OtherClass oc = new OtherClass(this.userControl11);
			MessageBox.Show(oc.GetTextBoxValue());
		}
	}
}


/************************
***** OtherClass.cs *****
************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
	public class OtherClass
	{
		private UserControl1 _uc;
		public OtherClass(UserControl1 TheUserControl)
		{
			_uc = TheUserControl;
		}
		public string GetTextBoxValue()
		{
			return _uc.textBox1.Text;
		}
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kalittaairAuthor Commented:
Thank you for all your help. The problem was that the Class was put in another project in the same Solution. when I moved the Class to the same project it worked fine.
0
kalittaairAuthor Commented:
updating the reason why I had the problem in the first place.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.