Winform TextBox 提示文字

bool txtInputHasText = false;

//txtInput 獲得焦點
private void txtInput_Enter(object sender, EventArgs e) {
  if (txtInputHasText == false)
    txtInput.Text = "";
  txtInput.ForeColor = Color.Black;
}

//txtInput 失去焦點
private void txtInput_Leave(object sender, EventArgs e) {
  if (txtInput.Text == "") {
    txtInput.Text = "提示文字";
    txtInput.ForeColor = Color.Gray;
    txtInputHasText = false;
  } else
    txtInputHasText = true;
}

發佈留言