Solved

displaying on the different form

Posted on 2009-05-12
7
286 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:IzzyTwinkly
7 Comments
 
LVL 16

Expert Comment

by:ToddBeaulieu
Comment Utility
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.
0
 

Author Comment

by:IzzyTwinkly
Comment Utility
possibly some codes here?  it sounds too hard for my level. sorry.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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

0
 
LVL 11

Accepted Solution

by:
Agarici earned 500 total points
Comment Utility
the way i see it, in order to keep your design you should pass a thread parameter when starting the thread "t"
like this:
            f2.Show();
            t = new Thread(new ParameterizedThreadStart(MyClass.myMethod));
            t.Start(f2);

then modify myMethod to match the ParameterizedThreadStart delegate
like this:
        public static void myMethod(object data)
        {
            Helper.AddString((data as Form),"Hi~~ I am from Helper class!!!");            
        }

and filanlly, modify the addString method to receive one more parameter (the form f2)
like this:
        public static void AddString(Form f2, string str)
        {
            // the next line is no longer needed, f2 comes as a parameter
            // Form2 f2 = new Form2();
            if (f2.RichTextBox1.InvokeRequired)                                
            {                                                    
                f2.RichTextBox1.Invoke(new AddStringDelegate(AddString), str);
                return;                                          
            }                                              

            f2.RichTextBox1.AppendText(str + "\n");
        }

hth,
A.
0
 
LVL 16

Expert Comment

by:ToddBeaulieu
Comment Utility
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

12 Experts available now in Live!

Get 1:1 Help Now