简体   繁体   中英

C# TextChanged not firing off (non ASP.net) Desktop application

I am new to C# and have been trying to learn how to use text changed so that users do not have to manually hit a button. But I can get my application to work with textChanged event. I have created a test program to see if it works and yes it indeed does. Here is the non working code what I need help with, if you need more please let me know.

Code in form:

private void custFNameTxt_TextChanged(object sender, EventArgs e)
    {
        searchFirstName(custFNameTxt, customers);//search first name make searched list
        Console.Write("working!!!!!!!!!!!!!!!!!!!!!!!"); // for testing
    }

Code was added to designer when I added event:

// 
        // custFNameTxt
        // 
        this.custFNameTxt.Location = new System.Drawing.Point(98, 45);
        this.custFNameTxt.MaxLength = 12;
        this.custFNameTxt.Name = "custFNameTxt";
        this.custFNameTxt.Size = new System.Drawing.Size(171, 20);
        this.custFNameTxt.TabIndex = 1;
        this.custFNameTxt.TextChanged += new System.EventHandler(this.custFNameTxt_TextChanged);

I have tried adding the handler to the load form (this did not get it working):

private void Form1_Load(object sender, EventArgs e)//when form loads do this
    {
        //set unsername field to be selected on load
        usernameTxt.Focus();
        custFNameTxt.TextChanged += new EventHandler(custFNameTxt_TextChanged);
    }

Here is the method its calling just in case the issue is with it and not the event call:

private void searchFirstName<T>(Control textBox, List<T> list)//method to search customers by first name
    {
        if (list.GetType() == typeof(List<Customer>))
        {
            searchForThis = (textBox as TextBox).Text.ToUpper().Trim().ToString();
            for (int i = 0; i < customers.Count -1; i++)
            {
                searchThis = customers[i].F_name.ToUpper();

                if(searchThis.Substring(0, searchForThis.Length) == searchForThis)
                {
                    searched.Add(customers[i]);
                }
            }//end for loop
        }//end if type of customer

I was too quick too seek help, looks like I have a bunch of other needed code in my button push that was needed in the text changed to work right. Thank you all for the help, I just could not think of what the issue was untill I got some advise.

if (!string.IsNullOrEmpty(custFNameTxt.Text) && string.IsNullOrEmpty(custIdTxt.Text) ||
            !string.IsNullOrEmpty(custLNameTxt.Text) && !string.IsNullOrEmpty(custFNameTxt.Text) ||
            string.IsNullOrEmpty(custLNameTxt.Text) && !string.IsNullOrEmpty(custFNameTxt.Text)) 
        {
            searchFirstName(custFNameTxt, customers);//search first name make searched list
            custSearchList.Clear();
            foreach (Customer custs in searched)
            {
                altRowColor(searched, custSearchList);//iterate throught list alt row color
            }
            altCust = 0;//reset altCust to avoid errors
            if (!string.IsNullOrEmpty(custFNameTxt.Text) && !string.IsNullOrEmpty(custLNameTxt.Text))//Narrow down the search
            {
                narrowByLast();//narrow method
                custSearchList.Clear();//clear text
                foreach (Customer custs in searched)
                {
                    altRowColor(searched, custSearchList);//iterate throught list alt row color
                }
                altCust = 0;//reset altCust to avoid errors
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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