How to pass String between Windows Forms

G

Guest

I built an application aroung the FileSystemWatcher and i am having trouble
passing a string from my mainForm to a second form toastForm. Can someone
please give me some insight on how to do this. I am a noob to programming so
go easy on me. I have googled searced for 2 days and have yet to find
something that makes sense to me.

Specifically i am wanting to pass string stringEvent to my toastForm
What kind of code do i need to place in my toastForm to acces the stringEvent?

Thanks for your help.


mainForm code

private void myFileWatcher_Deleted(object source,
FileSystemEventArgs e)
{

try
{

ListViewItem item1 = new ListViewItem(e.Name);
item1.SubItems.Add(e.ChangeType.ToString());
item1.SubItems.Add(DateTime.Now.ToShortDateString());
item1.SubItems.Add(DateTime.Now.ToShortTimeString());

listView1.Items.Insert(0, item1);

string stringEvent = e.FullPath.ToString();
showToastForm(stringEvent)
updateStatus(stringEvent);
this.Refresh();

}

catch (Exception ex)
{

MessageBox.Show(ex.Message,"Exception",MessageBoxButtons.OK,MessageBoxIcon.Stop);
}
}

private void showToastForm(string stringEvent)
{
tform.Show();
// do i need to put something here to pass string to toastForm?


}
 
R

RvGrah

Should be more like:
In Child form: public frmParent mydad;

In Parent form: f.mydad = this;
f.Show();
 
R

RvGrah

Read your post better, my answer is for modifying parent form variables
from the child form. For passing strings to your child form, declare
them as public in the main body of your child form class, then you can
set their value just before calling the Show() method of the child
class.
 
G

Guest

Thanks i knew it would be easy!
all it took was:

public string setLabelEvent; // in my child form
label1.Text = setLabelEvent; // in my child form


tform.setLabelEvent = "Foobar"; // Parent form
 
B

Bob Grommes

It looks like toastForm is already in existence, so likely you would
create a protected field in ToastForm to hold the event, and a public
property to access it. Then you could access the string from anyplace
within the toastForm. You could even put code in the property setter
that would update the form based on the string, if you want.

class toastForm : Form {
protected string eventString;

public string EventString {
set {this.eventString = value;}
get {return this.eventString;}
}


protected void SomeMethodThatUsesEventString() {

if (this.eventString == null) {
throw new ApplicationException("EventString has not been set.");
}

// do something with this.eventString
}

}

//In showToastForm():

tForm.EventString = stringEvent;
tForm.Show();

If you were creating toastForm each time you show it, you could also
pass the event string to toastForm's constructor.

Hope this helps,

--Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top