?
Solved

Why change form rich text box content fail

Posted on 2011-09-12
4
Medium Priority
?
345 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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