2009-03-18 3 views
3

SOLUTION行動:以下Patrick

おかげで、私は私のために働くVB.NET版にC#CodeProjectのバージョンをリファクタリングしています。うまくいけば、それは同様にあなたたちを助けることができる:カスタム透かしを入れたテキストボックス妙

Partial Public Class WatermarkedTextBox 
    Inherits TextBox 

    Private _waterMarkColor As Color = Color.LightGray 
    Public Property WaterMarkColor() As Color 
     Get 
      Return _waterMarkColor 
     End Get 
     Set(ByVal value As Color) 
      _waterMarkColor = value 
     End Set 
    End Property 
    Private _waterMarkText As String = "Watermark" 
    Public Property WaterMarkText() As String 
     Get 
      Return _waterMarkText 
     End Get 
     Set(ByVal value As String) 
      _waterMarkText = value 
     End Set 
    End Property 
    Sub New() 

    End Sub 
    Protected Overloads Overrides Sub OnCreateControl() 
     MyBase.OnCreateControl() 
    End Sub 
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
     MyBase.WndProc(m) 
     Const WM_PAINT As Integer = &HF 
     If m.Msg = WM_PAINT Then 
      If Text.Length <> 0 Then 
       Return 
      End If 
      Using g As Graphics = Me.CreateGraphics 
       g.DrawString(WaterMarkText, Me.Font, New SolidBrush(WaterMarkColor), 0, 0) 
      End Using 
     End If 
    End Sub 
    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) 
     MyBase.OnTextChanged(e) 
     Invalidate() 
    End Sub 
    Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs) 
     MyBase.OnLostFocus(e) 
     Invalidate() 
    End Sub 
    Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs) 
     MyBase.OnFontChanged(e) 
     Invalidate() 
    End Sub 
    Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs) 
     '' added so the watermark is not cleared until text is entered 
     MyBase.OnGotFocus(e) 
     Invalidate() 
    End Sub 
End Class 

を私はキオスクアプリケーションを構築しています、そして美的それを維持するために、私は、入力フィールドとして透かし入りテキストボックスを実装することを決めました。私はをCodeProjectに見つけ、それをVB.NETに変換して私のアプリケーションに入れました。それは透かしという意味で、動作し、うまくそれらをクリアしますが、私は、テキストを入力するに行くとき、これは起こる:

空:

alt text

が充填:

alt text

基本的には、ウォーターマーク全体をクリアするわけではなく、テキストボックスのデフォルトの高さに割り当てられたスペースだけです。ここで変換されたコードは次のとおりです。

Imports System 
Imports System.Collections.Generic 
Imports System.Linq 
Imports System.Text 
Imports System.Windows.Forms 
Imports System.Drawing 


Partial Public Class WatermarkedTextBox 
    Inherits TextBox 
    Private oldFont As Font = Nothing 
    Private waterMarkTextEnabled As Boolean = False 

#Region "Attributes" 
    Private _waterMarkColor As Color = Color.LightGray 
    Public Property WaterMarkColor() As Color 
     Get 
      Return _waterMarkColor 
     End Get 
     Set(ByVal value As Color) 
      _waterMarkColor = value 
      Invalidate() 
     End Set 
    End Property 
    Private _waterMarkText As String = "Water Mark" 
    Public Property WaterMarkText() As String 
     Get 
      Return _waterMarkText 
     End Get 
     Set(ByVal value As String) 
      _waterMarkText = value 
      Invalidate() 
     End Set 
    End Property 
