提问人:NICOLAS LEON NARINO 提问时间:9/5/2023 最后编辑:NICOLAS LEON NARINO 更新时间:9/6/2023 访问量:95
C# 双精度。从另一个类中的列表解析 TextBox 始终为“0”或“NaN”
C# double.Parse TextBox from List in another Class is always '0' or 'NaN'
问:
让我们从预期的工作流程开始:
- 在 Class1 中创建文本框列表并将其添加到 Panel 中。
- 在 Class2 中调用 List 以使用 double。使用 Windows 窗体中的用户输入进行分析。
- 类 3 中的按钮使用解析值激活一些几何计算。
问题是,当我显示消息以验证对象是否具有正确的值时,我总是得到“0”或“NaN”的值。
我试图使列表静态化,我用字典替换了列表,但没有结果;我试图解析 List 中的文本框,为 Class2 中的一些公共变量分配一个值,从而生成溢出异常。这是现在的最后一次尝试:
CustomTextBox 类:
{
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;
}
}
第2类:
{
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");
}
}
注意:如果您想查看其他方法的定义。我们开始了:
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);
}
}
答:
0赞
Jiri Volejnik
9/5/2023
#1
GetTextBoxCon() 方法始终创建一组新的新文本框。新文本框不包含任何文本。
在 ConeCalculation() 方法中,调用 GetTextBoxCon() 方法,将结果分配给局部临时变量 txtBoxCon,然后使用这些新的空文本框。不是您在面板中看到的原始文本框!这就是为什么你在 ConeCalculation() 方法中得到 0 或 NaN 的原因。
要使用添加到表单中的真实文本框,只需调用 GetTextBoxCon() 方法一次(可能在表单的构造函数中),将结果分配给表单/面板的成员变量(也可能称为 txtBoxCon)。然后,您只需从 ConeCalculation() 中删除 GetTextBoxCon() 方法的调用即可完成。
评论
DefaultTextBox();
... new DefaultTextBox();
List<TextBox>
CustomTextBox.Vent();
ctb.GetTextBoxCon()
TextChanged