Hello,
I am new to c# and have come accross a problem that I just can't get to work right.
Basically I have a form with a text box I want to use a log of events. I click on a button on the form to run certain tasks, basically this calls a static function in another class. Work is done in that function, but this also calls a void (sub) in the same class. I want to upate the text box on the form with info that is being processed in my void. So, I want it to update the text box live, not all after the fact.
Here is a basic example of what I am doing...
Here Is My Form...
namespace
ResponseTester{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string
ok = clsDAL.DoSomething();}
{
}
}
}
Here Is My Other Class..
namespace
ResponseTester{
class clsDAL
{
public static string DoSomething()
{
//do some work
UpdateForm();
return "OK";
}
public static void UpdateForm()
{
string[] files = Directory.GetFiles(@"C:\");
foreach (string i in files)
{
//I want to print each file to the text log on the main page
}
}
}
}
I played with delegates and other threads for a while and just couldn't get this to work correctly. Thanks in advace to any help anyone can give me.
thanks,
Chad
Chad ManclPosted Apr 7, 2008, 11:16 AM
Thanks for your help. With some minor tweaking I was able to get this to work exactly how I needed it to.
Chad
AlanPosted Apr 7, 2008, 10:22 AM
Try this:
namespace ResponseTester
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
// add this method
public void UpdateTextBox(string textLog)
{
textBox1.Text = textLog; // replace textBox1 with actual variable name
}
private void button1_Click(object sender, EventArgs e)
{
string
ok = clsDAL.DoSomething();}
{
}
}
}
namespace
ResponseTester{
class clsDAL
{
public static string DoSomething()
{
//do some work
UpdateForm();
return "OK";
}
public static void UpdateForm()
{
string[] files = Directory.GetFiles(@"C:\");
string textLog = "";
foreach (string i in files)
{
textLog += i + Environment.NewLine;
}
// get reference to frmMain instance
frmMain f = (frmMain)Application.OpenForms["frmMain"];
// call UpdateTextBox method
f.UpdateTextBox(textLog);
}
}
}