想在其他类/表格中调用方法 [英] want to call method in other class/Form

查看:59
本文介绍了想在其他类/表格中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不同的类/表单中调用方法,或者我想调用从form1到Form3的方法/不同的表格

这里是方法的类...



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...

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);
            }
        }















这是我想用该方法调用的另一个类(loadports();)










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

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();

推荐答案

解决方案1 ​​

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



解决方案2


Solution 2

public static void loadPorts() //public static
{
 .....
}
//CALL
Form1.loadPorts();


这是关于表单协作的流行问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
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


亲爱的,请查看此信息。



您的声明性函数名称是private void loadPorts()但是form1函数使用了另一种形式,然后函数名声明为public void loadPorts() not declareprivate void loadPorts()。



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() " .

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





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





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

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();
}


这篇关于想在其他类/表格中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