Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使一个文本框在.NET中的ComboBox更改时更新?_C#_Winforms - Fatal编程技术网

C# 如何使一个文本框在.NET中的ComboBox更改时更新?

C# 如何使一个文本框在.NET中的ComboBox更改时更新?,c#,winforms,C#,Winforms,代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Data.Sql; using System.Data.SqlClient; using System.Configuration; using System.Linq; using System.Text; using Sy

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Eventmanagement
{
    public partial class Registration : Form
    {
        SqlConnection aConnection;
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dta;

        public Registration()
        {
            InitializeComponent();
        }
        //--------------------------------------------//

        private void Registration_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'insertEventDataSet.Events' table. You can move, or remove it, as needed.

            populateEventSalesPersonList();
            populateEventNameIdTypeList();

            //+++++++++++++++++++++++++++++++++++++++++++//
            txtSalesTaxRate_Registration.Text = Convert.ToString( SalesTaxRate() +"%");

            //+++++++++++++++++++++++++++++++++++++++++++//
            txtSalesTax_Registration.Text = Convert.ToString(saleTax());
            txtTotalCharges_Registration.Text = Convert.ToString(totalCharges());
            txtAmountDue_Registration.Text = Convert.ToString(amountDue());
            //+++++++++++++++++++++++++++++++++++++++++++//



        }

        //--------------------------------------------//

        public string getConnectionString()
        {
            try
            {
                string sConnection = "";

                // Get the mapped configuration file.
                System.Configuration.ConnectionStringSettingsCollection ConnSettings = ConfigurationManager.ConnectionStrings;
                sConnection = ConnSettings["DBConnectionString"].ToString();

                return sConnection;


            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return "";
            }
        }



        //--------------------------------------------//
        public void populateEventNameIdTypeList()
        {
            try
            {
                cmbEvent_Registration.DataSource = getDataTable4();
                //----------------------------
                cmbEvent_Registration.ValueMember = "EventID";
                cmbEvent_Registration.DisplayMember = "EventName";




            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }


        }


        //--------------------------------------------//

        public void populateEventSalesPersonList()
        {
             try
            {

                cmbSalesPerson_Registration.DataSource = getDataTable5();
                cmbSalesPerson_Registration.ValueMember = "EmployeeID";
                cmbSalesPerson_Registration.DisplayMember = "SalesPerson";

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        //-------------------------------------------//

        public void populateFeeScheduleByEventList()
        {

            try
            {

                cmbFeeSchedule_Registration.DataSource = getDataTable6();
                cmbFeeSchedule_Registration.ValueMember = "FeeScheduleID";
                cmbFeeSchedule_Registration.DisplayMember = "Fee";
                cmbFeeSchedule_Registration.Text = cmbFeeSchedule_Registration.SelectedText.ToString();
                }

            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }

        //------------------------------------------//

        //------------------------------------------//



        private void btnclose_Registration_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInsert_Registration_Click(object sender, EventArgs e)
        {
            try
            {
                aConnection = new SqlConnection(getConnectionString());
                aConnection.Open();

                //Calling the Stored Procedure

                da.InsertCommand = new SqlCommand("RegistrationInsert", aConnection);
                da.InsertCommand.CommandType = CommandType.StoredProcedure;

                da.InsertCommand.Parameters.Add(@"RegistrationDate", SqlDbType.SmallDateTime).Value = Convert.ToDateTime(txtRegistrationDate_Registration.Text.ToString());
                da.InsertCommand.Parameters.Add(@"PurchaseOrderNumber", SqlDbType.VarChar).Value = txtPoNumber_Registration.Text; 
                da.InsertCommand.Parameters.Add(@"SalesPerson", SqlDbType.Int).Value = Convert.ToInt64(cmbSalesPerson_Registration.SelectedValue.ToString());
                da.InsertCommand.Parameters.Add(@"EventID", SqlDbType.Int).Value = cmbEvent_Registration.SelectedValue.ToString();
                da.InsertCommand.Parameters.Add(@"FeeScheduleID", SqlDbType.Int).Value = cmbFeeSchedule_Registration.SelectedValue.ToString();
                da.InsertCommand.Parameters.Add(@"RegistrationFee", SqlDbType.Money).Value = txtRegistrationFee_Registration.Text;

                da.InsertCommand.Parameters.Add(@"SalesTaxRate", SqlDbType.Float).Value = txtSalesTaxRate_Registration.Text;
                //da.InsertCommand.Parameters.Add(@"EndTime", SqlDbType.SmallDateTime).Value = Convert.ToDateTime(txtEndTime.Text.ToString());
                da.InsertCommand.ExecuteNonQuery();


                MessageBox.Show("Inserted successfully!!!");

                aConnection.Close();
                da.InsertCommand.Dispose();

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }


        }

        //-------------------------------------------//

        public DataTable getDataTable4()
        {
            try
            {
                dta = new DataTable();
                aConnection = new SqlConnection(getConnectionString());
                aConnection.Open();
                da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand("EventsSelectAll", aConnection);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.ExecuteNonQuery();

                da.Fill(dta);
                aConnection.Close();
                return dta;

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return null;
            }
        }
        public DataTable getDataTable5()
        {
            try
            {
                dta = new DataTable();
                aConnection = new SqlConnection(getConnectionString());
                aConnection.Open();
                da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand("sp_RegistrationSalesPerson", aConnection);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.ExecuteNonQuery();
                dta.Clear();
                da.Fill(dta);
                aConnection.Close();
                return dta;

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return null;
            }
        }
        public DataTable getDataTable6()
        {
            try
            {
                dta = new DataTable();
                da = new SqlDataAdapter();
                aConnection = new SqlConnection(getConnectionString());

                aConnection.Open();

                da.SelectCommand = new SqlCommand("sp_FeeScheduleSelectAllByEventID", aConnection);
                da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString());
                da.SelectCommand.CommandType = CommandType.StoredProcedure;


                da.SelectCommand.ExecuteNonQuery();
                da.Fill(dta);
                aConnection.Close();
                return dta;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return null;
            }


        }



        private void cmbEvent_Registration_SelectedIndexChanged(object sender, EventArgs e)
        {
                populateFeeScheduleByEventList();
                txtRemainingSeats_Registration.Text = Convert.ToString(spaces());
        }





        private void cmbFeeSchedule_Registration_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtRegistrationFee_Registration.Text = cmbFeeSchedule_Registration.Text.ToString();


        }
        public int totalRegisteredAttendees()
        {
            da = new SqlDataAdapter();
            aConnection = new SqlConnection(getConnectionString());
            da.SelectCommand = new SqlCommand("RegistrationSelectAllByEventID", aConnection);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString());

            aConnection.Open();
            int val = (int)da.SelectCommand.ExecuteScalar();
            aConnection.Close();
            return val;
        }
        public int spaces()
        {
            da = new SqlDataAdapter();
            aConnection = new SqlConnection(getConnectionString());
            da.SelectCommand = new SqlCommand("EventsSelect", aConnection);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString());
           // MessageBox.Show(cmbEvent_Registration.SelectedValue.ToString());
            aConnection.Open();

            int spaces = Convert.ToInt16(da.SelectCommand.ExecuteScalar());
            aConnection.Close();

            int value = totalRegisteredAttendees();
            int totalspaces = spaces - value;
            return totalspaces;

        }

        public double SalesTaxRate()
        {
            da = new SqlDataAdapter();
            aConnection = new SqlConnection(getConnectionString());
            da.SelectCommand = new SqlCommand("CompanyInfo", aConnection);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            aConnection.Open();
            double sale = Convert.ToDouble(da.SelectCommand.ExecuteScalar());
            aConnection.Close();
            return sale;
        }

        public void updateSaleTax()
        {

        }

        public double saleTax()
        {
            double regFee = Convert.ToDouble(txtRegistrationFee_Registration.Text.ToString());
            double sTR = Convert.ToDouble(SalesTaxRate());
            double sr = regFee * (sTR / 100);
            return sr;
       }
        public double totalCharges()
        {
            double tc = Convert.ToDouble(txtRegistrationFee_Registration.Text.ToString()) +  (saleTax());
            return tc;
        }
        public double amountDue()
        {
            double aD;
            if (txtTotalPaid_Registration.Text == string.Empty)
            {
                aD = Convert.ToDouble(txtTotalCharges_Registration.Text.ToString());
            }
            else
            {
                double a = Convert.ToDouble(txtTotalPaid_Registration.Text.ToString());
                 double b=    (Convert.ToDouble(txtTotalCharges_Registration.Text.ToString()));
                 aD = b-a;
            }
                return aD;
        }

        private void txtSalesTaxRate_Registration_TextChanged(object sender, EventArgs e)
        {

        }

        private void gpRegistraton_Enter(object sender, EventArgs e)
        {

        }

    }
}
我希望saleTax在

txtRegistrationFee_Registration.Text.ToString()

改变,所以基本上我想重新连接整个东西。请帮忙。

我上次在Winforms工作已经很久了。但是textbox不提供TextChange事件吗?也许那个事件有不同的名称。但当您更改文本时,必须有触发的事件


当您想从textbox获取数据时,它只是
textbox.Text
。您不需要调用
textbox.Text.ToString()
。文本已经是字符串。

您可以通过
TextChanged
事件连接您的
txtRegistrationFee\u注册。事件处理程序可以调用
saleTax()

FYI,这里没有C#NET这样的东西。这种语言名为C#