c#win form [英] c# win form

查看:91
本文介绍了c#win form的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体,它有控件textbox1,textbox2和button1。我添加另一个类来计算textbox1,textbox2中两个数字的加法。但是无法从新添加的类访问winform中的控件。请帮我解决这个问题。我在c#中完成了这个程序。

谢谢!!

解决方案

你不应该那样做 - 计算类应该什么都没有与UI控件有关。相反,您应该在按下按钮时将值从UI传递到Calculation类 - 或者作为字符串,或者作为Calculate类理解的适当数据类型。您可以通过构造函数重载或通过属性执行此操作:

  public   class 计算
{
public string Operand1 { get ; set ; }
public string Operand2 { get ; set ; }
public 计算(字符串 operand1, string operand2)
{
Operand1 = operand1;
Operand2 = operand2;
}
public string 添加()
{
...
}
}

你可以添加一个带有操作数参数的重载静态Add方法:

  public   static   string 添加( string  operand1, string  operand2)
{
return ;
}





但是不要试图让Calculate类引用UI控件 - 这被认为是非常糟糕的做法!


I have a windows form which have controls textbox1,textbox2 and button1. I add another class to calculate the addition of both the numbers in textbox1,textbox2.But can't able to access the controls in winform from newly added class. Kindly help me to solve this problem. I did this program in c#.
Thanks !!

解决方案

You shouldn't do it like that - the Calculation class should have nothing to do with the UI controls. Instead, you should pass the values from the UI to the Calculation class when the button is pressed - either as strings, or as appropriate data types that the Calculate class understands. You can do this via a Constructor overload or via Properties:

public class Calculate
    {
    public string Operand1 { get; set; }
    public string Operand2 { get; set; }
    public Calculate(string operand1, string operand2)
        {
        Operand1 = operand1;
        Operand2 = operand2;
        }
    public string Add()
        {
        ...
        }
    }

and you could add an overloaded static Add method which takes operand parameters:

public static string Add(string operand1, string operand2)
    {
    return "";
    }



But don't try to have the Calculate class refer to the UI controls - that is considered very bad practice!


这篇关于c#win form的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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