Call a Method from Another Form

How to call function from another form

This worked for me: In your Program class, declare a static instance of Main (The class, that is) called Form. Then, at the beginning of the Main method, use Form = new Main(); So now, when starting your app, use
Application.Run(Form);

public static Main Form;

static void Main() {
Form = new Main();
Application.Run(Form)
}

Now, calling a function from another form is simple.

Program.Form.MasterReset();  //Make sure MasterReset is a public void

Calling method from another Form C#

if you want to use method for saving data than i suggest you make seperate class with the method for saving

Below is just example , you need something like this

public class DbCrud
{
public bool SaveData(DateSet ds)
{
//create data adapter object with required connection information
// and just call save here
sqlCnn.Open();
sqlCmd = new SqlCommand(sql, sqlCnn);
adapter.SelectCommand = sqlCmd;
adapter.Update(ds);
}
}

in form1 class

private DbCrud dbCrud;
public form1()
{
dbCrud =new DbCrud();
dbCrud.SaveData(ds);
}

in form 2 class

private DbCrud dbCrud;
public form2()
{
dbCrud =new DbCrud();
dbCrud.SaveData(ds);
}

Calling a method of a form inside another form

One method is that send the Form1 class as a parameter to the player class.

Like the following:



public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//...................
Player player = new Player(this);
player.Show();
}

Player Class

class Player
{

Form1 f;
public Player(Form1 _f)
{
f = _f;
}
public void Print()
{
f.SendLog();
}
}

Another way is to use Application.OpenForms which Gets a collection of open forms owned by the application.

var form1 = Application.OpenForms.OfType<Form1>().Where(x => x.Name == "formName").FirstOrDefault();
form1.SendLog();

or use

foreach (Form frm in Application.OpenForms)
{
if(frm.Name == "formName")
{
frm.SendLog();
break;
}
}


Call a method from another form

Instead of creating an instance of a new Form, you probably need an instance of already opened form and call the method from there. You can try:

if (System.Windows.Forms.Application.OpenForms["yourForm"] != null)
{
(System.Windows.Forms.Application.OpenForms["yourForm"] as Form1).Output();
}

plus you can replace calling the button3_Click(null,null) in your Output method, by placing the code of the event in a separate method and then calling that method against your button click event or your public output method

Calling a function in the main Form from another Class in C#

If you need to access the current instance of your main form, you could pass it along to the class:

public partial class Form1 : Form
{
internal void get_data(int mydata)//Change to internal or public, as default is private
{
//code
}

private void button1_Click(object sender, EventArgs e)
{
prog var1 = new prog();
var1.start_data(this);//pass along instance of your main form
}
}

public class prog
{
private Form1 MainForm;
public void start_data(Form1 form)
{
MainForm = form;//set form
Thread ct = new Thread(doSmt);
ct.Start();
}

private void doSmt()
{
int data = 40;
MainForm.get_data(data); //use form
}
}

How to call a function from another form

Your first example works if you pass the parameters expected by MySqlConnect.

public partial class Form2 : Form
{
Form1 mainForm;

public Form2(Form1 mainForm)
{
InitializeComponent();

this.mainForm = mainForm;
mainForm.MySQLConnect(this, new EventArgs());
}
}

However, I can't imagine what a function named MySqlConnect could do with the parameters passed.

So it is better to remove them in the method definition and do not pass anything when you call it.

And I agree with the comments above. Why do you hide such important (and often required functionality) inside a Form instance? You have to pass this form instance everywhere you need to connect to your database. It is better to prepare some static service class ( in a Database Access Layer) that carry on this job

C# Call a method from a different form class?

When you are working with multiple forms, you need to pass a reference to the second form, so that it "knows" about the first form. To do this you will need to change the constructor of your second form, and add a private reference to that form, like so:

class Form2 : Form
{
//Variables
private Form1 _ParentForm; //Add this here

//Constructor
public Form2(Form1 parentForm)
{
InitalizeComponent();
_ParentForm = parentForm; //Add this here
}
}

The, when you create the second form on your main form, you can use this to pass the reference of itself on to the new form:

class Form1 : Form
{

public void ChangeBack(Color clr) //No longer needs to be static
{
this.BackColor = clr;
}

public void CreateSecondForm()
{
Form2 secondForm = new Form2(this);
secondForm.Show();
}
}

Then you can call any function on the parent form (i.e. Form1) from the second form like so:

public void ChangBackColor_Click(object sender, EventArgs e)
{
if (ColorDialog.ShowDialog() == DialogResult.OK)
{
//Access Form1's reference with _ParentForm instead of Form1
_ParentForm.ChangeBack(ColorDialog.Color);
}
}

Call method on another form that is still open?

There's no need for the second form to know about the first form at all. Having it know about it complicates its code, and needlessly ties it to that form. Having another form knowing about the internal UI components of the main form is even worse; if you do that then changes to how the main form displays the data would break this other form. Just have the popup have a property representing the value that lets it be set/fetched externally:

public partial class Form2 : Form
{
public string Result //TODO give better name
{
get { return textBox.Text; }
}
public string DisplayText //TODO give better name
{
get { return label.Text; }
set { label.Text = value; }
}
}

Then the main form can set the display value, show the form, and fetch the resulting value:

Form2 popup = new Form2();
popup.DisplayText = "asdf";
popup.ShowDialog();
myField = popup.Result;


Related Topics



Leave a reply



Submit