0

I wanted to pass data to form1 from another class but I fail to do so. What I have tried is

    class other_class {
      public a_function() {
        Form1 form1 = new form1();
        form1.something("Lorem Ipsum");
      }
   }

While on the form1 is simply

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            other_class a = new other_class();
            a.a_function();
        }
        public void something(string str) {
            //Pass to Another generic function
            another_function(str);
        }
       public void another_function(string str) {
            //Do update Textbox and RichtextBox Fields
       }
 }

However, it seemed that the functions within form1 is not callable from another class. What's the best fix or an alternative for this?

Mac Cortes
  • 37
  • 7
  • 1
    Your `other_class` code does not compile. You need to implement a method or constructor in that class and then call the `form1` method from there. You should also call `form1.Show` at some point, otherwise how do you know it's not working? – Rufus L Apr 10 '18 at 04:48
  • @RufusL I updated the code. There was an issue with previous example – Mac Cortes Apr 10 '18 at 04:57
  • There's still an issue. You have a recursive loop now, where your form initialization tries to instantiate a class, which then instantiates the form again. And you're still not calling `.Show` anywhere... What are you actually trying to do? This example does not make sense. – Rufus L Apr 10 '18 at 04:59
  • Possible duplicate of [Passing data between two forms with properties](https://stackoverflow.com/questions/5087934/passing-data-between-two-forms-with-properties) – Keith Nicholas Apr 10 '18 at 05:17
  • this gets asked over and over and over and over again – Keith Nicholas Apr 10 '18 at 05:17

2 Answers2

0

Following code will be helpful to you,

other_class.cs :

 class other_class
 {
        //Create a constructor in order to initialise the class
        public other_class()
        {
        }

        public void a_function()
        {
            Form1 form1 = new Form1();
            form1.something("Lorem Ipsum");
            form1.Show();
        }
 }

Form1.cs :

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

        public void something(string str)
        {
             another_function(str);
        }

        public void another_function(string str)
        {
            //Added a label to Form1.cs
            //Set its text here 
            label1.Text = str;
        }
}

and I have created another form Form2 in order to initialize other_class and call other_class's a_function() method from there as follows,

public partial class Form2 : Form
{
        public Form2()
        {
            InitializeComponent();
        }

        //Added a button to Form2.cs
        //Set a button click event
        private void button1_Click(object sender, EventArgs e)
        {
             other_class obj = new other_class();    
             obj.a_function();      
        }
}
Abhilash Ravindran C K
  • 1,818
  • 2
  • 13
  • 22
0

I solved my own question thanks to: https://social.msdn.microsoft.com/Forums/vstudio/en...

class other_class {
      public Form1 form;

      public other_class(Form1 frm) {
        form = frm;
      }
      public void a_function() {
        form.something("Lorem Ipsum");
      }
   }

and

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        other_class a = new other_class(this);
        a.a_function();
    }

I realized that I don't have to reinitiate the form1 since it's running already

Mac Cortes
  • 37
  • 7