Unique Permanent Management Identifier

What we have
We have some comprehensive control winforms. To save its state, we use some kind of custom serialized class. Let's say we serialize it in xml. Now we could save this xml as a file in the user directory or include it in some other file ....
But...

Question
if a user creates several of these controls in his winform application (during development), which unique identifier is better to use to find out which saved configuration belongs to which of these controls?

So this identifier should:

  • Stay on top of all application launches
  • Automatically set (or already set, as we can assume that Control.Name always exists)
  • Unique application

I think one could imagine several ways to do this, and I believe that there may be some default methods.

Which is better to use? Why?

+4
source share
3 answers

This small extension method does the job:

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'

Using:

 Control aaa; Dictionary<string, ControlConfigs> configs; ... configs[aaa.GetFullName()] = uniqueAaaConfig; 
+1
source

I am using a composite indentifier consisting of a complete management hierarchy tree. Assuming your name is Form1, you have the group package Groupbox1 and the TextBox1 text box, the composite identifier will be Form1 / Groupbox1 / TextBox1.

If you want to follow this, here are the details:

http://netpl.blogspot.com/2007/07/context-help-made-easy-revisited.html

+1
source

This is the method that I created to define a unique name that includes the full name of the form (with it the namespace), and then each parent control over the control in question. Thus, it can become something like:

MyCompany.Inventory.SomeForm1.SomeUserControl1.SomeGroupBox1.someTextBox1

  static string GetUniqueName(Control c) { StringBuilder UniqueName = new StringBuilder(); UniqueName.Append(c.Name); Form OwnerForm = c.FindForm(); //Start with the controls immediate parent; Control Parent = c.Parent; while (Parent != null) { if (Parent != OwnerForm) { //Insert the parent control name to the beginning of the unique name UniqueName.Insert(0, Parent.Name + "."); } else { //Insert the form name along with it namespace to the beginning of the unique name UniqueName.Insert(0, OwnerForm.GetType() + "."); } //Advance to the next parent level. Parent = Parent.Parent; } return UniqueName.ToString(); } 
+1
source

All Articles