Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag for United States of America

asked on

displaying on the different form

Hi I am experimenting some odd scenarios here.
I have 2 forms: 'Form1.cs' and 'Form2.cs'
I have 'MyClass' class.
Form1 has only one button.(button1)
Form2 has only one richtextbox.(RichTextBox1)

This is what I want to do:
When I click a button in Form1, it calls myMethod in 'MyClass' class.  Then, myMethod displays the msg into RichTextBox1 in Form2.

In Form1, I have
public partial class Form1 : Form
    {
       
        Form2 f2 = new Form2();
        Thread t = null;
 
        public Form1()
        {
            InitializeComponent();            
                       
            //when closing form we must stop thread first
            f2.FormClosing +=  new FormClosingEventHandler(f2_FormClosing);
        }
 
 
        void f2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t != null)
            {
                t.Abort();
                t.Join();
                t = null;
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (t != null)
                return;

            f2.Show();
            t = new Thread(new ThreadStart(MyClass.myMethod));
            t.Start();
        }
    }  

Form2 doesn't have one richtextbox1.  Its property is RichTextBox1.
MyClass has the follwoing code:
class MyClass
    {
       // Form2 f2 = new Form2();
        public static void myMethod()
        {
            Helper.AddString("Hi~~ I am from Helper class!!!");
           
        }

        public delegate void AddStringDelegate(string str);              
        public static void AddString(string str)
        {
            Form2 f2 = new Form2();
            if (f2.RichTextBox1.InvokeRequired)                                
            {                                                    
                f2.RichTextBox1.Invoke(new AddStringDelegate(AddString), str);
                return;                                          
            }                                              

            f2.RichTextBox1.AppendText(str + "\n");
        }
    }
I know that f2 used in Form1 to open Form2 and f2 in MyClass are the same one.  So how can I solve this problem?
Please note that I want to add the msg. from MyClass.
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

W/o getting into too much discussion on design, at a minimum, it would seem that you should be using events. Have the forms sink an event from the class. Have the class raise the event and it's done. A class should not know anything about the contents of a form. Those details should be internal.
Avatar of IzzyTwinkly

ASKER

possibly some codes here?  it sounds too hard for my level. sorry.
Avatar of Bob Learned
Add a property to Form2, and set the value:

Example:

f2.Property = "This is an example";
private string _property = "";
public string Property 
{
   get { return _property; }
   set { _property = value; }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Agarici
Agarici
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No offense intended to anyone, but I'm disappointed to see all this advice of reaching into a UI control to manipulate its contents. VB6 really instilled this type of concept, which seems to have intrenched itself deeply. Developers seem to have carried the approach forward into OO environments. Paradigms like Model View Controller are examples of really good seperation of concern, but you don't have to go to such extremes to minimize tight coupling like this. As I suggested previously this scenario really begs for events.