Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calling Method in another class

Posted on 2011-09-05
1
Medium Priority
?
302 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
[X]
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
1 Comment
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

609 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