C# TextChanged 未触发(非 ASP.net)桌面应用程序
C# TextChanged not firing off (non ASP.net) Desktop application
我是 C# 的新手,一直在尝试学习如何使用文本更改,以便用户不必手动点击按钮。但是我可以让我的应用程序使用 textChanged 事件。我已经创建了一个测试程序来查看它是否有效,是的,它确实有效。这是我需要帮助的非工作代码,如果您需要更多,请告诉我。
形式代码:
private void custFNameTxt_TextChanged(object sender, EventArgs e)
{
searchFirstName(custFNameTxt, customers);//search first name make searched list
Console.Write("working!!!!!!!!!!!!!!!!!!!!!!!"); // for testing
}
代码是在我添加事件时添加到设计器中的:
//
// 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);
我已经尝试将处理程序添加到加载表单(这没有让它工作):
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);
}
这是它调用的方法,以防问题出在它而不是事件调用上:
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
我寻求帮助的速度太快了,看来我的按钮推送中还有一堆其他需要的代码,文本中需要这些代码才能正常工作。谢谢大家的帮助,在我得到一些建议之前,我只是想不出问题是什么。
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
}
}
我是 C# 的新手,一直在尝试学习如何使用文本更改,以便用户不必手动点击按钮。但是我可以让我的应用程序使用 textChanged 事件。我已经创建了一个测试程序来查看它是否有效,是的,它确实有效。这是我需要帮助的非工作代码,如果您需要更多,请告诉我。
形式代码:
private void custFNameTxt_TextChanged(object sender, EventArgs e)
{
searchFirstName(custFNameTxt, customers);//search first name make searched list
Console.Write("working!!!!!!!!!!!!!!!!!!!!!!!"); // for testing
}
代码是在我添加事件时添加到设计器中的:
//
// 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);
我已经尝试将处理程序添加到加载表单(这没有让它工作):
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);
}
这是它调用的方法,以防问题出在它而不是事件调用上:
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
我寻求帮助的速度太快了,看来我的按钮推送中还有一堆其他需要的代码,文本中需要这些代码才能正常工作。谢谢大家的帮助,在我得到一些建议之前,我只是想不出问题是什么。
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
}
}