简体   繁体   English

无论文本框是否更改,TextChangedEvent均不会触发

[英]TextChangedEvent does not fire when textbox changes or not

I am having a difficult time pinpointing what am I doing wrong here. 我很难在这里指出我在做什么错。 My logic is to run the stored procedure ( UpdateRate ) only if a textbox changes. 我的逻辑是仅在文本框更改时才运行存储过程( UpdateRate )。 If there is no change in the textbox, just skip the stored procedure for that row of information and move to the next one. 如果文本框中没有更改,则只需跳过该信息行的存储过程,然后移至下一行。

Can someone please help me figure this out? 有人可以帮我解决这个问题吗? I have tried everything. 我已经尝试了一切。 Keep in mind that I am new at this and might not fully understand complicated answers. 请记住,我是新手,可能无法完全理解复杂的答案。

C#: C#:

public partial class MainWindow : 
{
    internal static string oldAvgRate;
    internal static string oldOTRate;
    internal static string ratetype;
    internal static string rtOT;

    public MainWindow()
    {
        InitializeComponent();

        string connectionString = "datasource=;port=;username=;password=";
        string sDate = DateTime.Now.ToString("yyyy-MM-dd");
        MySqlConnection connection = new MySqlConnection(connectionString);

        MySqlCommand avgRate = new MySqlCommand("Select ID, DateFrom, DateTo, RateType, Amount, Description from Daily.Rates where RateType = 'Average Hourly Wages' and DateTo >= @sDate", connection);
        avgRate.Parameters.Add(new MySqlParameter("sDate", sDate));

        MySqlCommand otRate = new MySqlCommand("Select ID, DateFrom, DateTo, RateType, Amount, Description from Daily.Rates where RateType = 'Average OT Hourly Wages' and DateTo >= @sDate", connection);
        otRate.Parameters.Add(new MySqlParameter("sDate", sDate));

        try
        {
            connection.Open();

            MySqlDataReader AvgR = avgRate.ExecuteReader();

            while (AvgR.Read())
            {
                txtAHW.Text = AvgR["Amount"].ToString(); 
                dfAHW.Text = AvgR["DateFrom"].ToString();
                dtAHW.Text = AvgR["DateTo"].ToString();
                txtcommAHW.Text = AvgR["Description"].ToString();

                oldAvgRate = txtAHW.Text = AvgR["Amount"].ToString();
                ratetype = AvgR["RateType"].ToString();
            }

            AvgR.Close();
            AvgR.Dispose();

            MySqlDataReader OtR = otRate.ExecuteReader();

            while (OtR.Read())
            {
                txtOTHW.Text = OtR["Amount"].ToString();
                dfOTHW.Text = OtR["DateFrom"].ToString();
                dtOTHW.Text = OtR["DateTo"].ToString();
                txtcommOTHW.Text = OtR["Description"].ToString();

                oldOTRate = txtOTHW.Text = OtR["Amount"].ToString();
                rtOT = OtR["RateType"].ToString();
            }   

            OtR.Close();
            OtR.Dispose();
        }

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

        connection.Close();
    }

