I have a forms application with multiple forms. How would I access a function on form 4 from form1?
4 replies to this topic
#1
Posted 28 December 2010 - 10:54 AM
|
|
|
#2
Posted 28 December 2010 - 06:29 PM
This question gets asked all the time, but I still find it complicated to get forms working with each other sometimes. Try looking at this post: c# - Find, Find Next? - Stack Overflow
Good Luck. :c-smile:
Good Luck. :c-smile:
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#3
Posted 29 December 2010 - 06:42 AM
you need to adjust the accessibility of that method, but the better question should be; "should you be accessing a method from one form on another?"
so long as you have reference to that form, from the first, you will be able to access that method.
And that's where people generally fall short, and is one of a few reasons why you need to ask yourself if you really should be doing this.
if you don't have reference to an instance of that particular form , AND the particular method you're trying to call, doesn't require any specific instance information, you can make the method static, which will allow you to access it by the type, rather than by the instance. example:
the difference between them is this.
first example:
second example:
//form code...
public void method(string input) {
...
}
so long as you have reference to that form, from the first, you will be able to access that method.
And that's where people generally fall short, and is one of a few reasons why you need to ask yourself if you really should be doing this.
if you don't have reference to an instance of that particular form , AND the particular method you're trying to call, doesn't require any specific instance information, you can make the method static, which will allow you to access it by the type, rather than by the instance. example:
//form code...
public static void method(string input) {
...
}
the difference between them is this.
first example:
Form2 form_two = new Form2();
Form2 form_three = new Form2();
form_two.method("input");
form_three.method("input");
second example:
Form2.method("input");
#4
Posted 29 December 2010 - 08:00 AM
Quote
"should you be accessing a method from one form on another?"
Quote
//form code...
public void method(string input) {
...
} so long as you have reference to that form, from the first, you will be able to access that method.And that's where people generally fall short, and is one of a few reasons why you need to ask yourself if you really should be doing this.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.
Science is only an educated theory, which we cannot disprove.
#5
Posted 29 December 2010 - 08:24 AM
It's all a matter of style I suppose, I personally view forms as a presentation device, I mean it's just do you hand in reference from one form as it's constructed? Maybe.. or do you keep reference to the forms from some known common place, like as part of a DOM or int he Program class (like where you're entry point is), also maybe..
It's just a matter of organization, thats why i made the comment, should you be doing this. In my opinion, only methods that pertain to a particular form should reside in there. If they are needed by more than one form, perhaps the form should inherent a base form which implements it... or perhaps the code should be moved into a functional module of some sort...
but on the note of public not making it accessible from another form. It will in fact, the rules are the same for any class in c# whether it inherits from object or System.Windows.Forms.Form
and if the method were static, it will have no issues accessing variables, so long as those variables are also static. a static method will have no knowledge of any instance of that type.
when you say you weren't sure if you did it right, its likely because you didn't have a point of reference to the second form. you were likely just saying Form2.Method <-- where Form2 is the class name, not the actual name of the Form2 variable. And this is again, why doing this is somewhat awkward.
It's just a matter of organization, thats why i made the comment, should you be doing this. In my opinion, only methods that pertain to a particular form should reside in there. If they are needed by more than one form, perhaps the form should inherent a base form which implements it... or perhaps the code should be moved into a functional module of some sort...
but on the note of public not making it accessible from another form. It will in fact, the rules are the same for any class in c# whether it inherits from object or System.Windows.Forms.Form
and if the method were static, it will have no issues accessing variables, so long as those variables are also static. a static method will have no knowledge of any instance of that type.
when you say you weren't sure if you did it right, its likely because you didn't have a point of reference to the second form. you were likely just saying Form2.Method <-- where Form2 is the class name, not the actual name of the Form2 variable. And this is again, why doing this is somewhat awkward.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









