Solved

Why change form rich text box content fail

Posted on 2011-09-12
4
341 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
[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
  • 2
  • 2
4 Comments
 
LVL 86

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 86

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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