StrataFrame Forum

Controls w/option to have a default search message

http://forum.strataframe.net/Topic25576.aspx

By Derek Price - 1/20/2010

I really like web sites that have search boxes with the grayed out text message that clears when clicked in. See attached images. It seems that this would be a great feature for the StrataFrame-wrapped controls like the textbox, combobox, etc., especially in BrowseDialogs. This would another little UI feature that would really help the product shine.



Thanks,

Derek
By Bill Cunnien - 1/20/2010

Hi Derek,



The feature could be easily implemented by extending the SF TextBox control. Simply add a property for the text that you want to display, then add some code to the get/lose focus events to manage the handling of the text for that property. Look at some other posts about subclassed controls. It is really not hard to do and you may find that other things you'd like implemented will be quite easy for you to handle.



Have a great day!

Bill
By Russell Scott Brown - 1/20/2010

Les Pinter has an article about subclassing SF controls (textbox) that might help further:

http://lespinter.com/FoxToNet/ShowArticle.aspx?ArtNum=414

By Les Pinter - 1/20/2010

Hi Derek,

   The file MyControls.vb listed below is approximately how you could do it. I couldn't resist. Just add it to your project, then drop instances on your form, setting the +Pinter/InititalText property for each textbox to the prompt you want to see in each empty textbox. (I subclassed TextBox because I just got my laptop back from HP and haven't yet reinstalled SF, but this should give you a leg up.) Some of it, like the BackColor handling, is tentative and not really necessary.

 ----------------------------------------------------------------------------------------

Imports System.Windows.Forms
Imports System.ComponentModel

Public Class MyTextBox : Inherits TextBox

Private OriginalColor As Color

Private _InitialText As String = ""
<Browsable(True), _
 Bindable(True), _
 Category("+Pinter"), _
 DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Property InitialText() As String
 Get
  Return _InitialText
 End Get
 Set(ByVal value As String)
  _InitialText = value
  Text = _InitialText
 End Set
End Property

 Sub New()

  Font = New System.Drawing.Font("Courier New", _
   9.0!, _
   System.Drawing.FontStyle.Regular, _
   System.Drawing.GraphicsUnit.Point, _
   CType(0, Byte))
   BackColor = Color.Ivory

 End Sub

 Private Sub MyTextBox_Enter( _
  ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles Me.Enter

  BackColor = Color.White
  OriginalColor = ForeColor
  ForeColor = Color.Black

  If InitialText IsNot Nothing Then
    If Text.TrimEnd() = InitialText Then
     Text = ""
    End If
  End If

 End Sub

 Private Sub MyTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles Me.Leave

  If InitialText IsNot Nothing Then
    If Text.TrimEnd() = "" Then
     Text = InitialText
    End If
  End If
  BackColor = Color.Ivory
  ForeColor = OriginalColor

 End Sub

End Class

----------------------------------------------------------------------------------------

Les

By Bill Cunnien - 1/20/2010

If the textbox is to be bound, make sure to strip that InitialText value from the Text property before saving. There may be a way to overlay that initial text, too, rather than replace the Text property. Just thinking out loud. Les have given you a great start, though. Have fun!!
By Greg McGuffey - 1/20/2010

I haven't tried this, so it might not work, but I'd actually look into overriding the OnPaint method. If there is Text set, just do default (base.OnPaint), otherwise render the InitialText. That way you wouldn't have to worry about handling accidentally saving the InitialText and you'd only have one method to override (rather than a set of events). I.e. this would more clearly separate the text that is initially shown vs. the Text that is bound.
By Greg McGuffey - 1/20/2010

I couldn't help myself. I had to try the OnPaint method. It wasn't easy. I just posted my test project in the user samples area:



http://forum.strataframe.net/FindPost25587.aspx



It is kind of cool, but doesn't perform as well as I'd like. It is a good example of how to do this sort of thing (and maybe why not to do it), so you might want to check it out. This drawing stuff isn't easy.... Blink
By Derek Price - 1/21/2010

Wow you guys are fast! I was just pondering this idea yesterday and you've already implemented it. Well done! We've sub-classed many of the controls to add our own stuff as well. It just seemed like a "wouldn't it be nice" feature for all StrataFrame users. Now I have to find the time to see what you've done.



Thanks again!

Derek