forms, classes and frustration

  • Thread starter Thread starter Treefrog
  • Start date Start date
T

Treefrog

Hi,

I'm developing a serious headache trying to get this to work. I want to
write to a label on frmMain from another class but I can't for the life of
me figure out how to do it. I'm pretty new to c# and the only other language
I've used properly is PHP so a lot of the concepts are difficult for me to
grasp. Why it's so difficult to do what I believed should be simple is
beyond me but as I said, some concepts are difficult to understand. Any help
would be very much appreciated.

This code is currently on the form:
------------------------------------------------------------------
public void updateStatus(string newText)
{
UpdateUIDelegate uid = new UpdateUIDelegate(UpdateUI);
this.Invoke(uid, new object[] { newText });
} // end updateStatus


delegate void UpdateUIDelegate(string str);
private void UpdateUI(string uiInfo)
{
if (StatusLineCount >= StatusLineLimit)
{
this.lblStatus.Text = "";
StatusLineCount = 0;
}
this.lblStatus.Text += uiInfo;
this.lblStatus.Refresh();
StatusLineCount++;
} // end UpdateUI
-------------------------------------------------------------------

When a button is pressed on the form I run

testClass hello = new testClass("example string");

and then in there I try

updateStatus("example string");

doesn't work so I tried frmMain. but it's doesn't display updateStatus in
the autofill.
I tried making updateStatus static but that didn;t work because it had no
reference to the instance of frmMain so couldn't use the label.

Can anybody tell me how to do this? I'm sure it must be easy but I can't
figure it out....

thanks in advance,


Treefrog
 
Hi,

The testClass should be given a reference to the main form upon
construction, like this:

class testClass
{
// Some private members declared...

frmMain _parentForm;
string _statusText;

public testClass(frmMain form, string text)
{
_parentForm = form;
_statusText = text;

// Do the rest of initialization.
}

public void WorkerMethod()
{
// Do some useful stuff...
// and now...
parentForm.updateStatus(_statusText);
}
}
 
Back
Top