从C#中的Static方法访问TextBox?

时间:2012-08-26 03:57:30

标签: c# forms static

我有两个类:MyForm和数据库

在MyForm中,我有一种方法可以更改标签文本以显示错误:

public void printError(string text){
    label1.Text = text;
}

我的数据库类也需要访问该方法,所以我将其设为静态:

public static void printError(MyForm form, string text){
    form.label1.Text = text;
}

现在的问题是,如何从数据库类调用该方法?

This question我发现我需要将MyForm传递给数据库的构造函数,如下所示:

class MyForm : Form{
    Database db;
    public Form(){
        db = new Database(this);
    }
}

class Database{
    MyForm form;
    public Database(MyForm f){
        form = f;
    }
    ...
    //then I can access the printError like this
    MyForm.printError(form, "You got error");
}

我试过了,冻结了表单。还有其他解决办法吗?

由于

4 个答案:

答案 0 :(得分:2)

就像@Matthew Ferreira和其他人所说的那样,设计并不是一个想法,但这里有一些东西可以让你开始。

class MyForm : Form
{
    public void SomeMethod()
    {
        var dataAccess = new Repository();

        dataAccess.ExecuteQuery();

        if (dataAccess.Exceptions.Any())
        {
            // display your error messages
            form.label1.Text = dataAccess.Exceptions.Select(x => x.ToString());
        }
    }
}

class Repository
{
    private readonly HashSet<Exception> _exceptions = new HashSet<Exception>();

    public IEnumerable<Exception> Exceptions
    {
        get { return _exceptions; }
    }

    public int ExecuteQuery()
    {
        var numberOfRecordsAffected = 0;

        try
        {
            // do something
        }
        catch (Exception ex)
        {
            // normall catching exceptions is a bad idea
            // and you should really catch the exception at the 
            // layer best equiped to deal with it
            _exceptions.Add(ex);
        }

        // but, for the purpose of this example we might want to add some logic to try the query on another database ????
        try
        {
            // do something
        }
        catch (Exception ex)
        {
            _exceptions.Add(ex);
        }

        return numberOfRecordsAffected;
    }
}

答案 1 :(得分:2)

这是一个非常简单的示例,说明如何在数据层不知道您的UI的情况下实现此目的:

class MyForm : Form
{
    Database db;

    public Form()
    {
        db = new Database(this);
    }

    public void DoSomething()
    {
        var errors = db.Login("", "");
        if (errors.Any())
            label1.Text = errors.First(); // Or you can display all all of them
    }
}

class Database
{    
    public List<string> Login(string username, string password)
    {
        var errors = new List<string>();

        if (string.IsNullOrEmpty(username))
            errors.Add("Username is required");

        if (string.IsNullOrEmpty(password))
            errors.Add("Password is required");

        [...]

        return errors;
    }
}

答案 2 :(得分:1)

您需要查找“关注点分离”。将UI代码与数据库访问层(DAL)混合在一起真的很糟糕。最好将UI绑定到通过DAL填充的业务对象。

要让UI知道错误,您只需使用委托。

namespace OperationErrorDelegate
{
    public delegate void OperationErrorHandler(Exception ex);

    public class DAL
    {
        public event OperationErrorHandler ReportError;

        public void DoDALOperationThatCausesError()
        {
            try
            {
                int i = 1;
                int j = 0;
                int k = i/j;
            }
            catch (Exception ex)
            {
                ReportError(ex);
            }
        }
    }
}

将此代码添加到表单中:

using System ;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            DAL DAL = new DAL();
            DAL.ReportError += new OperationErrorHandler(DAL_OperationErrorProgress);
            DAL.DoDALOperationThatCausesError();
        }

    private void DAL_OperationErrorProgress(Exception ex)
    {
        label1.Text = ex.Message;
    }
}

}

答案 3 :(得分:0)

假设OP的要求是在标签中显示错误消息,当凭据错误时:

   private void btn_login_Click(object sender, EventArgs e)
    {
         MySqlConnection con = new MySqlConnection("server=localhost;uid=root;password=abc;database=mydb");
         MySqlCommand cmd = new MySqlCommand("select * from emp where name='" + textBox1.Text + "'and pwd='" + textBox2.Text + "'",con);
         con.Open();
         MySqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read()) 
        {    //successful
            //navigate to next page or whatever you want
        }
        else
            Label1.Text("Invalid userid or password");
        con.Close();
    }

如果您需要error message for wrong data type (the user input string but the database column is Integer),请在客户端使用验证。你不需要在后端做这件事,因为这将是一个负担。 您可以在button_click本身中使用正则表达式。