C# 双精度。从另一个类中的列表解析 TextBox 始终为“0”或“NaN”

C# double.Parse TextBox from List in another Class is always '0' or 'NaN'

提问人:NICOLAS LEON NARINO 提问时间:9/5/2023 最后编辑:NICOLAS LEON NARINO 更新时间:9/6/2023 访问量:95

问:

让我们从预期的工作流程开始:

  1. 在 Class1 中创建文本框列表并将其添加到 Panel 中。
  2. 在 Class2 中调用 List 以使用 double。使用 Windows 窗体中的用户输入进行分析。
  3. 类 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);
    }
}
C# 列表 WinForms 分析 参考

评论

0赞 Jimi 9/5/2023
什么将文本框添加到 UI?顺便说一句,哪个文本框?什么?你为什么没有?这与UI中的控件有什么关系?在做什么?为什么要返回新列表?-- IMO,目前还不清楚你在那里做什么以及你在解析什么。看起来您正在分析空的 TextBox 控件 -- 您还应该发布处理程序中的代码DefaultTextBox();... new DefaultTextBox();List<TextBox>CustomTextBox.Vent();ctb.GetTextBoxCon()TextChanged
0赞 NICOLAS LEON NARINO 9/6/2023
嗨,吉米,从这两个答案中,现在很明显我正在调用空文本框。为了以防万一,“DefaultTextBox()”为每个新文本框定义视觉参数。'.Vent()' 方法只创建一个实例并将其调用回来,而不是每次都创建一个新实例。感谢您的回答;我还将添加 TextChanged 代码。
0赞 Jimi 9/6/2023
您必须发布的是一个最小的、可重复的示例。更具体地说(已经提到过),如何将控件添加到 UI,为什么要创建新列表,以及此新列表如何与接收输入的控件相关。没有这些细节,这只是猜测

答:

0赞 Jiri Volejnik 9/5/2023 #1

GetTextBoxCon() 方法始终创建一组新的新文本框。新文本框不包含任何文本。

在 ConeCalculation() 方法中,调用 GetTextBoxCon() 方法,将结果分配给局部临时变量 txtBoxCon,然后使用这些新的空文本框。不是您在面板中看到的原始文本框!这就是为什么你在 ConeCalculation() 方法中得到 0 或 NaN 的原因。

要使用添加到表单中的真实文本框,只需调用 GetTextBoxCon() 方法一次(可能在表单的构造函数中),将结果分配给表单/面板的成员变量(也可能称为 txtBoxCon)。然后,您只需从 ConeCalculation() 中删除 GetTextBoxCon() 方法的调用即可完成。