Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique persistent control identifier

What we have
We have some complex winforms control. To store its state we use some custom serialized class. Lets say we've serialized it to xml. Now we could save this xml as a file in User directory or to include it in some another file....
But...

The question is,
if user creates several such controls across his winform application (at design time), what unique identifier is better to use in order to know which of the saved configs belongs to which of these controls?

So this identifier should:

  • Stay the same across application launches
  • Automatic given (or already given, like we can assume that Control.Name is always there)
  • Unique across application

I think one could imagine several ways of doing it and I believe there are might be some default ways of doing it.

What is better to use? Why?

like image 822
MajesticRa Avatar asked Oct 10 '22 23:10

MajesticRa


1 Answers

This small extension method does the work:

public static class FormGetUniqueNameExtention
{
    public static string GetFullName(this Control control)
    {
        if(control.Parent == null) return control.Name;
        return control.Parent.GetFullName() + "." + control.Name;
    }
}

It returns something like 'Form1._flowLayoutPanel.label1'

Usage:

Control aaa;
Dictionary<string, ControlConfigs> configs;
...
configs[aaa.GetFullName()] = uniqueAaaConfig;
like image 184
MajesticRa Avatar answered Nov 04 '22 12:11

MajesticRa