VB.NET文本框特殊操作方法分享

VB.NET文本框的操作方式多样化,应用起来也比较简单,开发人员可以根据不同的需求任意选择一种方法来帮助自己实现所需要的功能。那么今天介绍的VB.NET文本框的操作方法,则是针对中文输入的限制等。

下面是VB.NET文本框操作的相关代码:

 
 
 
  1. 'sunnyxing2004-04-01***修改   
  2. Public Class MyTextBox   
  3. Inherits System.Windows.Forms.TextBox   
  4. Private m_strValidText As String = "0123456789.+-" 
    & Chr(13).ToString   
  5. Private m_blnEditable As Boolean = True   
  6. #Region " Windows 窗体设计器生成的代码 "   
  7. Public Sub New()   
  8. MyBase.New()  

该调用是 Windows 窗体设计器所必需的。

 
 
 
  1. InitializeComponent()   
  2. '在 InitializeComponent() 
    调用之后添加任何初始化   
  3. End Sub   
  4. 'UserControl1 重写 dispose 以清理组件列表。   
  5. Protected Overloads Overrides 
    Sub Dispose(ByVal disposing As Boolean)   
  6. If disposing Then   
  7. If Not (components Is Nothing) Then   
  8. components.Dispose()   
  9. End If   
  10. End If   
  11. MyBase.Dispose(disposing)   
  12. End Sub  

Windows 窗体设计器所必需的

 
 
 
  1. Private components As System.ComponentModel.IContainer   
  2. '注意: 以下过程是 Windows 窗体设计器所必需的   
  3. '可以使用 Windows 窗体设计器修改此过程。   
  4. '不要使用代码编辑器修改它。   
  5. <System.Diagnostics.DebuggerStepThrough()> 
    Private Sub InitializeComponent()   
  6. components = New System.ComponentModel.Container   
  7. End Sub   
  8. #End Region   
  9. Private Sub MyTextBox_KeyPress(ByVal sender As Object, 
    ByVal e As System.Windows.Forms.KeyPressEventArgs) 
    Handles MyBase.KeyPress   
  10. Dim strLocalString As String   
  11. If EditAble Then   
  12. strLocalString = m_strValidText & Chr(8).ToString   
  13. Else   
  14. strLocalString = m_strValidText   
  15. End If   
  16. If UCase(strLocalString).IndexOf(UCase(e.KeyChar)) < 0 Then   
  17. e.Handled = True   
  18. Beep()   
  19. Else   
  20. End If   
  21. End Sub   
  22. Public Property ValidText() As String   
  23. Get   
  24. Return m_strValidText   
  25. End Get   
  26. Set(ByVal Value As String)   
  27. m_strValidText = Value   
  28. End Set   
  29. End Property   
  30. Public Property EditAble() As Boolean   
  31. Get   
  32. Return m_blnEditable   
  33. End Get   
  34. Set(ByVal Value As Boolean)   
  35. m_blnEditable = Value   
  36. End Set   
  37. End Property   
  38. Public Sub CheckText(ByVal sender As Object, ByVal e 
    As System.EventArgs) Handles MyBase.TextChanged   
  39. Dim cha As Char   
  40. Try   
  41. cha = CType(Me.Text.Substring(Me.SelectionStart - 1, 1), Char)   
  42. If m_strValidText.IndexOf(cha) < 0 Then   
  43. MeMe.Text = Me.Text.Remove(Me.SelectionStart - 1, 1)   
  44. End If   
  45. Catch ex As Exception   
  46. End Try   
  47. End Sub   
  48. End Class  

VB.NET文本框中限制中文输入的方法就为大家介绍到这里。

【编辑推荐】

  1. VB.NET变量生存周期基本概念详解
  2. VB.NET静态托盘程序编写方式浅谈
  3. VB.NET操作Word经验总结
  4. VB.NET判断数组维数具体实现方法探讨
  5. VB.NET制作图片按钮实现步骤一一讲解
THE END