#End Region 

    Public Sub New() 
     JoinEvents(True) 
    End Sub 
    Protected Overloads Overrides Sub OnCreateControl() 
     MyBase.OnCreateControl() 
     WaterMark_Toggle(Nothing, Nothing) 
    End Sub 
    Protected Overloads Overrides Sub OnPaint(ByVal args As PaintEventArgs) 
     'Dim drawFont As New System.Drawing.Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit) 
     Dim drawFont As New Font("Arial", 28, FontStyle.Bold) 'New System.Drawing.Font(oldFont.FontFamily, oldFont.Size, oldFont.Style, oldFont.Unit) 
     Dim drawBrush As New SolidBrush(WaterMarkColor) 
     args.Graphics.DrawString((If(waterMarkTextEnabled, WaterMarkText, Text)), drawFont, drawBrush, New PointF(0.0F, 0.0F)) 
     MyBase.OnPaint(args) 
    End Sub 

    Private Sub JoinEvents(ByVal join As Boolean) 
     If join Then 
      AddHandler Me.TextChanged, AddressOf WaterMark_Toggle 
      AddHandler Me.LostFocus, AddressOf Me.WaterMark_Toggle 
      AddHandler Me.FontChanged, AddressOf Me.WaterMark_FontChanged 
     End If 
    End Sub 

    Private Sub WaterMark_Toggle(ByVal sender As Object, ByVal args As EventArgs) 
     If Me.Text.Length <= 0 Then 
      EnableWaterMark() 
     Else 
      DisableWaterMark() 
     End If 
    End Sub 

    Private Sub EnableWaterMark() 
     oldFont = New System.Drawing.Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit) 
     Me.SetStyle(ControlStyles.UserPaint, True) 
     Me.waterMarkTextEnabled = True 
     Refresh() 
    End Sub 

    Private Sub DisableWaterMark() 
     Me.waterMarkTextEnabled = False 
     Me.SetStyle(ControlStyles.UserPaint, False) 
     If oldFont IsNot Nothing Then 
      Me.Font = New System.Drawing.Font(oldFont.FontFamily, oldFont.Size, oldFont.Style, oldFont.Unit) 
     End If 
    End Sub 

    Private Sub WaterMark_FontChanged(ByVal sender As Object, ByVal args As EventArgs) 
     If waterMarkTextEnabled Then 
      oldFont = New System.Drawing.Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit) 
      Refresh() 
     End If 
    End Sub 
End Class 

私はOnPaintイベントで私のセットのフォントサイズを使用するクラスを強制しようとしませんが、それとは運。私には欠けているものがありますが、それはこれよりも難しいものですか?

ありがとう!あなただけのDisableWaterMarkサブの最後に更新が欠落している

+0

がうれしい助けることができる何かherehereがあります。 g.DrawString(WaterMarkText、Me.Font、New SolidBrush(WaterMarkColor)、0、0) –

+0

これを追加していただきありがとうございます。色はBrushesよりも多くのオプションを持っています:D – Anders

答えて

2

Private Sub DisableWaterMark() 
    Me.waterMarkTextEnabled = False 
    Me.SetStyle(ControlStyles.UserPaint, False) 
    If oldFont IsNot Nothing Then 
     Me.Font = New System.Drawing.Font(oldFont.FontFamily, oldFont.Size, oldFont.Style, oldFont.Unit) 
    End If 
    Refresh() 
End Sub 

EDIT:

よりもむしろUserPaintコントロールのスタイルを使用して、あなたはのWndProcでWM_PAINTメッセージを処理することができますテキストが空の場合にのみウォーターマークを印刷します。しかし結果は基本的に同じです。

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
    MyBase.WndProc(m) 
    Const WM_PAINT As Integer = &HF 
    If m.Msg = WM_PAINT Then 
     If Text.Length <> 0 Then 
      Return 
     End If 
     Using g As Graphics = Me.CreateGraphics 
      g.DrawString("Water Mark", Me.Font, Brushes.LightGray, 0, 0) 
     End Using 
    End If 
End Sub 

Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) 
    MyBase.OnTextChanged(e) 
    Invalidate() 
End Sub 

Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs) 
    MyBase.OnLostFocus(e) 
    Invalidate() 
End Sub 

Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs) 
    MyBase.OnFontChanged(e) 
    Invalidate() 
End Sub 
+0

問題の一部を解決して、ウォーターマークがクリアされました!ただし、フォントは、明示的にTextBoxの宣言でフォントを設定しても、デフォルトの8pt Microsoft Sans Serifフォントに固執し続けます。 – Anders

+0

このコメントは、DisableWaterMarkサブに欠けていたものを指摘していた点に関連しています。この新しい方法を試して、すぐに報告します。 – Anders

+0

優秀、私は元の方法を削除し、あなたのものに置き換えました。チャンピオンのように動作します。私は最終版で投稿を更新します。ありがとう! – Anders

0

多分アンダースを働いているあなた

関連する問題