Solved

Why change form rich text box content fail

Posted on 2011-09-12
4
336 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sum Column in GridView 3 44
Syntax Error 2 47
C# Single Form 8 29
C#  Radio button search for Date not DateTime 4 35
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

861 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

28 Experts available now in Live!

Get 1:1 Help Now