I am working on a C# Windows Form project and I need to access the controls on a form from a separate class within the project. can anyone help me with this?

Recommended Answers

All 5 Replies

You could pass the controls you want to access to this class?

Hi,

There are few ways to this. One of it is by sending IntPtr pointing to COntrol's handle. Here is the code.

[Form1]

public partial class Form1 : Form
    {
        private static IntPtr m_oControlHandle;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_oControlHandle = textBox1.Handle;
            Class1 cls = new Class1();
            cls.AccessControl();
        }

        public static IntPtr GetHandle()
        {
            return m_oControlHandle;
        }

    }

[Class1]

class Class1
    {
        public void AccessControl()
        {
            IntPtr p_controlPtr = Form1.GetHandle();

            TextBox txt = (TextBox)Control.FromHandle(p_controlPtr);

            txt.Text = "testing";           
        }
    }

This is a raw solution to give you some ideas. Please remember to clean up the pointer at Form's dispose.

Please vote me & close the thread if this solved your problem.

I did a simple example with 2 forms. Both have textBoxes, and when clicking the button on form2, text transfers to from textBox on form2 to textBox on form1.

//form1:
 public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            StartMethod();
        }

        private void StartMethod()
        {
            string strID = "SS001";
            int intID = Convert.ToInt32(strID.Substring(strID.Length - 3));  
            //increase the value by 1:
            intID++;
            strID = intID.ToString();
            strID = CreateNewAutoIncrementID(strID);
            UseNewID(strID);
        }

        private string CreateNewAutoIncrementID(string strId)
        {
            int idLenght = strId.Length;
            while (idLenght < 3)
            {
                strId = "0" + strId;
                idLenght = strId.Length;
            }
            strId = "SS" + strId;
            return strId;
        }

        private void UseNewID(string strID)
        {
            //for exampe,
            //method where you can use the new id!
        }

        public void SetTextFromAway(string msg)
        {
            textBox1.Text = msg;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this);
            form2.Show(this);
        }
    }

//form2:
  public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 _form1)
        {
            InitializeComponent();
            form1 = _form1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string value = textBox1.Text;
            form1.SetTextFromAway(value);
        }
    }

Thanks Guys, that was very helpful, I have got it to work now.

If it was, please mark the thread as answered.
thx
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.