我正在尝试创建一个numericUpDown.value列表,当我按下按钮时,它将设置来自演示者的随机值;但是,当我按下按钮时,什么都没发生,我看到了数值的变化,但是我认为它只是用数值而不是设定值代替了数字控件。
视图:
public partial class Form1 : Form, IForm1
{
public List<decimal> _valueList { get; set; }
public Form1()
{
InitializeComponent();
this._valueList = new List<decimal> { numericUpDown1.Value, numericUpDown2.Value, numericUpDown3.Value, numericUpDown4.Value, numericUpDown5.Value, numericUpDown6.Value };
}
public List<decimal> ValueList
{
get => _valueList; set => _valueList = value;
}
public event EventHandler ButtonClick;
private void button1_Click(object sender, EventArgs e)
{
ButtonClick?.Invoke(sender, e);
}
}
接口:
public interface IForm1
{
List<decimal> ValueList { get; set; }
event EventHandler ButtonClick;
}
演示者:
public class PresenterForm1
{
private IForm1 _form1;
public PresenterForm1(IForm1 form1)
{
_form1 = form1;
form1.ButtonClick += ButtonClick;
}
private void ButtonClick(object sender, EventArgs e)
{
var random = new Random();
for(int i = 0; i < 6; i++)
{
_form1.ValueList[i] = random.Next(0, 10);
}
}
如果我将类型更改为List<NumericUpDown>
,则可以使用,但是我认为这是错误的方法:
foreach (var item in _form1.ValueList)
{
item.Value = random.Next(0, 99);
}