Solved

Calling Method in another class

Posted on 2011-09-05
1
286 Views
Last Modified: 2012-05-12
AppendTextBox1 is defined in my Form1 class. How can I call it from another class to append text to my TextBox1?

using System;

namespace Program
{
  class MyClass
  {
    public MyClass()
    {
      AppendTextBox1("Hello World from MyClass" + Environment.NewLine);
    }
  }
}

Open in new window


using System;
using System.Windows.Forms;

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

    // This delegate enables asynchronous calls for setting
    // the text property on the textBox1 control.
    delegate void AppendTextBox1Delegate(string text);


    /* Call this method to append text to textBox1 */
    public void AppendTextBox1(string text)
    {
      if (this.textBox1.InvokeRequired)
      {
        AppendTextBox1Delegate d = new AppendTextBox1Delegate(AppendTextBox1);
        this.textBox1.Invoke(d, new object[] { text });
      }
      else
      {
        this.textBox1.AppendText(text);
      }
    }


    private void button1_Click(object sender, EventArgs e)
    {
      AppendTextBox1("Hello World from button1_Click" + Environment.NewLine);
      MyClass m = new MyClass();   //test
    }
  }
}

Open in new window

0
Comment
Question by:deleyd
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 36484918
Hi deleyd;

Make the following change to Form1

MyClass m = new MyClass(this);   //test

Open in new window


Then make the following changes to MyClass.

private Form1 parentForm = null;

public MyClass(Form1 pForm1  )
{
    parentForm = pForm1;
    parentForm.AppendTextBox1( "Hello World from MyClass" + Environment.NewLine );
}

Open in new window


MyClass needs a reference to the class it is calling. So to be able to call functions in Form1 from MyClass, MyClass needs a reference to Form1. One way to do that is to have the constructor of MyClass to receive a reference to a form it will be communicating with.

Fernando
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now