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

Stupid question...

P: n/a
I constantly have a delima calling functions inbetween forms, for
example in a project, in frmMain.cs , I have a function called
RefMain();, I would like to call it in frmAdd.cs, but none of the
following methods work:

1:
frmMain frm;
frm.RefMain();

2:
frmMain frm = new frmMain();
frm.RefMain();

I can never seem to get the functions to call correctly between forms,
and I cannot make the function a static void, please help! :'(

Feb 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Feb 1, 7:14 pm, a...@goldews.com wrote:
I constantly have a delima calling functions inbetween forms, for
example in a project, in frmMain.cs , I have a function called
RefMain();, I would like to call it in frmAdd.cs, but none of the
following methods work:

1:
frmMain frm;
frm.RefMain();

2:
frmMain frm = new frmMain();
frm.RefMain();

I can never seem to get the functions to call correctly between forms,
and I cannot make the function a static void, please help! :'(
In my app I have a child form called Times (in Times.cs) which has
this function:
internal string MyFunc()
{
}

(note that it is internal so it can be accessed from anywhere the app)
then in my main form I can access it:
Times frmTimes = new Times();
frmTimes.MyFunc();

Hope this example helps.

Jon
dbgurus.com.au

Feb 1 '07 #2

P: n/a
I constantly have a delima calling functions inbetween forms, for
example in a project, in frmMain.cs , I have a function called
RefMain();, I would like to call it in frmAdd.cs, but none of the
following methods work:

1:
frmMain frm;
frm.RefMain();

2:
frmMain frm = new frmMain();
frm.RefMain();

I can never seem to get the functions to call correctly between forms,
and I cannot make the function a static void, please help! :'(
1. would not work, as "frm" is unassigned
2. will compile, but this will call "RefMain()" on a new instance of
your form.

I think you want to call that RefMain method on an existing form. This
means you will need a reference to that *existing* form. How you do it
really depends on your architecture, but basically you need to store
that reference somewhere, where other forms can find it.

Hans Kesting
Feb 1 '07 #3

P: n/a
I tried passing the instance of frmMain to frmNew when it's called:

private void newToolStripMenuItem_Click(object sender,
EventArgs e)
{
frmNew fmNew = new frmNew(this);
fmNew.Show();
}

However, in frmNew, I do not need to use my function in the load of
frmNew, i need to use it in a button on click, as follows:
public frmNew(frmMain frmM)
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{

As you can see, I declare what is being passed to frmNew, however, I
need to use the function in Add_Click, not frmNew(). Hope this clears
up my delima.

Feb 4 '07 #4

P: n/a
Smarter <ad**@goldews.comwrote:
I tried passing the instance of frmMain to frmNew when it's called:

private void newToolStripMenuItem_Click(object sender,
EventArgs e)
{
frmNew fmNew = new frmNew(this);
fmNew.Show();
}

However, in frmNew, I do not need to use my function in the load of
frmNew, i need to use it in a button on click, as follows:
public frmNew(frmMain frmM)
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{

As you can see, I declare what is being passed to frmNew, however, I
need to use the function in Add_Click, not frmNew(). Hope this clears
up my delima.
So keep a reference to the main form within the other form:

frmMain creator;

public frmNew(frmMain frmM)
{
InitializeComponent();
creator = frmM;
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 4 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.