Solved

Why change form rich text box content fail

Posted on 2011-09-12
4
337 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
ID: 36526527
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
ID: 36534015
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
ID: 36535786
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
ID: 36538084
Idle Mind: It works, you are definitly expert, thanks a lot!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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