Solved

Calling Method in another class

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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