c# - 如何通过来自另一个类的方法调用为控件分配一些值 [英] How to assign some value to a control through Method Call from another class in c#

查看:58
本文介绍了c# - 如何通过来自另一个类的方法调用为控件分配一些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个全局 Reset 类,该类重置表单中的所有值并调用该表单的初始方法.我将表单名称动态传递给 Reset 类并重置所有控件的值并调用该表单的方法.这是我的代码...

重置类

I am trying to make a global Reset class which reset all the values in a form and called the initial Method of that form. I pass the Form Name dynamically to Reset class and reset all the control's value and called the Method of that form. Here is my Code...

Reset Class

class Reset
{
    public void BlankAll(string FormName, Panel pnl, string UserName)
    {          
        foreach (Control ch in pnl.Controls)     // this foreach loop reset the values of each control, it's working fine. So don't bother about it.
        {
            if (ch.Name.Substring(ch.Name.Length - 3) != "_NI")
            {
                if (ch is TextBox || ch is ComboBox)
                    ch.Text = string.Empty;
                else if (ch is DateTimePicker)
                    ch.Text = DateTime.Now.ToShortDateString();
                else if (ch is DataGridView)
                {                        
                    DataGridView dgv = (DataGridView)pnl.Controls.Find(ch.Name, true).SingleOrDefault();
                    for (int i = dgv.Rows.Count - 1; i > 0; i--)
                    {
                        dgv.Rows.RemoveAt(i);
                    }
                    for (int i = 1; i < dgv.Columns.Count; i++)
                    {
                        dgv.Rows[0].Cells[i].Value = string.Empty;
                    }

                }
            }
        }            

        Type tp = Type.GetType("HealthClub." + FormName);     // HealthClub is NameSpace
        object myobj = Activator.CreateInstance(tp);
        MethodInfo method = myobj.GetType().GetMethod("FillValues");   // FillValues is the method which I am trying to call
        object[] parametersArray = new object[] { UserName };
        method.Invoke(myobj, parametersArray);    

    }
}

这是 Form1 中的 FillValues 方法,我想从 Reset 类中调用它

Here is FillValues Method in Form1 which I want to Invoke from Reset class

public void FillValues(string UserName)
{
    DataTable DT;
    SqlCommand cmd = new SqlCommand();
    try
    {
        cmd.Connection = Connections.Connection[UserName];
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "TrainerMaster_pro";
        cmd.Parameters.AddWithValue("Option", "FillValues".Trim());
        if (Connections.Connection[UserName].State == ConnectionState.Closed)
            Connections.Connection[UserName].Open();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DT = new DataTable();
        adp.Fill(DT);                
        lblId___.Text = DT.Rows[0][0].ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        cmd.Parameters.Clear();
        cmd.Dispose();
        Connections.Connection[UserName].Close();                
    }
}

现在我试图通过 Reset 类从 Form2 重置 Form1.所以我在 Form2

Now I am trying to Reset Form1 from Form2 through Reset class. So I am doing this in Form2

private void btnReset_Click(object sender, EventArgs e)
{
    Reset RS = new Reset();
    RS.BlankAll("Form1", pnl, UserName);
}

一切正常,方法 FillValues 调用成功,但主要问题是数据库值未分配给 FillValues 方法中的 lblId___.我理解这一点,因为在我的 Reset 类中,我正在为我传递的表单创建新对象.有些人建议我使用按引用调用来调用方法.

Everything is working fine and Method FillValues invoke successfully but the main problem is database value is not assigning to lblId___ in FillValues Method. I understand this because in my Reset class I am creating the new object for the form which I am passing. Some people suggest me to Invoke method using call by reference.

怎么做?

请大家帮忙....

推荐答案

在 BlankAll 中,您传递表单的名称,即表单本身.所以你的签名应该是这样的

At BlankAll, you pass the name of the form, the the form itself. so your signature should look like this

class Reset
{
    public void BlankAll(Form form, Panel pnl, string UserName)
    {          
        foreach (Control ch in pnl.Controls) 
        {
            if (ch.Name.Substring(ch.Name.Length - 3) != "_NI")
            {
                if (ch is TextBox || ch is ComboBox)
                    ch.Text = string.Empty;
                else if (ch is DateTimePicker)
                    ch.Text = DateTime.Now.ToShortDateString();
                else if (ch is DataGridView)
                {                        
                    DataGridView dgv = (DataGridView)pnl.Controls.Find(ch.Name, true).SingleOrDefault();
                    for (int i = dgv.Rows.Count - 1; i > 0; i--)
                    {
                        dgv.Rows.RemoveAt(i);
                    }
                    for (int i = 1; i < dgv.Columns.Count; i++)
                    {
                        dgv.Rows[0].Cells[i].Value = string.Empty;
                    }
                }
            }

     Type tp = Type.GetType("HealthClub." + form.Name);
     object myobj = Activator.CreateInstance(tp);
     MethodInfo method = myobj.GetType().GetMethod("FillValues");   
     object[] parametersArray = new object[] {form, UserName };
     form.Refresh();
        }            

在调用 fill 时,您应该传递表单,以便该方法知道它应该更新哪个"标签

When Calling the fill you should pass the form so the method will know "which" label it should update

public void FillValues(Form form, string UserName)
{
   DataTable DT;
   SqlCommand cmd = new SqlCommand();
   try
   {
       cmd.Connection = Connections.Connection[UserName];
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.CommandText = "TrainerMaster_pro";
       cmd.Parameters.AddWithValue("Option", "FillValues".Trim());
       if (Connections.Connection[UserName].State == ConnectionState.Closed)
            Connections.Connection[UserName].Open();
       SqlDataAdapter adp = new SqlDataAdapter(cmd);
       DT = new DataTable();
       adp.Fill(DT);                
       form.lblId___.Text = DT.Rows[0][0].ToString();
     }
     catch (Exception ex)
     {
       MessageBox.Show(ex.ToString());
     }
     finally
     {
       cmd.Parameters.Clear();
       cmd.Dispose();
       Connections.Connection[UserName].Close();                
     }
}

这篇关于c# - 如何通过来自另一个类的方法调用为控件分配一些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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