Accessing methods between forms
I have Form1 and Form2. Form1 is the main form for the application, form2 is created by form1 when you click a button.
Inside Form2 I have a method called showmessage that is public and part of the Form2 class.
In form1, I create an instance of the form2 object and then call the show method and it pops up. Then I attempt to call the form2.showmessage function that I wrote but can't get to it.
What gives?
I'm new to C# and trying to get past this learning curve. Would appreciate any help you could give me.
Thanks
glenn
Scott LyslePosted Mar 8, 2007, 4:05 AM
It is interesting that the demo did not work for you; I just checked it, it works fine including the intellisense. As Mike Gold mentioned, the key to making this work is providing public access which is not the default condition.
Mike GoldPosted Mar 7, 2007, 6:22 PM
The way I talk between forms is to simply pass a pointer from form1 to form2 into the constructor and keep a reference to it.
e.g.
private Form1 _form;
Form2(Form1 form)
{
_form = form;
}
public void MyMethodToCallForm1()
{
_form.DoCalculation();
}
However looking back at your post, it might be that you forgot to make your form2 method have public access.
GlennPosted Mar 7, 2007, 8:58 AM
GlennPosted Mar 7, 2007, 8:54 AM
Scott LyslePosted Mar 7, 2007, 1:09 AM
http://www.c-sharpcorner.com/UploadFile/scottlysle/LimitedDataXfer01302007012304AM/LimitedDataXfer.aspx?ArticleID=98b894ff-2a4d-42b6-8aa7-7c0b210e001f