Reputation: 1179
I'm trying to add items to listbox from another class,the information pass to the function but the listbox doesn't seem to update. this is my code:
Main class (FORM) :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// the function that updates the listbox
public void logURI(string OutputLog, string Information, string JOB)
{
try
{
listBox1.BeginUpdate();
listBox1.Items.Insert(0, DateTime.Now.ToString() + " : " + JOB + " " + Information);
listBox1.Items.Add("1");
listBox1.EndUpdate();
textBox1.Text = JOB;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Second Class:
public class FtpFileSystemWatcherTS
{
Form1 logs = new Form1();
logs.logURI( "", "Found folder modefied today (" + FileName.TrimEnd(), ") ElectricaTS");
}
What am I doing wrong?
Upvotes: 1
Views: 6396
Reputation: 8613
You are creating the Form
from within the other class - any changes you make for the Form
's children will not be displayed because it is another form that is being shown. Instead, you want to either pass the Form
instance that is running into the FtpFileSystemWatcher
class, so that it can access the Form.Controls
property, or give it direct access to the ListBox
or source of the ListBox
items.
EDIT
Suggestion:
public partial class Form1 : Form
{
private FtpFileSystemWatcher mWatcher;
// ... some code ...
public Form1()
{
InitializeComponent();
// Create a new watcher and give it access to this form
mWatcher = new FtpFileSystemWatcher(this);
}
// ... Logging code ...
}
public class FtpFileSystemWatcher
{
private Form1 mMainForm;
public FtpFileSystemWatcher(Form1 mainForm)
{
mMainForm = mainForm;
}
public void Log()
{
mMainForm.logUri(...);
}
}
This is just an example of some code format that you could use to give the FtpFileSystemWatcher
access to the running Form
. This will be setup when the Form
is run (assuming you have it running correctly). You should then see your desired updates.
Upvotes: 2
Reputation: 250
you can use inheritance easily since the procedure's access modifier is set to public
Main class (FORM) :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// the function that updates the listbox
public void logURI(string OutputLog, string Information, string JOB)
{
try
{
listBox1.BeginUpdate();
listBox1.Items.Add("0");
listBox1.Items[0] = DateTime.Now.ToString() + " : " + JOB + " " + Information;
listBox1.Items.Add("1");
listBox1.EndUpdate();
textBox1.Text = JOB;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public class FtpFileSystemWatcherTS : Form1
{
logURI( "", "Found folder modefied today (" + FileName.TrimEnd().toString(), ") ElectricaTS");
}
Upvotes: 0