Foreach循环没有到达第一个面板项目

本文关键字:第一个 项目 循环 Foreach | 更新日期: 2023-09-27 18:05:10

在我看来,我的foreach循环没有到达我的第二个面板,或者有什么东西使它跳过循环中的项目。

这是面板和循环的创建(我知道SQL容易受到注入,但为了使问题更短,我只是使用SQL):

这是为product表中的每个产品创建的内容:

Label lbName = new Label();
lbName.Text = name + "'n" + row[7].ToString();
lbName.Height = 40;
lbName.Width = 150;
lbName.Name = name + row[7].ToString();
lbName.Location = new Point(ptX, ptY);
pt.Controls.Add(lbName);
Label lbID = new Label();
lbID.Text = row[0].ToString();
lbID.Height = 40;
lbID.Width = 150;
lbID.Name = "ID" + name + row[7].ToString();
lbID.Location = new Point(ptX, ptY);
lbID.Visible = false;
pt.Controls.Add(lbID);
TextBox txtStockCount = new TextBox();
txtStockCount.Text = "0";
txtStockCount.Height = 40;
txtStockCount.Width = 100;
txtStockCount.Name = "txtTS" + name + row[7].ToString();
txtStockCount.Location = new Point(ptX + 150, ptY);
txtStockCount.GotFocus += txtStockCount_GotFocus;
txtStockCount.KeyPress += txtStockCount_KeyPress;
txtStockCount.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtStockCount);
TextBox txtBroken = new TextBox();
txtBroken.Text = "0";
txtBroken.Height = 40;
txtBroken.Width = 100;
txtBroken.Name = "txtTB" + name + row[7].ToString();
txtBroken.Location = new Point(ptX + 300, ptY);
txtBroken.GotFocus += txtStockCount_GotFocus;
txtBroken.KeyPress += txtStockCount_KeyPress;
txtBroken.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtBroken);
TextBox txtRecieve = new TextBox();
txtRecieve.Text = "0";
txtRecieve.Height = 40;
txtRecieve.Width = 100;
txtRecieve.Name = "txtTR" + name + row[7].ToString();
txtRecieve.Location = new Point(ptX + 450, ptY);
txtRecieve.GotFocus += txtStockCount_GotFocus;
txtRecieve.KeyPress += txtStockCount_KeyPress;
txtRecieve.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtRecieve);
Label lbDifference = new Label();
lbDifference.Text = " 0 ";
lbDifference.Height = 40;
lbDifference.Width = 150;
lbDifference.Name = "lblDiff" + name + row[7].ToString();
                    lbDifference.Location = new Point(ptX + 600, ptY);
pt.Controls.Add(lbDifference);
Button btnConfirm = new Button();
btnConfirm.Text = "Confirm";
btnConfirm.Height = 30;
btnConfirm.Width = 80;
btnConfirm.Name = "btnConfirm" + name + row[7].ToString();
                    btnConfirm.Location = new Point(ptX + 750, ptY);
                    btnConfirm.Click += btnConfirm_Click;
pt.Controls.Add(btnConfirm);

和读取面板以从文本框中更新数据库:

private void txtStockCount_LostFocus(object sender, EventArgs e)
{
        TextBox txtB = (sender as TextBox);
        string name = txtB.Name.Remove(0,5);
        string prodID = "";
        string QtyToday = "";
        string QtyBD = "";
        string QtyRev = "";
        string table = "";
        foreach (Panel p in pnls)
        {
            IEnumerable<Label> labels = p.Controls.OfType<Label>();
            foreach (Label label in labels)
            {
                string Compare = label.Name.Remove(0, 2);
                if (name == Compare)
                {
                    prodID = label.Text;
                    if (Regex.IsMatch(Compare, @"^[a-hA-H]"))
                    {
                        table = "ProductHistoryAH";
                    }
                    else if (Regex.IsMatch(Compare, @"^[i-qI-Q]"))
                    {
                        table = "ProductHistoryIQ";
                    }
                    else if (Regex.IsMatch(Compare, @"^[r-zR-Z]"))
                    {
                        table = "ProductHistoryRZ";
                    }
                }
            }
            string sql = "Select * From Product WHERE ProdID = '" + prodID + "'";
            DataTable dtP = db.GetDataTable(sql);
            IEnumerable<TextBox> textBoxes = p.Controls.OfType<TextBox>();
            foreach (TextBox textBox in textBoxes)
            {
                string name1 = textBox.Name.Remove(0, 5);
                label2.Text = name1;
                if (name == name1)
                {
                    if (textBox.Name == "txtTS" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
                    {
                        QtyToday = textBox.Text;
                    }
                    if (textBox.Name == "txtTB" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
                    {
                        QtyBD = textBox.Text;
                    }
                    if (textBox.Name == "txtTR" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
                    {
                        QtyRev = textBox.Text;
                    }
                }
            }                
            string sql1 = "Select * From " + table + " WHERE ProdID = '" + prodID + "' AND ProdHDate = '" + DateTime.Today.Date.ToShortDateString() + "'";
            DataTable dt = db.GetDataTable(sql1);                
            if (dt.Rows.Count == 0)
            {
                dbh.addnewstockhistory(ph);
            }
            else if (dt.Rows.Count > 0)
            {                    
                dbh.updatestockhistory(ph);
            }
        }            
}

一切工作100%,它插入和更新数据很容易,与第一个面板,但没有其他面板上的其他文本框工作。

错误在foreach面板循环中,foreach循环在面板循环中。它说在字符串sql1的位置附近有语法错误。但我不明白的是,它适用于第一个面板,但非其他面板,它确实有prodID,我知道这是因为我把语句放在一个标签只是为了看看它看起来如何。

请帮助。

Foreach循环没有到达第一个面板项目

我想我解决了。我所要做的就是检查面板是否是承载控件的面板,检查面板是否是按钮的父面板,并且它工作。

if(p == btn.Parent)
{
    //Do my stuff
}