C#中一个类怎么调用另个窗体上面的控件

2025-01-02 10:05:46
推荐回答(5个)
回答1:

先建一个cs文件
using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
public class Class1
{
private double TXT1;
public double txt1
{
get { return TXT1; }
set { TXT1 = value; }
}

private double TXT2;
public double txt2
{
get { return TXT2; }
set { TXT2 = value; }
}

private double SUM;

public double Math()
{
SUM = TXT1 + TXT2;
return SUM;
}

}
}

Button的代码

Class1 Class = new Class1();
Class.txt1 = double.Parse(textBox1.Text);
Class.txt2 = double.Parse(textBox2.Text);
textBox3.Text = Class.Math().ToString() ;

回答2:

public button_submit()
{textBox3.text=textBox1.text.trim()+textBox2.text.trim();}
窗体类名.控件名 记得要把控件设成public

回答3:

控件要是public的类型

窗体名.控件名

回答4:

3楼的好点 1 2楼影响数据的封装

回答5:

窗体类名.控件名 记得要把控件设成public