0

I am using windows forms to represent some parameters (i.e. temperature) incoming from an external device. I would like to represent this data in a windows forms labels and would like to do it this way: 1) create a label named according to data register; 2) While reading data, create a string that contains label object reference as string; 3) Make an object reference out of the string and call the text() method to update the data stored in the label.

Some example code on how I tried to do this:

// Here I create a label that is referenced by L0x7241
private System.Windows.Forms.Label L0x7241;


private void UpdateLabels(byte[] data)
        {
            // Convert bytes to register name
            var reg = new byte[2];
            reg[0] = data[1];
            reg[1] = data[0];
            // Here I construct a string, that contains the same text: L0x7241
            string associatedWidget = "L0x" + BitConverter.ToString(reg).Replace("-", "");
            // Here I would like to change text of the label
            // that has the reference that i have just constructed
            associatedWidget.Text("1111");
        }

I understand that a string containing the same text as object reference is not the reference itself, thus I am looking for a way to convert a string to a reference if that is possible at all.

Now, I understand that I could do the same by associating each label with a variable and update them on variable change. Yet I think that the above mentioned way would make the program significantly smaller...

  • you can pass string by ref, what do u think about it? – Yauhen Sampir Mar 13 '19 at 07:59
  • 3
    The best way in your case would be to give a name to your label: `L0x7241.Name = "L0x7241";` (if you created that label using the designer then it already has been assigned a name so you don't have to do this step). Then it's possible to find a control by its name: https://stackoverflow.com/questions/1536739/get-a-windows-forms-control-by-name-in-c-sharp – Kevin Gosse Mar 13 '19 at 08:01
  • I kindof, maybe, think I understand what you're asking for. But strings in .NET are *immutable*. You never change strings, you always get a new object instead. – Damien_The_Unbeliever Mar 13 '19 at 08:03
  • checkout the `system.Activator` namespace. This is exactly what you need – bilpor Mar 13 '19 at 09:03
  • Thank you for the suggestions. I couldn't implement any of those probably due to lack of experience in programming. Anyway, using controls didn't work out for me because, if I understand correctly, a label isn't a control, thus "Controls[]" or "Controls.find()" always return null. The system activator class is used to create new objects and I want to associate the string with an existing one. Lastly, I am aware that strings are immutable and here I am not trying to make changes to one. I am writing a helper function that will be called many times, thus - create and discard strings. – Aivaras Kazakevičius Mar 13 '19 at 11:21
  • A Label **IS** a Control. You can find it with `var lbl1 = this.Controls.Find("label1", true).FirstOrDefault();` or `var lbl2 = this.Controls.OfType – Jimi Mar 13 '19 at 17:53

1 Answers1

2

Associating objects with keys - that's what dictionaries are for.

private Dictionary<string, Label> mapping = new Dictionary<string, Label>();

public void MyForm()
{
    InitializeComponent();

    // assuming you created the labels in design time populate your dictionary here:
    mapping["L0x7241"] = labelL0x7241;
    // ...
}

private void UpdateLabels(byte[] data)
{
    string associatedWidget = GetKey(data); // whatever logic here
    mapping[associatedWidget].Text = "1111"; // change the text of the associated label
}
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65