【问题标题】:How to capture values from panel placed on a form如何从放置在表单上的面板中捕获值
【发布时间】:2014-12-01 22:51:00
【问题描述】:

我不明白如何在表单面板中的文本框中捕获值。我正在尝试使用这些值更新我的数据库表。

谁能指出我正确的方向?

代码:

private void btnSupplier_Click(object sender, EventArgs e)
    {
        try
        {
            Panel pnlAddSupplier = new Panel();
            TextBox txtSupplierID = new TextBox();
            TextBox txtSupplierName = new TextBox();
            Button btnAddSupplier = new Button();
            Label lblSupplierID = new Label();
            Label lblSupplierName = new Label();

            // Initialize the Panel control.
            pnlAddSupplier.Location = new Point(56, 74);
            pnlAddSupplier.Size = new Size(200, 200);
            // Set the Borderstyle for the Panel to three-dimensional.
            pnlAddSupplier.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            // Initialize the Label and TextBox controls.
            lblSupplierID.Location = new Point(60, 0);
            lblSupplierID.Text = "Supplier ID";
            lblSupplierID.Size = new Size(104, 20);
            txtSupplierID.Location = new Point(20, 40);
            txtSupplierID.Text = "";
            txtSupplierID.Size = new Size(152, 20);

            lblSupplierName.Location = new Point(20, 70);
            lblSupplierName.Text = "Supplier Name";
            lblSupplierName.Size = new Size(150, 20);
            txtSupplierName.Location = new Point(20, 90);
            txtSupplierName.Text = "";
            txtSupplierName.Size = new Size(152, 20);

            lblSupplierID.Location = new Point(16, 16);
            lblSupplierID.Text = "Supplier ID";


            btnAddSupplier.Location = new Point(60, 120);
            btnAddSupplier.Text = "Add";
            btnAddSupplier.Click += btnAddSupplier_Click;
            // Add the Panel control to the form. 
            this.Controls.Add(pnlAddSupplier);
            // Add the Label and TextBox controls to the Panel.
            pnlAddSupplier.Controls.Add(lblSupplierID);
            pnlAddSupplier.Controls.Add(txtSupplierID);
            pnlAddSupplier.Controls.Add(lblSupplierName);
            pnlAddSupplier.Controls.Add(txtSupplierName);
            pnlAddSupplier.Controls.Add(btnAddSupplier);

        }
        catch { }
    }

 public void btnAdd_Click(object sender, EventArgs e)
        {
            string username= //how to assign textbox value?;
            int age = //how to assign textbox value;

            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

            string query = "INSERT INTO table(username, age) VALUES ('@xxx', '@xxx') "; 

             //please not these values are for demonstration.

            using (connection)
            {
                  ObjDb.OpenConnection();
                ObjDb.ExecuteNonQuery(query, connection);
            }
        }

如您所见,此代码对 SQL 注入开放,我想使用参数。为此,我需要捕获详细信息输入。如何实现?

我尝试分配 supplierID = txtSupplierID.Text,但收到一条消息说 txtSupplier.Text 不存在。

谢谢。

