簡體   English   中英

如何將多維數組從父級傳遞到子窗體中的控件?

[英]How do I pass multi dimensional array from parent to the controls in child form?

我有一個父表格和四個孩子 forms,

在每個子表單中,有 28 個標簽要填寫, 在此處輸入圖像描述

父窗體計算數組中的值

UInt16[,] _array = 新 UInt16[4, 28]; 然后發送每個孩子的價值

  • _array[0, 0] ~[0, 27] 到 Child1
  • _array[1, 0] ~[1, 27] 到 Child2
  • _array[2, 0] ~[2, 27] 到 Child3
  • _array[3, 0] ~[3, 27] 到 Child4

如何發送值

  1. 子窗體

我在屬性中將 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";
                }
            }
        }
    }
  1. 父表格

UInt16[,] _array = new UInt16[4, 28]{//Some Values}
if (Application.OpenForms["ChildTest"] is ChildTest childForm)
                    {
                        childForm.Focus();
                        return;
                    }
                    childForm = new ChildTest();

您的問題是如何將多維數組傳遞給您的ChildTest forms。 我的建議是以數據綁定的形式傳遞它。 這樣,您的MainForm可以使用像_array[1][5] = 0xff這樣的語句來設置值,這將自動在正確的表單上正確的 position 顯示值。 本演練將演示如何在一次性設置中連接它們。 首先是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;

測試前后


可觀察的 UInt

綁定基於一個uint值,該值在其值更改時發送通知。 這個 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;
}

多維數組class

多維數組提供了一個標准數組索引器,它使用_array[bank][index] = 0xnn獲取和設置一個uint值。 引擎蓋下是 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表格 class

這是一個帶有TableLayoutPanelForm ,除了添加了一個DataSource屬性外,它是不起眼的。 正是在這里,可觀察的 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構造函數

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM