Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Calling Method in another class

Posted on 2011-09-05
1
290 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 63

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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