• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 39
  • Last Modified:

Custom Textbox Control

I want to have a custom text control that while editing could :

1. Convert to Proper Case.
2. Convert to Upper Case

How could I do this ?

Thank you.
0
emi_sastra
Asked:
emi_sastra
  • 2
1 Solution
 
it_saigeDeveloperCommented:
So first things first, you would want to inherit from TextBox; e.g. -
Partial Class TextBoxEx
	Inherits System.Windows.Forms.TextBox

Open in new window

Then you want to decide how a user chooses Proper Case And/Or Upper Case;  e.g. Using a Property set in the designer -
<Category("Behavior"), Description("Specifies the case-setting when the focus of the control is lost.")>
Public Property [CaseType]() As CaseType = CaseType.Standard

Open in new window

Finally, choose when the aforementioned property setting is applied; e.g. OnLostFocus -
Protected Overrides Sub OnLostFocus(e As EventArgs)
	Select Case [CaseType]
		Case CaseType.Proper
			Text = ConvertToProperCase(Text)
		Case CaseType.Upper
			Text = ConvertToUpperCase(Text)
		Case CaseType.Standard
		Case Else
			Exit Select
	End Select
	MyBase.OnLostFocus(e)
End Sub

Open in new window


Full Implementation w/o Conversion methods -

TextBoxEx.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class TextBoxEx
	Inherits System.Windows.Forms.TextBox

	'Control overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

	'Required by the Control Designer
	Private components As System.ComponentModel.IContainer

	' NOTE: The following procedure is required by the Component Designer
	' It can be modified using the Component Designer.  Do not modify it
	' using the code editor.
	<System.Diagnostics.DebuggerStepThrough()> _
	Private Sub InitializeComponent()
		components = New System.ComponentModel.Container()
	End Sub

End Class

Open in new window

TextBoxEx.vb -
Imports System.ComponentModel

Public Class TextBoxEx

	<Category("Behaviour"), Description("Specifies the case-setting when the focus of the control is lost.")>
	Public Property [CaseType]() As CaseType = CaseType.Standard
	Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
		MyBase.OnPaint(e)

		'Add your custom paint code here
	End Sub

	Protected Overrides Sub OnLostFocus(e As EventArgs)
		Select Case [CaseType]
			Case CaseType.Proper
				Text = ConvertToProperCase(Text)
			Case CaseType.Upper
				Text = ConvertToUpperCase(Text)
			Case CaseType.Standard
			Case Else
				Exit Select
		End Select
		MyBase.OnLostFocus(e)
	End Sub
End Class

Public Enum CaseType As Integer
	Standard = 0
	Proper = 1
	Upper = 2
End Enum

Open in new window

-saige-
1
 
emi_sastraAuthor Commented:
Hi Saige,

Thank you, let me try it first.
0
 
emi_sastraAuthor Commented:
Hi Saige,

Sorry for late reply.
Thank you very much for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now