        private string UpdateRate(string dateFrom, string newRate, string oldRate, string ratetype, string description)
        {
            string connectionString = "datasource=;port=;Initial Catalog='';username=;password=";
            MySqlConnection connection = new MySqlConnection(connectionString);

            try
            {
                connection.Open();

                MySqlCommand cmd = new MySqlCommand("UpdateRate", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@p_DateFrom", MySqlDbType.Date).Value = dateFrom;
                cmd.Parameters.Add("@p_NewAmount", MySqlDbType.Decimal).Value = newRate;
                cmd.Parameters.Add("@p_OldAmount", MySqlDbType.Decimal).Value = oldRate;
                cmd.Parameters.Add("@p_RateType", MySqlDbType.VarChar).Value = ratetype;
                cmd.Parameters.Add("@p_Description", MySqlDbType.VarChar).Value = description;

                cmd.ExecuteNonQuery();              
                connection.Close();
                return newRate;            
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }

        private bool txtAHWHasChangedFlag;
        private bool txtOTHWHasChangedFlag;

        private void textChangedEventHandler(object sender, TextChangedEventArgs args)
        {        
            var control = sender as TextBox;

            if (control.Name == "txtAHW" )
                txtAHWHasChangedFlag = true;
            else if (control.Name == "txtOTHW")
                txtOTHWHasChangedFlag = true;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtAHWHasChangedFlag) //True regardless if changes are made in the textbox or not :(
            {
            oldAvgRate = UpdateRate(dfAHW.SelectedDate.Value.ToString("yyyy-MM-dd"), txtAHW.Text, oldAvgRate, ratetype, txtcommAHW.Text);
            MessageBox.Show("Done", "Test", MessageBoxButton.OK);
            }

            if (txtOTHWHasChangedFlag) //True regardless if changes are made in the textbox or not :(
            {
            oldOTRate = UpdateRate(dfOTHW.SelectedDate.Value.ToString("yyyy-MM-dd"), txtOTHW.Text, oldOTRate, rtOT, txtcommOTHW.Text);
            MessageBox.Show("Done", "Test", MessageBoxButton.OK);
            }

            if (!txtAHWHasChangedFlag && !txtOTHWHasChangedFlag)
            {    
            MessageBox.Show("Nothing has changed", "Test", MessageBoxButton.OK);
            return;
            }
        }
}

XAML: XAML:

<TextBox x:Name="txtAHW" TextChanged="textChangedEventHandler"/>
<TextBox x:Name="txtOTHW" TextChanged="textChangedEventHandler"/>

I have set 2 breakpoints inside inside btnSave_Click on the if statements, started the solution, changed one of the textboxes and notice that whatever I do, both statements result in True . 我在btnSave_Click内部的if语句中设置了2个断点,开始了解决方案,更改了一个文本框,并注意到无论我做什么,两个语句都将得出True Even if I disable my textboxes and click on the save button, I still get True instead of False . 即使我禁用了文本框并单击保存按钮,我仍然会得到True而不是False When I attempt to debug I notice the following error on the TextChangedEvent, for one of the textboxes that I change: 当我尝试调试时,对于我更改的文本框之一,我在TextChangedEvent上注意到以下错误:

在此处输入图片说明

I would really appreciate any suggestion. 我真的很感谢任何建议。 Thank you! 谢谢!

Attempt based on @user2107843 answer. 尝试基于@ user2107843答案。 It solves my initial problem, but when I click save the second time, it runs both stored procedures instead of only the one that as changed. 它解决了我的最初问题,但是当我第二次单击“保存”时,它将运行两个存储过程,而不是仅运行已更改的存储过程。 So if I change txtAHW and then click save, it works, it only runs the stored procedure for txtAHW. 因此,如果我更改txtAHW,然后单击“保存”,它将起作用,它将仅运行txtAHW的存储过程。 If right after that I change txtOHW as well, the stored procedure runs for both instead for running only for txtOHW. 如果之后我也更改了txtOHW,则存储过程将同时为这两个运行,而不是仅为txtOHW运行。 My logic here is that txtAHW has already been saved so no need to run again. 我的逻辑是txtAHW已经保存,因此无需再次运行。 Can some help me improve this: 可以帮助我改善这一点:

private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{        
    var control = sender as TextBox;

    if (control.Name == "txtAHW")

        if (oldAvgRate != txtAHW.Text && oldAvgRate != null)
            txtAHWHasChangedFlag = true;
        else
            txtAHWHasChangedFlag = false;
    else if (control.Name == "txtOTHW")
        if (oldOTRate != txtOTHW.Text && oldOTRate != null)
            txtOTHWHasChangedFlag = true;
        else
            txtOTHWHasChangedFlag = false;
}

You should make false in textChangedEventHandler 您应该在textChangedEventHandler中设置为false

private void textChangedEventHandler(object sender, TextChangedEventArgs args)
        {        
            var control = sender as TextBox;

            if (control.Name == "txtAHW" )

                if(oldAvgRate != txtAHW.Text && oldAvgRate !=null)
                   txtAHWHasChangedFlag = true;
                 else
                   txtAHWHasChangedFlag = false
            else if (control.Name == "txtOTHW")
                txtOTHWHasChangedFlag = true;
        }

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

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