4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
0

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.

0

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);
        }
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.