2015-04-22 124 views
0

我有一个保存按钮,它抓取信息并将它存储在Winform的SQL中。 我遇到的问题是,如果您单击保存按钮,它仍将空白信息存储在数据库中。我不想要这个。这是我的代码。请记住,实际的保存,更新和获取数据是完美的。在C#Winfoms中停止按钮点击功能

 private void btnSave_Click(object sender, EventArgs e) 
    { 
     if (txtLocalSourcePath.Text != null) 
     { 
      if (ResourceKey == 0) 
      { 
       ConnString = ConfigurationManager.ConnectionStrings["Dev"].ConnectionString; 
       GetData(ConnString); 
       InsertData(ConnString); 
       GetData(ConnString); 
       lblInsertNewRecord.Visible = true; 
       lblInsertNewRecord.Text = String.Format("You have inserted a new record at {0}", System.DateTime.Now); 
      } 
      else 
      { 
       UpdateData(); 
       GetData(ConnString); 
       lblInsertNewRecord.Visible = true; 
       lblInsertNewRecord.Text = String.Format("Updated Record at {0}", System.DateTime.Now); 
      } 
      ClearData(); 
     } 
     else 
     { 
      lblInsertNewRecord.Visible = true; 
      lblInsertNewRecord.Text = "Cannot add record. Please select file."; 
     } 
    } 

我尝试了这些选项: stopping a function executed on a winform button click How to cancel any event in winforms? How to cancel winform button click event?

+0

您可能想尝试'if(txtLocalSourcePath.Text!= null && txtLocalSourcePath.Text.Length> 0)'(编辑:固定'.Length'到'.Text.Length') –

+0

好的,让我试试看。感谢您的快速响应 –

+0

我不能这样做.Length是因为它是DevExpress.XtraEditors.ButtonEdit –

回答

3

你可能想尝试if (txtLocalSourcePath.Text != null && txtLocalSourcePath.Text.Length > 0),或锤提出的解决方案:

if (!string.IsNullOrEmpty(txtLocalSourcePath.Text))

.Text属性不是其实null,但只是一个空白("")字符串。

编辑:Russ Wilson在评论中的建议也很方便:if (!string.IsNullOrWhiteSpace(txtLocalSourcePath.Text)),它保证字符串不只是空格/制表符等。

+0

完美!再次感谢您的快速响应! –

+0

@KevinFischer马克请解决,以便将其排出队列。 :) –

+0

尝试。我必须等待2分钟 –