简体   繁体   English

C#TextChanged无法启动(非ASP.net)桌面应用程序

[英]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. 我是C#的新手,并且一直在尝试学习如何使用更改后的文本,以便用户不必手动单击按钮。 But I can get my application to work with textChanged event. 但是我可以让我的应用程序可以处理textChanged事件。 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
            }
        }

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

相关问题 Asp.net上的C#Textchanged事件触发两次 - c# Textchanged event on Asp.net is firing twice 更新面板 textchanged 问题 asp.net c# - Update panel textchanged problem asp.net c# ASP.NET C#中的Word Counter桌面应用程序 - Word counter desktop application in ASP.NET C# ASP.NET/C# SelectedIndexChanged无法启动 - ASP.NET/C# SelectedIndexChanged not firing on 如何在同一解决方案中为桌面添加C#应用程序和为Web添加ASP.net应用程序 - How to add a C# application for desktop AND a ASP.net application for web in the same solution 使用C#在Asp.Net中执行Text Box_TextChanged事件? - Execute Text Box_TextChanged Event in Asp.Net using C#? 有没有办法用C#在asp.net中的textchanged事件中以编程方式打开引导程序模型? - Is there a way to open programmatically a bootstrap model in a textchanged event in asp.net with c#? Asp.net Dropdownlist选定的索引已更改且TextChanged事件未触发? (C#) - Asp.net Dropdownlist selected index changed AND TextChanged Events not Fire? (C#) 在移动版和桌面版ASP.NET C#Web应用程序(窗体)之间共享代码 - Sharing Code Between Mobile and desktop version of ASP.NET C# Web Application (Forms) 用于桌面应用程序的ASP.net? - ASP.net for desktop application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM