Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

displaying on the different form

Posted on 2009-05-12
7
Medium Priority
?
339 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
5 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

877 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