在 C# 中清除文字框
    
    Muhammad Maisam Abbas
    2024年2月16日
    
    Csharp
    Csharp GUI
    
 
本教程將討論清除 C# 中文字框的方法。
在 C# 中使用 String.Empty 屬性清除文字框
    
C# 中 String.Empty 屬性表示空字串。它等效於 C# 中的""。我們可以將 TextBox 的文字設定為等於 String.Empty 來清除文字框。下面的程式碼示例向我們展示瞭如何清除 C# 中帶有 String.Empty 屬性的文字框。
using System;
using System.Windows.Forms;
namespace check_if_textbox_is_empty {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e) {
      textBox1.Text = String.Empty;
    }
  }
}
在上面的程式碼中,我們通過使它等於 C# 中的 String.Empty,清除了 textBox1 文字框中的所有文字。
在 C# 中使用 TextBox.Text=""方法清除文字框
在以前的方法中,String.Empty 屬性表示一個空字串,並且等於""。我們也可以通過使 TextBox.Text 屬性等於""來清除文字框。下面的程式碼示例向我們展示瞭如何使用 C# 中的 TextBox.Text 方法清除文字框。
using System;
using System.Windows.Forms;
namespace check_if_textbox_is_empty {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e) {
      textBox1.Text = "";
    }
  }
}
在上面的程式碼中,我們通過使它等於 C# 中的"",清除了 textBox1 文字框中的所有文字。
使用 C# 中的 TextBox.Clear() 函式清除文字框
TextBox.Clear() 函式用於清除 C# 文字框中的所有文字。下面的程式碼示例向我們展示瞭如何使用 C# 中的 TextBox.Clear() 函式清除文字框。
using System;
using System.Windows.Forms;
namespace check_if_textbox_is_empty {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e) {
      textBox1.Clear();
    }
  }
}
在上面的程式碼中,我們使用 C# 中的 textBox1.Clear() 函式清除了 textBox1 文字框中的所有文字。
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn