通过方法调用将随机数返回给文本框.请建议
本文关键字:文本 返回 方法 调用 随机数 | 更新日期: 2023-09-27 18:13:34
这是基于一个控制台程序,我正试图为它做一个GUI。
请查看firstNumber_Click
和Arithmetic
firstRandomNumber()
有机会我可以得到一个解释,为什么它不返回随机数到文本框?
算术
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAI_GUI
{
class Arithmetic
{
public int firstNumber;
public int secondNumber;
public Arithmetic(int _firstNumber, int _secondnumber)
{
firstNumber = _firstNumber;
secondNumber = _secondnumber;
}
public Arithmetic()
{
}
public int firstRandomNumber()
{
Random rnd = new Random();
firstNumber = rnd.Next(0, 9);
return firstNumber;
}
public int secondRandomNumber()
{
Random rnd = new Random();
secondNumber = rnd.Next(0, 9);
return secondNumber;
}
public int FirstNumber
{
get
{
return firstNumber;
}
set
{
firstNumber = value;
}
}
public int SecondNumber
{
get
{
return secondNumber;
}
set
{
secondNumber = value;
}
}
}
}
Form4
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CAI_GUI
{
public partial class Form4 : Form
{
Arithmetic A1 = new Arithmetic();
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
A1.secondRandomNumber();
}
private void button2_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void firstNumber_Click(object sender, EventArgs e)
{
int first = Convert.ToInt32(num1.Text);
num1.Text = A1.firstRandomNumber().ToString();
}
}
}
我认为它不工作,因为行int first = Convert.ToInt32(num1.Text);
,因为它无法转换,试试这个。
private void firstNumber_Click(object sender, EventArgs e)
{
num1.Text = A1.firstRandomNumber().ToString();
}