【问题讨论】:

    标签: c# mysql winforms


    【解决方案1】:

    我不知道哪个文本框用于年龄,但我希望这能给你一个好的起点。

    string username = txtSupplierID.Text; // how to assign textbox value?;
    int age = Int32.Parse(...); // how to assign textbox value;
    

    您需要对年龄进行更多验证。因此,最好使用:

    int age;
    if (!Int32.TryParse(..., out age))
    {
        MessageBox.Show("Incorrect age!");
        return;
    }
    

    将 txtSupplierID 更改为字段:

    private TextBox txtSupplierName;
    

    并更新这些行:

    txtSupplierName = new TextBox(); // remove TextBox
    

    【讨论】:

    • 我已经试过了,我似乎无法访问这个文本框。我得到“txtSupplierID.Text”不存在。此外,这些值仅用于演示,我实际上并没有存储年龄值。但很高兴指出验证。
    • 在这种情况下,将txtSupplierID 设为字段而不是局部变量。
    • 我在创建面板之前已经声明了这一点。文本框 txtSupplierID = new TextBox();
    • 如果前面声明了,还是局部变量。当您的 btnSupplier_Click 结束时,局部变量超出范围。因此,您应该将其设为字段。
    • 我明白了,所以如果我把它变成一个字段。这不会影响 txtSupplierID.Location = new Point(20,40) 吗?我肯定会抛出一个空引用错误。
    【解决方案2】:

    这是你的意思吗?假设用户将始终在 txtSupplierID TextBox 中输入整数值,这应该可以正常工作。将 Name 属性分配给您的 Control 并通过该 Name 访问它。随意问任何我没有解释的东西。干杯!

    private void btnSupplier_Click(object sender, EventArgs e)
    {
        Panel pnlAddSupplier = new Panel()
        {
            Name = "pnlAddSupplier",
            Location = new Point(56, 74),
            Size = new Size(200, 200),
            BorderStyle = BorderStyle.Fixed3D
        };
        TextBox txtSupplierID = new TextBox()
        {
            Name = "txtSupplierID",
            Location = new Point(20, 40),
            Text = string.Empty,
            Size = new Size(152, 20)
        };
        TextBox txtSupplierName = new TextBox()
        {
            Name = "txtSupplierName",
            Location = new Point(20, 90),
            Text = string.Empty,
            Size = new Size(152, 20)
        };
        Label lblSupplierID = new Label()
        {
            Name = "lblSupplierID",
            Location = new Point(20, 20),
            Text = "Supplier ID:",
            Size = new Size(104, 20)
        };
        Label lblSupplierName = new Label()
        {
            Name = "lblSupplierName",
            Location = new Point(20, 70),
            Text = "Supplier Name:",
            Size = new Size(150, 20)
        };
        Button btnAddSupplier = new Button()
        {
            Name = "btnAddSupplier",
            Location = new Point(60, 120),
            Text = "Add",
        };
        btnAddSupplier.Click += btnAdd_Click;
    
        // Add controls to the Panel.
        pnlAddSupplier.Controls.Add(lblSupplierID);
        pnlAddSupplier.Controls.Add(txtSupplierID);
        pnlAddSupplier.Controls.Add(lblSupplierName);
        pnlAddSupplier.Controls.Add(txtSupplierName);
        pnlAddSupplier.Controls.Add(btnAddSupplier);
    
        // Add the Panel control to the form. 
        this.Controls.Add(pnlAddSupplier);
    }
    
    public void btnAdd_Click(object sender, EventArgs e)
    {
        string username = this.Controls["pnlAddSupplier"].Controls["txtSupplierName"].Text;
        int age = Int32.Parse(this.Controls["pnlAddSupplier"].Controls["txtSupplierID"].Text);
    
        MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
    
        string query = "INSERT INTO table(username, age) VALUES ('@xxx', '@xxx') ";
    
        using (connection)
        {
            ObjDb.OpenConnection();
            ObjDb.ExecuteNonQuery(query, connection);
        }
    }
    

    【讨论】:

    • 字符串 username = this.Controls["pnlAddSupplier"].Controls["txtSupplierName"].Text 出现空引用异常;
    • @Harry 我刚试过,效果很好。您必须分配控件的 Name 属性。 txtSupplierName.Name = "txtSupplierName";你确定你这样做了吗?
    • @Harry 我很高兴它有帮助 ;)
    【解决方案3】:

    首先,您需要为控件命名:

     txtSupplierID.Name = "txtSupplierID";
    

    然后你可以得到这样的值:

    Control[] Found = this.Controls.Find("txtSupplierID", true);
    
    TextBox IDTextBox = Found[0] as TextBox;
    
    string SupplierID = IDTextBox.Text;
    

    别忘了添加错误检查!

    【讨论】:

    • Control ctrl = Controls["txtSupplierID"]; 也可以用来定位控件。
    【解决方案4】:

    您正在使您的供应商 ID 文本框成为您的 btnSupplier_Click 函数的局部变量。您需要将其设为类变量。如果您使用设计器将它拖放到您的网页上,那么这个变量应该出现在您的 *.designer.cs 文件中。

    以下是如何使用参数来防止 SQL 注入:

                DbCommand myCommand = Connection.CreateCommand();
                myCommand.CommandText = @"select * from myTable mt
                    where 
                    mt.paramter = @myParameter";
    
                DbParameter myParameter = myCommand.CreateParameter();
                myParameter.ParameterName = "@myParameter";
                myCommand.Parameters.Add(myParameter);    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 2017-09-27
      相关资源
      最近更新 更多