Stupid question...

A

adam

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! :'(
 
J

JonBosker

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
 
H

Hans Kesting

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
 
S

Smarter

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.
 
J

Jon Skeet [C# MVP]

Smarter said:
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;
}
 

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