Solved

displaying on the different form

Posted on 2009-05-12
7
306 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
ID: 24368586
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
ID: 24368673
possibly some codes here?  it sounds too hard for my level. sorry.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24378902
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
ID: 24383280
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
ID: 24383556
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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 …

813 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

17 Experts available now in Live!

Get 1:1 Help Now