[英]How do I pass multi dimensional array from parent to the controls in child form?
I have a parent form and four child forms,我有一个父表格和四个孩子 forms,
In each child form, there are 28 labels to fill,在每个子表单中,有 28 个标签要填写,
The parent form computes the value in the array父窗体计算数组中的值
UInt16[,] _array = new UInt16[4, 28]; UInt16[,] _array = 新 UInt16[4, 28]; then send the value for each child
然后发送每个孩子的价值
How do I send the values如何发送值
I have set the TabIndex of 28 labels to(0~27) in property我在属性中将 28 个标签的 TabIndex 设置为(0~27)
public partial class ChildTest : Form
{
public List<Control> Cell_Volt1 = new List<Control>();
public ChildTest()
{
InitializeComponent();
}
private void ChildTest_Load(object sender, EventArgs e)
{
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i].TabIndex < 28)
{
Cell_Volt1.Add(tableLayoutPanel1.Controls[i]);
tableLayoutPanel1.Controls[i].Text = "001x";
}
}
}
}
UInt16[,] _array = new UInt16[4, 28]{//Some Values}
if (Application.OpenForms["ChildTest"] is ChildTest childForm)
{
childForm.Focus();
return;
}
childForm = new ChildTest();
Your question is how to pass a multidimensional array to your ChildTest
forms.您的问题是如何将多维数组传递给您的
ChildTest
forms。 My suggestion is to pass it in the form of data bindings.我的建议是以数据绑定的形式传递它。 This way, your
MainForm
can set values using a statement like _array[1][5] = 0xff
and this will automatically display the value at the correct position on the right form.这样,您的
MainForm
可以使用像_array[1][5] = 0xff
这样的语句来设置值,这将自动在正确的表单上正确的 position 显示值。 This walk-through will demonstrate how to connect these in a one-time setup.本演练将演示如何在一次性设置中连接它们。 First a couple of usage examples in the
MainForm
:首先是
MainForm
中的几个用法示例:
private void buttonClear2_Click(object sender, EventArgs e)
{
for (int cell = 0; cell < 28; cell++) _array[1, cell] = 0x00;
}
private void buttonTest1_Click(object sender, EventArgs e) => _array[0,0] = 0xFF;
Observable UInt可观察的 UInt
The bindings are based on a uint
value that sends a notification whenever its value changes.绑定基于一个
uint
值,该值在其值更改时发送通知。 This class works behind the scenes to keep everything synced.这个 class 在幕后工作以保持一切同步。
public class ObservableUInt : INotifyPropertyChanged
{
uint _Value = 0;
public uint Value
{
get => _Value;
set
{
if (!Equals(_Value, value))
{
_Value = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Formatted)));
}
}
}
public string Formatted => $"0x{_Value.ToString("X2")}";
public event PropertyChangedEventHandler PropertyChanged;
public static implicit operator uint(ObservableUInt @this) => @this._Value;
}
Multidimensional array class多维数组class
The multidimensional array provides a standard array indexer that gets and sets a uint
value using _array[bank][index] = 0xnn
.多维数组提供了一个标准数组索引器,它使用
_array[bank][index] = 0xnn
获取和设置一个uint
值。 Under the hood are arrays made from these observable uints but it makes no difference in terms of interacting with the multidimensional array.引擎盖下是 arrays 由这些可观察的 uint 制成,但在与多维数组交互方面没有区别。
public class MultidimensionalArray
{
public MultidimensionalArray()
{
for (uint i = 0; i < 112; i++)
{
_array[i / 28].Add(new ObservableUInt());
}
}
public uint this[int bank, int index]
{
get => _array[bank][index];
set => _array[bank][index].Value = value;
}
public uint this[uint bank, uint index]
{
get => _array[bank][(int)index];
set => _array[bank][(int)index].Value = value;
}
public ObservableUInt[] this[int bank] => _array[bank].ToArray();
ObservableCollection<ObservableUInt>[] _array =
Enumerable.Range(0, 4)
.Select(_ => new ObservableCollection<ObservableUInt>())
.ToArray();
}
ChildTest
form class ChildTest
表格 class
This is a Form
with a TableLayoutPanel
on it that is unremarkable except for the addition of a DataSource
property.这是一个带有
TableLayoutPanel
的Form
,除了添加了一个DataSource
属性外,它是不起眼的。 It is here that the observable uints are bound to the textbox values.正是在这里,可观察的 uint 被绑定到文本框值。
public partial class ChildTest : Form
{
public ObservableUInt[] DataSource
{
set
{
if (value != null)
{
for (int i = 0; i < 28; i++)
{
var row = (i / 7) * 2;
var column = i % 7;
// Add Label
var label = new Label
{
Size = new Size(width: 150, height: 50),
Text = $"{i + 1}",
TextAlign = ContentAlignment.MiddleCenter,
BackColor = Color.DimGray,
ForeColor = Color.White,
Anchor = (AnchorStyles)0xf,
};
tableLayoutPanel.Controls.Add(label, column, row);
row++;
// Add Textbox and bind the Formatted property to it
var textbox = new TextBox
{
Size = new Size(width: 150, height: 50),
TextAlign = HorizontalAlignment.Center,
};
tableLayoutPanel.Controls.Add(textbox, column, row);
textbox.DataBindings.Add(
nameof(Label.Text),
value[i],
nameof(ObservableUInt.Formatted),
false,
DataSourceUpdateMode.OnPropertyChanged
);
}
}
}
}
}
MainForm
Constructor MainForm
构造函数
public MainForm()
{
InitializeComponent();
// Initialize 4 x 28 with respective index
for (uint i = 0; i < 112; i++)
{
var bank = i / 28;
var index = i % 28;
_array[bank, index] = i + 1;
}
_childTest1 = new ChildTest { DataSource = _array[0] };
_childTest2 = new ChildTest { DataSource = _array[1] };
_childTest3 = new ChildTest { DataSource = _array[2] };
_childTest4 = new ChildTest { DataSource = _array[3] };
}
private readonly MultidimensionalArray _array = new MultidimensionalArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.