从C#类更改Windows窗体标签 [英] Changing Windows Form Labels from C# Class

查看:364
本文介绍了从C#类更改Windows窗体标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



寻求更多帮助。



我的标签上有我希望在调用类的过程时更改的窗体。



Hi again all,

Seeking some more help.

I have a Label on my windows form that I would like to change when a procedure from a class is called.

private void StartCmd_Click(object sender, EventArgs e)
{
     Library lib = new Library();
     lib.StopService();
         lib.DeletePrefs();
}





在课堂上,我在DeletePrefs下面有以下内容





In the class I have the following under DeletePrefs

public void DeletePrefs()
{
    try
    {
    fmMain F = new fmMain();
    String StrFile;
    StrFile = @"C:\Program Files\X\Workspace\prefs.txt";
              
     if (File.Exists(StrFile))
     {
          F.pFileStatus.ForeColor = System.Drawing.Color.Black;
          File.Delete(StrFile);
          F.pFileStatus.Text = "Deleted";
                    
     }
     catch (Exception ex)
            {
                string strMessage = ex.Message;
MessageBox.Show("An error occured deleting prefs file " + strMessage, "Delete                      File Process", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
}





代码执行时工作正常,但标签永远不会更新。

有人可以解释如何在C#类中引用Windows窗体。



非常感谢



When the code executes it works fine, However the label never updates.
Can someone please explain how to reference Windows Forms in C# Classes.

Thanks kindly

推荐答案

更好的方法是将信息从库中返回到主程序/表单。然后更新窗体内的控件。通过这种方式,您可以使用松散耦合的体系结构,并且可以分离各个层。 Libray不需要知道您的表单,您可以重用您的库代码。尝试使用这种简单的方法:



Better way is to return information from your library to main program/form. Then update controls inside form. Doing this way you have loosely coupled architecture, and you can separate your layers. Libray doesn't need to know about your forms which and you can reuse your Library code. Try to use this simple approach:

public bool DeletePrefs()
{
    try
    {
        bool result = false; // Default method result
        String StrFile = @"C:\Program Files\X\Workspace\prefs.txt";
        if (File.Exists(StrFile))
        {
            File.Delete(StrFile);
            result = true;
        }
    }
    catch (IOException ex)
    {
        // We are catching only IO Exceptions
    }
    catch (...) // If you are expecting other exceptions inside this method you should handle them here.
    {

    }
    finally
    {
        // Optional clean up
    }
    return result;
}





打电话给你的班级应该是:



And calling your class should be:

private void StartCmd_Click(object sender, EventArgs e)
{
    Library lib = new Library();
    lib.StopService();
    if (lib.DeletePrefs())
    {
        pFileStatus.ForeColor = System.Drawing.Color.Black;
        pFileStatus.Text = "Deleted";
    }
    else
    {
        MessageBox.Show("Error while deleting prefs file");
    }
}







如果您需要返回其他错误信息,可以创建自定义例外或重新抛出标准例外以通知图书馆消费者。



更多信息你可以在这里找到:

http: //msdn.microsoft.com/en-us/library/vstudio/ms229042%28v=vs.100%29.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/vstudio/ms229014%28v = vs.100%29.aspx [ ^ ]



我希望你觉得这很有用:)




If you need to return additional error informations you can create your custom exceptions or rethrow standard exceptions to inform your library consumer about it.

More informations you can find here:
http://msdn.microsoft.com/en-us/library/vstudio/ms229042%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/vstudio/ms229014%28v=vs.100%29.aspx[^]

I hope you find this useful :)


第一或一个ll,你创建了一个新的fmMain实例。您正在修改表单的副本,而不是您在屏幕上看到的副本。



其次,您的图书馆不应该关注更改任何标签或其他控件。 fmMain类中的代码对此负责。如果继续你正在做的事情,你的Library类完全依赖于fmMain的存在。由于这个原因,您将无法以任何其他形式或任何其他项目使用该类。



您的库应该做的是暴露数据它试图通过活动或其他代表发送到表单。然后由表单来订阅这些事件并决定要显示的数据以及如何显示它。
First or all, you created a new instance of the fmMain. You're modifying a copy of the form, not the one that you see on screen.

Secondly, your library should NOT be concerned with changing any label or other control. The code in the fmMain class is responsible for that. If you continue with what you're doing, your Library class is completely dependent on the existence of fmMain. You will not be able to use the class in any other form or even any other project because of that.

What you're library should be doing is exposing the data it's trying to send to the form through events or other delegates. Then it's up to the form to subscribe to those events and decide what data to show and how to show it.


您的表单类应该实现名为的接口IHost 应至少有一个名为 RefreshAdterDelete()的方法,然后逻辑类将使用此接口与表单通信,因此您的逻辑类应该拥有 IHost 类型的构造函数的属性或参数,删除后必须调用接口的方法。
Your form class should implement an interface named IHost that should have at least one method named RefreshAdterDelete() and then the logic class will communicate with the form by using this interface, so your logic class should have a property or a param of its constructor of type IHost and after delete it has to invoke the method of the interface.


这篇关于从C#类更改Windows窗体标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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