C#中自定义类如何使用其他类控件

zhaokechun 2011-09-15 12:26:16
我生成了一个C#的windowsapplication,原本就有一个Form1类。在Form1中添加了两个label控件。
现在自定义了一个Test类,想在Test类中写一个SetNumber函数,函数功能为修改Form1中两个label的值,生成两个随机
整数(int),然后赋值给两个label。请问函数如何写,怎么在form1中调用SetNumber()函数。
...全文
526 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Herbert 2011-09-15
  • 打赏
  • 举报
回复
1.加参数
public static void SetNumber(Label label1,Label label2)
{
Random random = new Random();
label1.Text = random.Next(0, 100).ToString();
label2.Text = random.Next(0, 100).ToString();
}

2.或者 设置 label1,label2 可见性(Modifiers) 为 public
public static void SetNumber()
{
Random random = new Random();
Form1.label1.Text = random.Next(0, 100).ToString();
Form1.label2.Text = random.Next(0, 100).ToString();
}
threenewbee 2011-09-15
  • 打赏
  • 举报
回复
class Test
{
public static void SetNumber(Form1 f)
{
Random r = new Random();
int x = r.Next(0, 10);
int y = r.Next(0, 10);
f.label1.Text = x.ToString();
f.label2.Text = y.ToString();
}
}

调用
Test.SetNumber(this);
rinall 2011-09-15
  • 打赏
  • 举报
回复
把Form1中两个label设成public
在Form1中 Test t = new Test();
t.SetNumber();
threenewbee 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhaokechun 的回复:]
引用 2 楼 caozhy 的回复:
C# code
class Test
{
public static void SetNumber(Form1 f)
{
Random r = new Random();
int x = r.Next(0, 10);
int y = r.Next(0, 10);
f.label1.Text = x.To……

想请教为什么我把函数放在这不……
[/Quote]
InitializeComponent();的作用是创建界面上那些按钮标签,在此之前调用,这些还没有创建好呢.
zhaokechun 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 caozhy 的回复:]
C# code
class Test
{
public static void SetNumber(Form1 f)
{
Random r = new Random();
int x = r.Next(0, 10);
int y = r.Next(0, 10);
f.label1.Text = x.To……
[/Quote]
想请教为什么我把函数放在这不可以?
public Form1()
{
Test.SetNumber(this);
InitializeComponent();
}
zhaokechun 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chenzhuomin 的回复:]
这样编程的思路很混乱,可以在Test类中实现产生随机数的方法(GetNumber()),在Form1中适当的地方调用该方法,把获得的数值赋给Lable.Text。如:
public class Test
{
public static int GetNumber()
{
//在这里实现产生随机数。
}
}

public class Form1:Form
{
……
[/Quote]

我在Form1中还有一个按钮 希望每次单击该按钮时都从新刷新两个label的随机数 如何搞
chenzhuomin 2011-09-15
  • 打赏
  • 举报
回复
这样编程的思路很混乱,可以在Test类中实现产生随机数的方法(GetNumber()),在Form1中适当的地方调用该方法,把获得的数值赋给Lable.Text。如:
public class Test
{
public static int GetNumber()
{
//在这里实现产生随机数。
}
}

public class Form1:Form
{
void Form1_Loaded(object sender,EventArgs e)
{
this.Lable1.Text = Test.GetNumber().tostring();
this.Lable2.Text = Test.GetNumber().tostring();
}
}
threenewbee 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhaokechun 的回复:]
引用 2 楼 caozhy 的回复:
C# code
class Test
{
public static void SetNumber(Form1 f)
{
Random r = new Random();
int x = r.Next(0, 10);
int y = r.Next(0, 10);
f.label1.Text = x.To……


大哥 我的方法和你一摸一……
[/Quote]
当然可以。
zhaokechun 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 caozhy 的回复:]
C# code
class Test
{
public static void SetNumber(Form1 f)
{
Random r = new Random();
int x = r.Next(0, 10);
int y = r.Next(0, 10);
f.label1.Text = x.To……
[/Quote]

大哥 我的方法和你一摸一样 请问调用的时候放在哪里 我想放在button单击事件中可以吗

111,086

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