【问题标题】:Pass quantity amount text box value from child form to a variable on parent form将数量金额文本框值从子表单传递到父表单上的变量
【发布时间】:2019-07-21 16:36:26
【问题描述】:

我的 windows 窗体应用程序(销售点应用程序)的父窗体有一个网格视图,其中包含来自数据库的数据,如产品名称、数量和单价(不包括数量,因为我已将其默认值设置为 1)。这是我的代码:

        string productName = dr[0];
        int quantity = 1;
        int unitPrice = Convert.ToInt32(dr[1]);

        DataGridViewRow row = new DataGridViewRow();
        row.CreateCells(dgvAllSoldItems);

        row.Cells[0].Value = productName;
        row.Cells[1].Value = quantity;
        row.Cells[2].Value = unitPrice;
        row.Cells[3].Value = (quantity * unitPrice);

        dgvAllSoldItems.Rows.Add(row);

我想要的是允许我的用户在需要时更新特定项目的数量。我希望使用子表单来完成此操作。子表单上有一个用于数量(自定义数量)的文本框。

我已经配置了子表单,我可以在双击目标产品(数据网格视图行)时显示它。我想要的是允许用户将主表单上的数量 [int quantity = 1;] 的默认值替换为用户将放在子表单上的文本框上的自定义值。

我只是在将这个自定义数量(文本框值)从子表单传递到我的父表单时遇到问题。到目前为止,我已经使用以下代码将文本框值传递给我的父窗体,我很幸运能够在我的父窗体上的公共方法 [GetSoldItemQuantity] 中获取文本框值。

public void GetSoldItemQuantity(string customQuantity)
    {
        string globalQuantity = customQuantity;
    }

此时,我总是从string globalQuantity 的子表单中获取自定义数量值,但我无法将此值分配回int quantity 或用此值替换其现有值。

这是我的子表单上的按钮单击代码,它将自定义数量值从文本框传递到我父表单上的公共方法。

private void btnPassCustomQuantityToParentForm_Click(object sender, EventArgs e)
        {
            string newItemQuantity = txtCustomQuantity.Text;

            frmSale newSale = new frmSale();
            newSale.GetSoldItemQuantity(newItemQuantity);
            this.Close();
        }

如何将此自定义数量传递给我的第一个代码 sn-p 中默认值为 1 的数量变量?

【问题讨论】:

标签: c# winforms textbox parent-child


【解决方案1】:

您可以将textbox 修饰符设为公开,这样您就可以使用对子表单的引用从父表单访问textbox,此外,如果您想要整数值使用numeric up down 而不是简单的textbox。要将字符串转换为文本,您可以在 c# 中使用 Convert.ToInt32Int32.TryParse 方法

【讨论】:

    猜你喜欢
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多