Jump to content

From this form to that Form

- - - - -

  • Please log in to reply
4 replies to this topic

#1
williamevanl

williamevanl

    Learning Programmer

  • Members
  • PipPipPip
  • 61 posts
I have a forms application with multiple forms. How would I access a function on form 4 from form1?

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
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:
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
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?"

//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
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts

Quote

"should you be accessing a method from one form on another?"
I see no problems calling a function from Form1 in Form4.

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.
From what I remember(And I havnt used C# in about 6 months :c-whistle:) declaring a function as "public" doesnt allow you to access it from another form(pretty sure ive tried it, may have done it wrong), although declaring it "static" does allow you to access functions across forms is doesnt allow variables and stuff.(Once again I cant remember been a while, but i remember not being able to do stuff i needed because it was not allowed in a static function.)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#5
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users