i want to call a method in FORM class from another class.
i tried all d ways i could but nothng workd.anyone please help me out........
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jean PaulRank #32 · 38.5K reputationPosted Dec 6, 2010, 5:47 AM EST
Jean PaulRank #32 · 38.5K reputationPosted Nov 30, 2010, 4:09 AM EST
Eg:
YourForm
{
public void MyMethod()
{
}
}
YourClass
{
public void AnotherMethod()
{
new YourForm().MyMethod();
}
}
Karthik AgarwalRank #2,270 · 620 reputationPosted Nov 30, 2010, 5:43 AM EST
I dont want to instantiate Form from another class, i just want to call two methods which are present in FORM class.In the first method call i want to enable one textbox,button present on the FORM,and in the second method call i just want to copy some text into some other textbox which is also present on the same FORM.
i tried the code u posted but the control is reaching both the methods in the FORM class and executing each and everry instruction in both the methods but i am not able to see the result in the sense neither the textbox,button are enabled nor the text got copied in to another textbox.
Jean PaulRank #32 · 38.5K reputationPosted Nov 30, 2010, 6:04 AM EST
Create a public static variable inside the form, and set the value in the constructor.
Example code is given below:
public partial class Form1 : Form
{
public static Form1 CurrentInstance;
public Form1()
{
InitializeComponent();
// Set the instance for global access
CurrentInstance = this;
}
}
After that create your two methods in the form and it should be public.
Let's assume the methods are m1() and m2() respectively.
From your class, you can invoke like this:
Form1.CurrentInstance.m1();
Form1.CurrentInstance.m2();
Please let me know if it works.. else attach your sample source so i can try fixing it.
Karthik AgarwalRank #2,270 · 620 reputationPosted Dec 6, 2010, 4:54 AM EST
Thanks vry much the code which u hav posted is working..........thanks a lot.......if i hav any queries i'll ask u.....no pblm right..........
Mahesh ChandRank #1 · 393.3K reputationPosted Dec 6, 2010, 5:03 AM EST
You need to make sure you write proper English grammar and don't use coded English grammar. Also, make sure title of a question is proper and it should be about the problem.
Thanks!
Karthik AgarwalRank #2,270 · 620 reputationPosted Dec 6, 2010, 8:59 PM EST
Ya i accpeted your answer......Thanks once again.........