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?
 
Todd GerbertConnect With a Mentor IT 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
 
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Todd GerbertIT ConsultantCommented:
It would help to see what code you've got so far.
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.