By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

How to pass String between Windows Forms

P: n/a
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",MessageBoxB uttons.OK,MessageBoxIcon.Stop);
}
}

private void showToastForm(string stringEvent)
{
tform.Show();
// do i need to put something here to pass string to toastForm?
}
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In Child form: public frmChild mydad;

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

Nov 17 '05 #2

P: n/a
Should be more like:
In Child form: public frmParent mydad;

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

Nov 17 '05 #3

P: n/a
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.

Nov 17 '05 #4

P: n/a
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

"RvGrah" wrote:
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.

Nov 17 '05 #5

P: n/a
Glad to help :-)

Nov 17 '05 #6

P: n/a
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

NewToCode wrote:
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",MessageBoxB uttons.OK,MessageBoxIcon.Stop);
}
}

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

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.