如何在winform应用程序中禁用接受按钮属性

时间:2014-03-20 12:01:10

标签: c# winforms

我正在开发winform应用程序..
在我的表格中,我有四个文本框和一个保存按钮。在第一个按键时,我写了这样的代码:

if (e.KeyChar == (char)13)
{
    fetchdetails()
}

如果在文本框1中输入id并按Enter键,则执行fetchdetails并填充另外三个文本框。然后保存此详细信息。 如果我输入Enter按钮获取详细信息后,它将自动触发保存按钮。所以在fetchdetails()中我给出了这样的代码

this.AcceptButton=btnsave

现在发生的事情是:如果我输入id并点击enter,那么它不会填充数据。它直接保存按钮事件。
那么我能做什么呢?

2 个答案:

答案 0 :(得分:2)

在致电fetchdetails()

之前,你应该检查文本框是否有数据
if (e.KeyChar == (char)13 && textbox.text!=string.Empty)
{
fetchdetails()
}

答案 1 :(得分:1)

//Initially in the form
string lastFetchedId = string.Empty;

//KeyDown code
//Remove AcceptButton, if string is being edited etc. Anything other than enter
if (e.KeyCode != Keys.Enter) this.AcceptButton = null;
else
{
    //If something changed
    if (lastFetchedId != textBox.Text)
    {
       //Have Fetch return a true or false, after filling data in the textboxes
       if (fetchdetails(textBox.Text))
       {
         lastFetchedId = textBox.Text;
         this.AcceptButton = btnSave;
       }
    }
}
相关问题