Solved

Why change form rich text box content fail

Posted on 2011-09-12
4
335 Views
Last Modified: 2012-05-12
Programming a program with Visual studio 2010 C#.
The program just has one form "btGetUBMData", there is a rich text box control "rtbArbitration" in this form.  "doArbitration" function is in added class Arbitration. I changed rtbArbitration text, but when the program running, the text box show nothing, why?
 
public partial class btGetUBMData : Form
{
.....
Arbitration myArbitraion = new Arbitration();
private void btArbitration_Click(object sender, EventArgs e)
{
rtbArbitration.Clear();
Thread ArbiThread = new Thread(new ThreadStart(myArbitraion.doArbitraion));
ArbiThread.Start();
}
}

 public class Arbitration
{
....
public void doArbitraion()
{
....
btGetUBMData getMainForm = new btGetUBMData();
getMainForm.rtbArbitration.AppendText("  starting arbitration\r\n");
}
}

Open in new window

0
Comment
Question by:snowycanada
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
At line #19, you are creating a new instance of your main form.  This instance is not the same as your displayed form.  In fact, the instance pointed to by "getMainForm" is invisible because it was never displayed.

To get this to work, several things must be done:
(1) Pass a reference to your main form into your instance of Arbitration.
(2) Use a delegate and Invoke() to properly update the form since you are attempting to do this from a different thread.

*Your approach is "tightly coupled" because it requires a form instance to be passed in.

*A "loosely coupled" approach would make the Arbitration() class raise a custom event that the main form subscribes to.
0
 

Author Comment

by:snowycanada
Comment Utility
Idle Mind, I had tried several ways to do as your suggestion, but still failed. could you give some code sample about how to pass reference to main form into Arbitration class? thanks a lot
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
Try something like this out:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class btGetUBMData : Form
    {

        Arbitration myArbitraion;

        public btGetUBMData()
        {
            InitializeComponent();
            myArbitraion = new Arbitration(this);
        }

        public RichTextBox rtbArbitrationReference
        {
            get { return this.rtbArbitration; }
        }

        private void btArbitration_Click(object sender, EventArgs e)
        {
            rtbArbitration.Clear();
            Thread ArbiThread = new Thread(new ThreadStart(myArbitraion.doArbitraion));
            ArbiThread.Start();
        }

    }

    public class Arbitration
    {

        private delegate void UpdateDelegate(string text);

        private btGetUBMData myFormReference;

        public Arbitration(btGetUBMData form)
        {
            this.myFormReference = form;
        }

        public void doArbitraion()
        {
            SetRTB("  starting arbitration\r\n");
        }

        private void SetRTB(string text)
        {
            if (myFormReference.rtbArbitrationReference.InvokeRequired)
            {
                myFormReference.rtbArbitrationReference.BeginInvoke(new UpdateDelegate(SetRTB), new object[] { text });
            }
            else
            {
                myFormReference.rtbArbitrationReference.AppendText(text);
            }
        }

    }

}

Open in new window

0
 

Author Comment

by:snowycanada
Comment Utility
Idle Mind: It works, you are definitly expert, thanks a lot!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

7 Experts available now in Live!

Get 1:1 Help Now