[英]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.