Convert input value in a TextField : TextBox « GUI « VB.Net Tutorial






Convert input value in a TextField
Imports System.Windows.Forms

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ConvertForm
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SourceFahrenheit = New System.Windows.Forms.RadioButton
        Me.SourceCelsius = New System.Windows.Forms.RadioButton
        Me.SourceKelvin = New System.Windows.Forms.RadioButton
        Me.ValueFahrenheit = New System.Windows.Forms.TextBox
        Me.ValueCelsius = New System.Windows.Forms.TextBox
        Me.ValueKelvin = New System.Windows.Forms.TextBox
        Me.ConvertTemperature = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'SourceFahrenheit
        '
        Me.SourceFahrenheit.AutoSize = True
        Me.SourceFahrenheit.Checked = True
        Me.SourceFahrenheit.Location = New System.Drawing.Point(16, 24)
        Me.SourceFahrenheit.Name = "SourceFahrenheit"
        Me.SourceFahrenheit.Size = New System.Drawing.Size(75, 17)
        Me.SourceFahrenheit.TabIndex = 0
        Me.SourceFahrenheit.TabStop = True
        Me.SourceFahrenheit.Text = "&Fahrenheit"
        Me.SourceFahrenheit.UseVisualStyleBackColor = True
        '
        'SourceCelsius
        '
        Me.SourceCelsius.AutoSize = True
        Me.SourceCelsius.Location = New System.Drawing.Point(16, 56)
        Me.SourceCelsius.Name = "SourceCelsius"
        Me.SourceCelsius.Size = New System.Drawing.Size(58, 17)
        Me.SourceCelsius.TabIndex = 2
        Me.SourceCelsius.Text = "&Celsius"
        Me.SourceCelsius.UseVisualStyleBackColor = True
        '
        'SourceKelvin
        '
        Me.SourceKelvin.AutoSize = True
        Me.SourceKelvin.Location = New System.Drawing.Point(16, 88)
        Me.SourceKelvin.Name = "SourceKelvin"
        Me.SourceKelvin.Size = New System.Drawing.Size(53, 17)
        Me.SourceKelvin.TabIndex = 4
        Me.SourceKelvin.Text = "&kelvin"
        Me.SourceKelvin.UseVisualStyleBackColor = True
        '
        'ValueFahrenheit
        '
        Me.ValueFahrenheit.Location = New System.Drawing.Point(120, 24)
        Me.ValueFahrenheit.Name = "ValueFahrenheit"
        Me.ValueFahrenheit.Size = New System.Drawing.Size(100, 20)
        Me.ValueFahrenheit.TabIndex = 1
        '
        'ValueCelsius
        '
        Me.ValueCelsius.Location = New System.Drawing.Point(120, 56)
        Me.ValueCelsius.Name = "ValueCelsius"
        Me.ValueCelsius.Size = New System.Drawing.Size(100, 20)
        Me.ValueCelsius.TabIndex = 3
        '
        'ValueKelvin
        '
        Me.ValueKelvin.Location = New System.Drawing.Point(120, 88)
        Me.ValueKelvin.Name = "ValueKelvin"
        Me.ValueKelvin.Size = New System.Drawing.Size(100, 20)
        Me.ValueKelvin.TabIndex = 5
        '
        'ConvertTemperature
        '
        Me.ConvertTemperature.Location = New System.Drawing.Point(144, 128)
        Me.ConvertTemperature.Name = "ConvertTemperature"
        Me.ConvertTemperature.Size = New System.Drawing.Size(75, 23)
        Me.ConvertTemperature.TabIndex = 6
        Me.ConvertTemperature.Text = "Convert"
        Me.ConvertTemperature.UseVisualStyleBackColor = True
        '
        'ConvertForm
        '
        Me.AcceptButton = Me.ConvertTemperature
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(245, 169)
        Me.Controls.Add(Me.ConvertTemperature)
        Me.Controls.Add(Me.ValueKelvin)
        Me.Controls.Add(Me.ValueCelsius)
        Me.Controls.Add(Me.ValueFahrenheit)
        Me.Controls.Add(Me.SourceKelvin)
        Me.Controls.Add(Me.SourceCelsius)
        Me.Controls.Add(Me.SourceFahrenheit)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "ConvertForm"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Convert Temperatures"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents SourceFahrenheit As System.Windows.Forms.RadioButton
    Friend WithEvents SourceCelsius As System.Windows.Forms.RadioButton
    Friend WithEvents SourceKelvin As System.Windows.Forms.RadioButton
    Friend WithEvents ValueFahrenheit As System.Windows.Forms.TextBox
    Friend WithEvents ValueCelsius As System.Windows.Forms.TextBox
    Friend WithEvents ValueKelvin As System.Windows.Forms.TextBox
    Friend WithEvents ConvertTemperature As System.Windows.Forms.Button

End Class
Public Class ConvertForm
    Private Sub ConvertTemperature_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConvertTemperature.Click
        On Error Resume Next

        If (SourceFahrenheit.Checked = True) Then
            If (IsNumeric(ValueFahrenheit.Text) = True) Then
                ValueCelsius.Text = _
                   (Val(ValueFahrenheit.Text) - 32) / 1.8
                ValueKelvin.Text = _
                   ((Val(ValueFahrenheit.Text) - 32) / 1.8) + 273.15
            Else
                ValueCelsius.Text = "Error"
                ValueKelvin.Text = "Error"
            End If
        ElseIf (SourceCelsius.Checked = True) Then
            If (IsNumeric(ValueCelsius.Text) = True) Then
                ValueFahrenheit.Text = _
                   (Val(ValueCelsius.Text) * 1.8) + 32
                ValueKelvin.Text = Val(ValueCelsius.Text) + 273.15
            Else
                ValueFahrenheit.Text = "Error"
                ValueKelvin.Text = "Error"
            End If
        Else
            If (IsNumeric(ValueKelvin.Text) = True) Then
                ValueFahrenheit.Text = _
                   ((Val(ValueKelvin.Text) - 273.15) * 1.8) + 32
                ValueCelsius.Text = Val(ValueKelvin.Text) - 273.15
            Else
                ValueFahrenheit.Text = "Error"
                ValueCelsius.Text = "Error"
            End If
        End If
    End Sub
End Class


public class FahrenheitCelsiusConverter
    Public Shared Sub Main()
        Application.Run(New ConvertForm)
    End Sub
End Class








14.23.TextBox
14.23.1.Get input from TextBox and Set to LabelGet input from TextBox and Set to Label
14.23.2.Convert input value in a TextFieldConvert input value in a TextField
14.23.3.Inherit TextBox to create custom control based on TextBox
14.23.4.Display text file in a TextBoxDisplay text file in a TextBox
14.23.5.Numeric TextBoxNumeric TextBox
14.23.6.TextBox Text Changed EventTextBox Text Changed Event
14.23.7.Comprehensive Demo for TextBoxComprehensive Demo for TextBox
14.23.8.Change TextBox font by RadioButton and CheckBoxChange TextBox font by RadioButton and CheckBox
14.23.9.Get selected text in TextBoxGet selected text in TextBox
14.23.10.Create PasswordField by setting the TextBox.PasswordCharCreate PasswordField by setting the TextBox.PasswordChar
14.23.11.Copy selection text from a TextBox and paste to anotherCopy selection text from a TextBox and paste to another
14.23.12.Multiline Text BoxesMultiline Text Boxes
14.23.13.Disable TextBox Context MenuDisable TextBox Context Menu
14.23.14.Save text in TextBox to a file
14.23.15.Handles the Click event of a Button to change the background color of a TextBox.