Home > Software engineering >  How do I pass multi dimensional array from parent to the controls in child form?
How do I pass multi dimensional array from parent to the controls in child form?

Time:08-09

I have a parent form and four child forms,

In each child form, there are 28 labels to fill, enter image description here

The parent form computes the value in the array

UInt16[,] _array = new UInt16[4, 28]; then send the value for each child

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

How do I send the values

  1. ChildForm

I have set the TabIndex of 28 labels to(0~27) in property

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. Parent Form

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

CodePudding user response:

Your question is how to pass a multidimensional array to your 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. This walk-through will demonstrate how to connect these in a one-time setup. First a couple of usage examples in the MainForm:

private void buttonClear2_Click(object sender, EventArgs e) 
{ 
    for (int cell = 0; cell < 28; cell  ) _array[1, cell] = 0x00; 
}

before and after clear

private void buttonTest1_Click(object sender, EventArgs e) => _array[0,0] = 0xFF;

before and after test


Observable UInt

The bindings are based on a uint value that sends a notification whenever its value changes. This class works behind the scenes to keep everything synced.

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

The multidimensional array provides a standard array indexer that gets and sets a uint value using _array[bank,index] = 0xnn. Under the hood are arrays made from these observable uints but it makes no difference in terms of interacting with the multidimensional array.

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

This is a Form with a TableLayoutPanel on it that is unremarkable except for the addition of a DataSource property. It is here that the observable uints are bound to the textbox values.

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

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();
  • Related