Click here to Skip to main content
15,891,745 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to call method in different classes/Forms or i want to call method from form1 to Form3/different Forms
here is the class in which method is...

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SMS;
using System.Data.OleDb;
using System.IO.Ports;
using WindowsFormsAppDAL;
using SMS;



namespace WindowsFormsAppDAL
{
    public partial class Form1 : Form

    {
        //ModifyRegistry MR = new ModifyRegistry();
        public Form1()
        {
            //Form2 fm2 = new Form2();
            //fm2.Show();

            InitializeComponent();


        }



        ClsMgmt CM = new ClsMgmt();
        static int position = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            //Form2  fm2= new Form2();
            //fm2.Show();
            ShowData();
            //loadPorts();
        }



        public void ShowData()
        {

            GVD.DataSource = CM.GetData("select * from Students");
        }

        private void GVD_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tbID.Text = GVD.Rows[e.RowIndex].Cells[0].Value.ToString();
            tbName.Text = GVD.Rows[e.RowIndex].Cells[1].Value.ToString();
            tbFtaherName.Text = GVD.Rows[e.RowIndex].Cells[2].Value.ToString();
            tbNumber.Text = GVD.Rows[e.RowIndex].Cells[3].Value.ToString();
            tbEmail.Text = GVD.Rows[e.RowIndex].Cells[4].Value.ToString();
            tbaddress.Text = GVD.Rows[e.RowIndex].Cells[5].Value.ToString();

        }


        private void BtnNewRecord_Click(object sender, EventArgs e)
        {
            tbID.Text = string.Empty;
            tbName.Text = string.Empty;
            tbFtaherName.Text = string.Empty;
            tbNumber.Text = string.Empty;
            tbEmail.Text = string.Empty;
            tbaddress.Text = string.Empty;
           // tbID.Text.FirstOrDefault();
            BtnDel.Enabled = false;
            BtnNext.Enabled = false;
            BtnPrivious.Enabled = false;
            BtnSave.Enabled = true;
            BtnUpdate.Enabled = false;

        }


        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int succ = CM.SetData("insert into Students (Name , Fathers_Name , Mobile_Number , Email , Address ) values ( '" + tbName.Text + "'  , ' " + tbFtaherName.Text + " ' ,  '" + tbNumber.Text + "' , ' " + tbEmail.Text + " ' , ' " + tbaddress.Text + " ') ");


                if (succ > 0)
                {

                    MessageBox.Show("Record inserted ");

                    tbID.Text = string.Empty;
                    tbName.Text = string.Empty;
                    tbFtaherName.Text = string.Empty;
                    tbNumber.Text = string.Empty;
                    tbEmail.Text = string.Empty;
                    tbaddress.Text = string.Empty;

                    BtnPrivious.Enabled = true;
                    BtnSave.Enabled = false;
                    BtnNext.Enabled = true;
                    BtnUpdate.Enabled = true;
                    BtnDel.Enabled = true;
                    BtnUpdate.Enabled = true;

                }
                ShowData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please fill all entries properly!!!...", ex.Message);
            }

        }



        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            CM.SetData("Update  Students  set Name , Fathers_Name , Mobile_Number , Email , Address where Id=");
            ShowData();
        }


            public void next(int row)
            {
               tbID.Text = GVD.Rows[row].Cells[0].Value.ToString();
               tbName.Text = GVD.Rows[row].Cells[1].Value.ToString();
                tbFtaherName.Text = GVD.Rows[row].Cells[2].Value.ToString();
                tbNumber.Text = GVD.Rows[row].Cells[3].Value.ToString();
                tbEmail.Text = GVD.Rows[row].Cells[4].Value.ToString();
                tbaddress.Text = GVD.Rows[row].Cells[5].Value.ToString();
                position = row;


            }

        private void BtnNext_Click(object sender, EventArgs e)
        {

            if (position < GVD.Rows.Count - 1)
            {
                next(position + 1);
            }
        }

        private void BtnPrivious_Click(object sender, EventArgs e)
        {
              if (position>0)
            {
                next(position - 1);
            }
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are You sure?", "Message from Application Server", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

                int succ = CM.SetData("Delete from Students where Id=" + tbID.Text);

                if (succ > 0)
                {
                    MessageBox.Show("Recored Deleted Successfully");
                    tbID.Text = string.Empty;
                    tbName.Text = string.Empty;
                    tbFtaherName.Text = string.Empty;
                    tbNumber.Text = string.Empty;
                    tbEmail.Text = string.Empty;
                    tbaddress.Text = string.Empty;


                }

                ShowData();

            }
        }


    //this is the method which i want to use in other class/or in other Form

        private void loadPorts()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                cboPorts.Items.Add(port);
            }
        }







this is the other class that which i want to call in that method(loadports();)

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS;
using System.IO.Ports;
using System.Threading;

namespace WindowsFormsAppDAL
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();


        }
        Form3 f3 = new Form3();
Posted
Updated 1-Dec-12 9:23am
v4

Solution 1
C#
public void loadPorts() //public
{
......
}
//CALL
Form1 frm = new Form1();
frm.loadPorts();

Solution 2
C#
public static void loadPorts() //public static
{
 .....
}
//CALL
Form1.loadPorts();
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Dec-12 0:46am    
Forms are probably in the same assembly, so you don't need public, should use "internal". Never give more access them it is actually required. Also, in big applications, this is too tightly bound. Interface is a better solution...

Please see my answer.
--SA
Member 9411249 3-Dec-12 8:48am    
its not working its give error"Does not contain a definition for loadports and no extension methods" :(
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Hi dear, Check this information.

Your declarative function name is " private void loadPorts() " but form1 function used another form then function name is declare as " public void loadPorts() " not declare " private void loadPorts() " .

C#
public void loadPorts()
{
   string[] ports = SerialPort.GetPortNames();
   foreach (string port in ports)
   {
     cboPorts.Items.Add(port);
   }
}


//--------------------------------------------------------------------------

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS;
using System.IO.Ports;
using System.Threading;

namespace WindowsFormsAppDAL
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

private void Form2 _Load(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
    f3.loadPorts();
}
 
Share this answer
 
v2
Comments
Member 9411249 3-Dec-12 8:47am    
its not working its give error"Does not contain a definition for loadports and no extension methods" :(

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