Let's start with the intended workflow:
The problem is, I'm always getting a value of '0' or 'NaN' when I display a message to verify that the objects have the correct value.
I've tried to make the List static, I've replaced the List with Dictionary, with no results; and I've tried to parse the textboxes inside the List to assign a value to some public variables from Class2, generating an overflowexception. This is the last attempt for now:
CustomTextBox Class:
{
public List<System.Windows.Forms.TextBox> GetTextBoxCon()
{
return TextBoxCon();
}
public List<System.Windows.Forms.TextBox> TextBoxCon()
{
List<System.Windows.Forms.TextBox> txtBoxCon = new List<System.Windows.Forms.TextBox>();
System.Windows.Forms.TextBox txtConD = DefaultTextBox();
txtConD.Location = p1;
txtConD.Tag = 0;
System.Windows.Forms.TextBox txtCon_d = DefaultTextBox();
txtCon_d.Location = p2;
txtCon_d.Tag = 1;
txtBoxCon.Add(txtConD);
txtBoxCon.Add(txtCon_d);
// n more textboxes...
foreach (var textBox in txtBoxCon)
{
textBox.TextChanged += new EventHandler(ConTextBox_TextChanged);
}
return txtBoxCon;
}
}
Class2:
{
public void ConeCalculation()
{
CustomTextBox ctb = CustomTextBox.Vent();
List<System.Windows.Forms.TextBox> txtBoxCon = ctb.GetTextBoxCon();
//Previous attempt
/*System.Windows.Forms.TextBox txtConD = txtBoxCon[0];
System.Windows.Forms.TextBox txtCon_d = txtBoxCon[1];*/
//Last attempt
double pre_dimLarge = double.Parse(txtBoxCon[0].Text);
double dimSmall = double.Parse(txtBoxCon[1].Text);
string message = "";
message += "pre_dimLarge: " + pre_dimLarge.ToString() + '\n' + '\n';
message += "dimSmall: " + dimSmall.ToString() + "\n" + '\n';
MessageBox.Show(message, "Verified values");
}
}
Note: In case you want to see the definition of the other methods. There we go:
public static CustomTextBox inst = null;
public static CustomTextBox Vent()
{
if (inst == null)
{
inst = new CustomTextBox();
return inst;
}
return inst;
}
private void ConTextBox_TextChanged(object sender, EventArgs e)
{
if (sender is System.Windows.Forms.TextBox textBox)
{
if (!string.IsNullOrEmpty(textBox.Text) && !double.TryParse(textBox.Text, out double _))
{
textBox.ForeColor = Color.Red;
MessageBox.Show("Write just numbers.");
}
else
{
textBox.ForeColor = Color.Black;
}
}
}
public ConPanel()
{
Size = new System.Drawing.Size(258, 300);
Location = new System.Drawing.Point(12, 61);
CustomTextBox ctb = CustomTextBox.Vent();
AddTextBox(ctb);
}
public void AddTextBox(CustomTextBox ctb)
{
List<System.Windows.Forms.TextBox> conTextB = ctb.TextBoxCon();
foreach (System.Windows.Forms.TextBox textBox in conTextB)
{
Controls.Add(textBox);
}
}
The GetTextBoxCon() method always creates a new set of new text boxes. A new text box contains no text.
In the ConeCalculation() method, you call the GetTextBoxCon() method, assign the result to the local temporary variable txtBoxCon and then you work with these new empty text boxes. Not with original text boxes that you see in the panel! That's why you get 0 or NaN in the ConeCalculation() method.
To work with real text boxes that were added to the form, you need to call the GetTextBoxCon() method only once (perhaps in the form's constructor), assign the result to the member variable of the form/panel (perhaps called txtBoxCon, too). Then you just remove the call of the GetTextBoxCon() method from the ConeCalculation() an you're done.