简体   繁体   English

其他文本框文本更改时,如何自动更改文本框文本?

[英]How to change textbox text automatically when other textbox text changes?

I have these textboxes named bags,rate,quantity,packing size and amount what i want to do is that when user enters bags and rate and packing size the quantity textbox should automatically shows the corresponding quantity and amount but in my case when i click on calculate button then it calculates and show the quantity and amount i have tried using textchanged event but it does not do the job? 我将这些文本框命名为bags,rate,quantity,包装尺寸和数量,我想要做的是,当用户输入袋子,速率和包装尺寸时,数量文本框应自动显示相应的数量和数量,但在我的情况下,当我单击计算按钮,然后它计算并显示我使用textchanged事件尝试过的数量和数量,但是它没有完成工作?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Login
{
    public partial class Sale : Form
    {
        SaleCalci sale;
        SaleBillheader SaleHeaderModel = new SaleBillheader();
        tbl_SaleBillDetails SaleDetailModel = new tbl_SaleBillDetails();



        public Sale()
        {
            InitializeComponent();
        }

        private void Cancelbtn_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void clear()
        {
            txtBillNo.Text = txtDesc.Text = "";
            txtBags.Text = txtQty.Text = txtRate.Text = txtAmt.Text = "0.00";
            if(txtQty.Text !=null &&  txtAmt.Text !=null )
            {
                txtQty.Text = "0.00";
                txtAmt.Text = "0.00";
            }
            Savebtn.Text = "Save";
            SaleHeaderModel.SaleBillHeaderId = 0;
            SaleDetailModel.SaleBill_Id = 0;
        }

        private void Exitbtn_Click(object sender, EventArgs e)
        {
           var result =  MessageBox.Show("Are you sure you want to close this form ?", "Confirm", MessageBoxButtons.YesNo);
           if (result == DialogResult.Yes)
           { this.Close(); }


        }

        private void Sale_Load(object sender, EventArgs e)
        {
            ItemCombo();
            PartyCombo();
            PackingSizeCombo();

           // clear();
        }

        private void ItemCombo()
        {
            UserDataEntities db = new UserDataEntities();
            Itembox.DataSource = db.tbl_ItemId.ToList();
            Itembox.ValueMember = "ItemId";
            Itembox.DisplayMember = "ItemName";

        }
        private void PartyCombo()
        {
            UserDataEntities db = new UserDataEntities();
            PartyBox.DataSource = db.tbl_Parties.ToList();
            PartyBox.ValueMember = "Id";
            PartyBox.DisplayMember = "PartyName";

        }
        private void PackingSizeCombo()
        {
            UserDataEntities db = new UserDataEntities();
            PackingBox.DataSource = db.PackingSizes.ToList();
            PackingBox.ValueMember =  "PackingSizeId";
            PackingBox.DisplayMember = "PackingSize1";
        }

        private void Savebtn_Click(object sender, EventArgs e)
        {
            CalculateAmount();
            DisplayAmt();
        }
        private void CalculateAmount()
        {
            int bags = 0;
            decimal rate = 0;
            int pksize = 0;

            bags = Convert.ToInt32(txtBags.Text);
            rate = Convert.ToDecimal(txtRate.Text);
            pksize = Convert.ToInt32(PackingBox.Text);

            sale = new SaleCalci(bags,rate, pksize);

            //sale.Bags = Convert.ToInt32(txtBags.Text);
            //sale.Rate = Convert.ToDecimal(txtRate.Text);


            //SaleDetailModel.Bags = int.Parse(txtBags.Text.Trim());
            //SaleDetailModel.Qty = Convert.ToDecimal(txtQty.Text.Trim());
            //SaleDetailModel.Rate = Convert.ToDecimal(txtRate.Text.Trim());
           // SaleDetailModel.Amount = amount;






        }

        private void txtAmt_TextChanged(object sender, EventArgs e)
        {
           // txtAmt.Text = sale.CalucalteAmt.ToString();
        }

        private void Sale_Click(object sender, EventArgs e)
        {
            if ((txtBags.Text == "0.00") && (txtQty.Text == "0.00")&&(txtRate.Text == "0.00")&& (txtAmt.Text =="0.00"))
            {
                txtAmt.Clear();
                txtBags.Clear();
                txtQty.Clear();
                txtRate.Clear();
            }
        }

        private void txtQty_TextChanged(object sender, EventArgs e)
        {

            DisplayAmt();

        }

        private void txtBags_TextChanged(object sender, EventArgs e)
        {

           // sale.Bags = Convert.ToInt32(txtBags.Text);
           // DisplayAmt();
        }

        private void PackingBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtRate_TextChanged(object sender, EventArgs e)
        {

           // DisplayAmt();
        }

        private void DisplayAmt()
        {
            decimal _amt = sale.CalucalteAmt;
            txtQty.Text = sale.CalculateQty().ToString();
            txtAmt.Text = _amt.ToString();

        }


    }


}

Normally "TextChanged" event fires autamatically if text value changes. 通常,如果文本值更改,“ TextChanged”事件将自动触发。

So here the problem is i think about your other "partial class" in which there has to exist eventhandler work. 因此,这里的问题是我在考虑您的其他“局部类”,其中必须存在事件处理程序工作。 Something like: 就像是:

txtBags.TextChanged += new EventHandler(txtBags_TextChanged);

Please check your other partial class if this staement exist. 如果存在这种情况,请检查您的其他局部课程。

This eventhandler sometimes disappears from project if you move your gui elements or for some other causes... 如果您移动gui元素或其他原因,此事件处理程序有时会从项目中消失。

You can readd this statement manually. 您可以手动阅读此声明。

By the way if you have not experience with the other partial class then you can try to remove these textboxes and re-add them then your problem will autamatically solves. 顺便说一句,如果您没有其他局部类的经验,则可以尝试删除这些文本框并重新添加它们,这样您的问题将自动解决。

You need to call DisplayAmt in the TextChanged event of txtBags , txtRate and Size. 您需要在txtBags,txtRate和Size的TextChanged事件中调用DisplayAmt。 In the code above, call to DisplayAmt is commented out. 在上面的代码中,对DisplayAmt的调用已被注释掉。 Instead, you are calling DisplayAmt in the TextChanged event of txtQty. 而是在txtQty的TextChanged事件中调用DisplayAmt。

You should be, instead doing. 你应该做,而不是做。

private void txtAmt_TextChanged(object sender, EventArgs e)
{
   DisplayAmt();
}

private void txtRate_TextChanged(object sender, EventArgs e)
{
   DisplayAmt();
}

Similarly, you need to add Changed event for Text Control for Size. 同样,您需要为Text Control for Size添加Changed事件。 The txtQty is updated by the DisplayAmt() method. txtQty通过DisplayAmt()方法更新。 So you don't necessarily need it, unless for reasons that are not specified in OP. 因此,除非出于OP中未指定的原因,否则您不一定需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM