Solved

displaying on the different form

Posted on 2009-05-12
7
326 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
[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
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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

632 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