2013-08-28 4 views
5

내 프로그램 : 텍스트 상자가 하나뿐입니다. C# 언어를 사용하여 코드를 작성하고 있습니다.워터 마크 (워터 마크)

나의 목표 : 텍스트 상자에 텍스트/워터 마크를 표시하려면 '이름을 입력하십시오'. 따라서 사용자가 텍스트 상자를 클릭하면 기본 텍스트/워터 마크가 지워지고 삭제되므로 사용자는 텍스트 상자에 이름을 입력 할 수 있습니다.

내 문제 : 온라인에서 사용할 수있는 다양한 코드를 시도했지만 그 중 아무 것도 나를 위해 작동하지 않는 것 같습니다. 그래서 저는 여기서 간단한 코드를 물어야한다고 생각했습니다. 나는 온라인으로 코드를 발견하지만 작동하지 않습니다

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      SetWatermark("Enter a text here..."); 
     } 

     private void SetWatermark(string watermark) 
     { 
      textBox1.Watermark = watermark; 
     } 
    } 
} 

오류 :

Error 1 'System.Windows.Forms.TextBox' does not contain a definition for 'Watermark' and no extension method 'Watermark' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)

당신은 내가 목표로하고있는 무슨 다른 어떤 제안이 있다면, 나는 것이다

정말 감사합니다. 나는 온라인으로 많은 예제를 피곤했지만 모두 혼란 스럽다/작동하지 않는다. 사전에 도움을 주셔서 감사합니다. :)

+0

텍스트 상자에는 워터 마크가 없습니다. 텍스트를 회색으로 표시 한 다음 무언가를 넣으면 검정색으로 자신을 만들 수 있습니다. – Jonesopolis

+0

전경을 WhiteSmoke 등으로 설정 한 다음 GotFocus 이벤트에 연결하여 색상을 지우거나 변경할 수 있습니다. – DanteTheEgregore

답변

27

방금 ​​시도했습니다. 새 Windows Forms 프로젝트에서 제대로 작동하는 것 같습니다.

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     textBox1.ForeColor = SystemColors.GrayText; 
     textBox1.Text = "Please Enter Your Name"; 
     this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave); 
     this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter); 
    } 

    private void textBox1_Leave(object sender, EventArgs e) 
    { 
     if (textBox1.Text.Length == 0) 
     { 
      textBox1.Text = "Please Enter Your Name"; 
      textBox1.ForeColor = SystemColors.GrayText; 
     } 
    } 

    private void textBox1_Enter(object sender, EventArgs e) 
    { 
     if (textBox1.Text == "Please Enter Your Name") 
     { 
      textBox1.Text = ""; 
      textBox1.ForeColor = SystemColors.WindowText; 
     } 
    } 
} 
+0

굉장! 요점으로! Jonesy, 고마워! :) – Smith

+3

당신은 * 바퀴를 재 활성화 한 것 같습니다. 왜'EM_SETCUEBANNER'를 사용하지 않겠습니까 –

+2

이 답변은 DataBinding과 잘 맞지 않을 것이라고 생각합니다. – LarsTech