Search code examples
c#vb.netvisual-studiotcpuserform

C# Excecute method in original Form from another Form


I have a simple question for some of you.

I have two forms which contain two methods:

FORM 2: Which will take input from textbox and run a method from FORM1

public partial class Form2 : Form
    {
        Form1 mainForm = new Form1();
        public Form2()
        {
            InitializeComponent();
            //Form2_Load();
                        
        }


        private void txtCmdOutput_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    mainForm.outPutMessage(txtCmdOutput.Text.Length, 2, txtCmdOutput.Text.ToString());
                    txtCmdOutput.Text = "";

                }
            }
            catch (Exception err)
            {

            }
        }

FORM 1: Which will contain the method that will excecute using the inputs from FORM 2

public void outPutMessage(int bufferSize1, int messageID1, string message1)
        {
            NetworkStream networkstream = client.GetStream();


            int bufferSize = bufferSize1;
            var message = message1;

            int messageID = messageID1;

            string headerStr = "len:" + message.Length.ToString() + "\r\nMsgId:" + messageID;

            byte[] headerLength = BitConverter.GetBytes(headerStr.Length);

            networkstream.Write(headerLength, 0, 4);

            networkstream.Write(Encoding.ASCII.GetBytes(headerStr), 0, headerStr.Length);


            int bytesSent = 0;
            int bytesLeft = message.Length;
            byte[] encodedMessage = Encoding.ASCII.GetBytes(message);

            while (bytesLeft > 0)
            {
                int curDataSize = Math.Min(bufferSize, bytesLeft);
                networkstream.Write(encodedMessage, bytesSent, curDataSize);

                bytesSent += curDataSize;
                bytesLeft -= curDataSize;
            }
        }

My question is, is it possible to call the

outPutMessage(txtCmdOutput.Text.Length, 2, txtCmdOutput.Text.ToString());

from FORM 2(by taking the input from the textbox), but actually run the method in its original form (FORM 1)? as there is where all the other variables are contained.

What I attempted to do is indicate Form1 mainForm = new Form1(); and call the method, but it seems to then run the method in FORM 2.

UPDATE:

This is how I create FORM 2 from FORM 1:

private void button_Click1(object sender, EventArgs e)
        {
            Button selected = sender as Button;
            openClientForm(new Form2());
            outPutMessage("".Length, 4, ""); 
        }

private Form2 activeForm = null;
        private void openClientForm(Form2 clientForm)
        {
            if (activeForm != null)
                activeForm.Close();
            activeForm = clientForm;
            clientForm.TopLevel = false;
            clientForm.FormBorderStyle = FormBorderStyle.None;
            clientForm.Dock = DockStyle.Fill;
            panelClientForm.Controls.Add(clientForm);
            panelClientForm.Tag = clientForm;
            clientForm.BringToFront();
            clientForm.Show();
        }

THanks in advance.


Solution

  • As per @jmcilhinney setup an event in the child form, here in a Button Click event push information to listeners which means the child form does not know anything about the calling form.

    This would be your child form

    namespace PassStringFromChildToParentForm
    {
        public partial class ChildForm : Form
        {
            public delegate void OnPassData(int bufferSize1, int messageID1, string message1);
            public event OnPassData PassData;
    
            public ChildForm()
            {
                InitializeComponent();
            }
    
            private void PassDataButton_Click(object sender, EventArgs e)
            {
                PassData?.Invoke(textBox1.Text.Length,2, textBox2.Text);
            }
        }
    }
    

    The main form

    namespace PassStringFromChildToParentForm
    {
        public partial class MainForm : Form
        {
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void ShowChildForm_Click(object sender, EventArgs e)
            {
                ChildForm childForm = new ChildForm();
                childForm.PassData += PassData;
                childForm.Show(this);
                
            }
            private void PassData(int bufferSize1, int messageID1, string message1)
            {
                // perform work
            }
        }
    }