RadioButton Action Demo
Imports System
Imports System.Data
Imports System.Collections
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1()
Application.Run(form1)
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txtWords As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents radCountChars As System.Windows.Forms.RadioButton
Friend WithEvents radCountWords As System.Windows.Forms.RadioButton
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents lblResults As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtWords = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.radCountChars = New System.Windows.Forms.RadioButton()
Me.radCountWords = New System.Windows.Forms.RadioButton()
Me.Label2 = New System.Windows.Forms.Label()
Me.lblResults = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'txtWords
'
Me.txtWords.Location = New System.Drawing.Point(8, 32)
Me.txtWords.Multiline = True
Me.txtWords.Name = "txtWords"
Me.txtWords.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtWords.Size = New System.Drawing.Size(352, 152)
Me.txtWords.TabIndex = 0
Me.txtWords.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(160, 23)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Enter some text into this box?"
'
'radCountChars
'
Me.radCountChars.Checked = True
Me.radCountChars.Location = New System.Drawing.Point(8, 192)
Me.radCountChars.Name = "radCountChars"
Me.radCountChars.Size = New System.Drawing.Size(56, 32)
Me.radCountChars.TabIndex = 2
Me.radCountChars.TabStop = True
Me.radCountChars.Text = "Chars"
'
'radCountWords
'
Me.radCountWords.Location = New System.Drawing.Point(64, 192)
Me.radCountWords.Name = "radCountWords"
Me.radCountWords.Size = New System.Drawing.Size(56, 32)
Me.radCountWords.TabIndex = 3
Me.radCountWords.Text = "Words"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(128, 200)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(88, 16)
Me.Label2.TabIndex = 4
Me.Label2.Text = "The results are:"
'
'lblResults
'
Me.lblResults.Location = New System.Drawing.Point(208, 200)
Me.lblResults.Name = "lblResults"
Me.lblResults.Size = New System.Drawing.Size(80, 16)
Me.lblResults.TabIndex = 5
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(368, 229)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblResults, Me.Label2, Me.radCountWords, Me.radCountChars, Me.Label1, Me.txtWords})
Me.Name = "Form1"
Me.Text = "Word Counter"
Me.ResumeLayout(False)
End Sub
#End Region
Public Function CountCharacters(ByVal text As String) As Integer
Return text.Length
End Function
Public Function CountWords(ByVal text As String) As Integer
If txtWords.Text = "" Then Return 0
Dim words() As String = text.Split(" ".ToCharArray())
Return words.Length
End Function
Public Sub UpdateDisplay()
Dim countText As String = txtWords.Text
Dim resultText As String
If radCountWords.Checked = True Then
Dim numWords As Integer = CountWords(countText)
resultText = numWords & " words"
Else
Dim numChars As Integer = CountCharacters(countText)
resultText = numChars & " characters"
End If
lblResults.Text = resultText
End Sub
Private Sub txtWords_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWords.TextChanged
UpdateDisplay()
End Sub
Private Sub radCountWords_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountWords.CheckedChanged
UpdateDisplay()
End Sub
Private Sub radCountChars_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountChars.CheckedChanged
UpdateDisplay()
End Sub
End Class
Related examples in the same category