A clean way to share some state (in your case dictionary of keys) is via some shared service (singleton), so:
You could create a class (e.g. ElectrodeManager
) which will hold a dictionary of electrodes (empty in the first place).
In the Form1 you will populate that dictionary via specified method on that class (e.g. AddElectrode(string electrodeType, string electrodeKey)
-> which will add new item into the dictionary)
- so you will have Dictionary<string, string>
that holds e.g. {"T1", "K000-000"}, {"T2", "K000-0001"} ...
In the Form2 you will work that dictionary from the ElectrodeManager
and you will append string from the textbox onto electrode's key.
Example:
public class ElectrodeManager
{
#region Singleton Pattern
private static ElectrodeManager instance;
public static ElectrodeManager Instance
{
get
{
if (instance == null)
instance = new ElectrodeManager();
return instance;
}
}
private ElectrodeManager()
{
electrodes = new Dictionary<string, string>();
}
#endregion
#region Fields
private Dictionary<string, string> electrodes;
#endregion Fields
#region Methods
public void AddElectrode(string eType, string eKey)
{
if (!electrodes.ContainsKey(eType))
{
electrodes.Add(eType, eKey);
}
}
public void AppendStringToElectrodeKey(string eType, string keyAddendum)
{
string electrodeKey = String.Empty;
if (electrodes.TryGetValue(eType, out electrodeKey))
{
electrodes[eType] = String.Format("{0}-{1}", electrodes[eType], keyAddendum);
}
}
public IDictionary<string, string> GetElectrodes()
{
return electrodes;
}
#endregion Methods
}
Usage inside Form1 (somewhere in generation logic):
ElectrodeManager.Instance.AddElectrode("T1", "K000-000");
ElectrodeManager.Instance.AddElectrode("T2", "K000-001");
Inside Form2 (button click):
ElectrodeManager.Instance.AppendStringToElectrodeKey("T1", textBox.Text);
ElectrodeManager.Instance.AppendStringToElectrodeKey("T2", textBox.Text);
Of course, you could easily switch data type to the List<string>
if that suits you better.