Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making desktop application using C#.NET,SQL SERVER.
I have created two forms and i want that the method defined in one form should work in 2nd form, when that method called in 2nd form.

Please help me in this regard.

Thanks in Advance.
Posted
Updated 8-May-11 23:10pm
v2

Most robust and safe approach is implementing and interface by the form.
Please see my past solution:
How to copy all the items between listboxes in two forms[^]

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 9-May-11 15:49pm    
5ed!
Sergey Alexandrovich Kryukov 9-May-11 21:39pm    
Thank you, Espen.
--SA
fjdiewornncalwe 10-May-11 13:07pm    
Absolutely. +5.
I'm not sure why everyone is missing the point that we're talking about running operational logic in a UI object. That simply breaks the fundamental rules of software development surrounding combining business and UI logic.
Sergey Alexandrovich Kryukov 10-May-11 14:14pm    
Thank you, Markus.
You see, some fundamental rules are broken in first place by most our inquirers. It's important to note, that ad-hoc programming is highly stimulated by the tools like Designer. For example, I support the discipline when adding events by the click in designer is not allowed, because it mixes up functionality with things which should be separated, such as layout. The rule-breaking is well provoked, because its seems to be better for marketing (demonstrating the build ad-hoc application in seconds during a presentation). In practice, the acceptance of ad-hoc style depends on the size of the project. If the project is a small demo, it remains manageable. With more serious projects, using the same techniques kills manageability -- "It's a trap!".
--SA
yesotaso 10-May-11 15:43pm    
A well deserved 5. One statement pops in my mind "command pattern"... Anyway it is matter of habits I guess. What you did almost always affect what you'd do.
Yes you can. Make that method public (or better internal). In the other form code, create an instance of the first class and call that method using the instance.


That's a basic thing. I suggest you to get a good beginner book and invest some time studying it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 11:16am    
Good for a dirty solution. (Good that you mentioned "internal", why people fail to use it?)
Please see the more robust solution.
--SA
Ankur\m/ 10-May-11 1:52am    
I won't call it a dirty solution. I have not done a lot of windows programming, but this is one the approach mostly used.
I read your solution. You have suggested creating an Interface to access a method in one form from another form. Well I like the overall idea but is it feasible here? I mean if you decide these things well in advance at design time, it's fine, now that user has two forms, adding an interface and writing implementation in both the forms, doesn't sound very good to me. And what about all the OOP considerations?
Or did I not understand your approach well?
Sergey Alexandrovich Kryukov 10-May-11 2:17am    
Well, it simply depends on now much stuff the whole project has. It should be manageable. If it is manageable because it's just small, I agree with you. We don't know. My solution can be also considered dirty compared to something. I don't really understand all that buzz about form cooperation. There are a lot of cooperation between a lot of things, but somehow the beginner face with form cooperation. Look at other solutions in my references -- just public/internal was not even considered. I simply added to the set something that I find beneficial. By the way, more real problem is not even the access level (developers usually make it overly open), but passing the instance. Passing the instance as interface reference seems "ideologically" correct. A form should not know a partner is a form.
By the way, I up-voted this solution if first place, by 4.
--SA
Ankur\m/ 10-May-11 2:40am    
Passing the instance as interface reference seems "ideologically" correct. A form should not know a partner is a form.
I agree.

By the way, I up-voted this solution if first place, by 4.
I know you did that. And I did not ask this because of vote. I didn't know about "this" way of Class communication and wanted to know how feasible it is (and more importantly in this scenario).
BTW you get my 5 in the answer where you explained it.
PS: The link is wrong. Your answer is answer2. But it points to answer3.
Sergey Alexandrovich Kryukov 10-May-11 13:00pm    
Sure.
Thank you for the note on the reference. By the way, I saw a warning that the links to the answers are not permanent; so it's better to refer to answers.
--SA
Make that function as public/internal in Form1.
public string CallForm1()
{

}

Then, access the function like below in Form2.
Form1 f = new Form1 ();
f.CallForm1();

EDIT:Modified based on Ankur suggestion
 
Share this answer
 
v2
Comments
Ankur\m/ 9-May-11 5:25am    
Right, that's one of the method and you beat me to it. But the method shouldn't be necessarily declared public. 4!
Toniyo Jackson 9-May-11 5:29am    
Yeah. We can use internal also. Since OP is a beginner i don't want to confuse him.
Ankur\m/ 9-May-11 5:38am    
Giving a correct answer won't confuse him but guide him in a right direction.
Toniyo Jackson 9-May-11 5:42am    
Yeah. Modified :)
Sergey Alexandrovich Kryukov 9-May-11 11:15am    
Good for a dirty solution.
Please see mine.
--SA
Yes you can easuly use...
The thing is that you need to make the function public....
 
Share this answer
 
In first form make the function public & static
public static void Print()
        {
            Console.WriteLine("first form");
        }




In second form call the function by use this code
Form1.Print();


Enjoy
 
Share this answer
 
Comments
Ankur\m/ 9-May-11 6:13am    
Making a method static would help but by design it may be wrong. Static methods belong to the class. A static method CANNOT access instance fields in the same class or structure, and a static method cannot call instance methods.
So before you attach a static keyword to any method, you must first decide the usage of the method.
Sergey Alexandrovich Kryukov 9-May-11 11:17am    
Agree. This is just a wrong answer, misleading.
--SA
eshaq 9-May-11 13:45pm    
just I try to help, It's what I know.
Ankur\m/ 10-May-11 0:06am    
What you know is wrong. Read my comments and other answers. Hope it helps you as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900