Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How could iI change value in a windows form from another class?

Status
Not open for further replies.

Wolf67

Programmer
Mar 18, 2005
11
SE
I have create a windows form frmAccount, in the form a have a richtextbox "rtxtLastScan" from the form I call (FileDirectory fd = new FileDirectory();
fd.timer(int.Parse(rtxtSeconds.Text), rtxtAccount.Text, strStartUpPath);) another class FileDirectory.cs. From this class i want to set rtxtLastScan´s value, I tried different
ways but nothing seems to work. When i debugg i got down to ScanDirectory() and the code runs tro but the text in frmAccount rtxtLastScan doesn´t change. Could anyone help me?


public class FileDirectory
{

string strStartUpPath = "";
Account myAcc = new Account();
frmAccount myFrmAccount;



public void ScanDirectory()
{

myFrmAccount = new frmAccount();

myFrmAccount.rtxtLastScan.Text = "Hello";


}
}
 
You must not update a WinForm control from a different thread than created the form. This is a Win32 limitation.

Since you're calling it from a timer, and timers run on a threadpool, that means that you're calling it from another thread (bad thing to do).

Write some code in your form like this:
Code:
public delegate void ScanEventHandler(object sender, DateTime lastScanTime);
:
:
:
private void UpdateScan(object sender, DateTime lastScanTime)
{
  if (this.InvokeRequired)
  {
    this.BeginInvoke(new ScanEventHandler(UpdateScan),
                    new object[] { sender, lastScanTime } );
    return;
  }

  this.rtxtLastScan.Text = lastScanTime.ToString();
}
This will check to see if you're calling it on the current thread, and if not, do a BeginInvoke to make sure you are on the correct thread, and call back into itself to update the textbox.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top