Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

Remove all chars but a certain set

I am using this code:

 Private Sub allowedchars(ByVal sender As Object, ByRef e As System.Windows.Forms.KeyEventArgs)
        Select Case e.KeyCode
            Case 8, 46, 48 To 57, 96 To 105, 190
            Case Else
                e.Handled = True
        End Select
    End Sub
    Private Sub IoIbox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles IoIbox.KeyDown
        allowedchars(sender, e)
    End Sub

to disallow all characters except back (backspace), delete, 0-9 (num), 0-9 (normal), and . (keyboard).
As of now, it does not filter the keys that are pressed in the box.  Any suggestions?
0
Ander5
Asked:
Ander5
  • 10
  • 5
  • 2
1 Solution
 
wd006451Commented:
   Private Sub IoIbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IoIbox.KeyPress
        Dim IntegerOnly As Boolean
        Dim PositiveOnly As Boolean
        Dim KeyAscii As Integer
        KeyAscii = Asc(e.KeyChar)
        Select Case KeyAscii
            Case 48 To 57, 8, 13
            Case 45
                If PositiveOnly Then
                    KeyAscii = 0
                Else
                    If InStr(Me.Text, "-") <> 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case 46
                If IntegerOnly Then
                    KeyAscii = 0
                Else
                    If InStr(Me.Text, ".") <> 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case Else
                KeyAscii = 0
        End Select
        If KeyAscii = 0 Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End Sub
0
 
wd006451Commented:
Hope this helps
0
 
Ander5Author Commented:
It does and it doesn't, it only allows the numbers and periods (yay!) but now the program doesn't calculate anymore.  If it is of help, it calculates angles and indexes of light rays through different mediums (Snell's Law).  If you need me to, I will paste the code here...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
wd006451Commented:
Yes, please if that is ok.  It would help, or you could m s g me a t wd006451 at tagcase .com
0
 
Ander5Author Commented:
Imports System.Math
Public Class Form1
#Region " Code "
    Inherits System.Windows.Forms.Form
    Dim AoI, AoR, IoI, IoR As Double
#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 AoIbox As System.Windows.Forms.TextBox
    Friend WithEvents AoRbox As System.Windows.Forms.TextBox
    Friend WithEvents mat1 As System.Windows.Forms.Label
    Friend WithEvents Mat2 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents AoIl As System.Windows.Forms.Label
    Friend WithEvents IoR2l As System.Windows.Forms.Label
    Friend WithEvents AoRl As System.Windows.Forms.Label
    Friend WithEvents IoIbox As System.Windows.Forms.TextBox
    Friend WithEvents IoIl As System.Windows.Forms.Label
    Friend WithEvents IoRbox As System.Windows.Forms.TextBox
    Friend WithEvents clrall As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.IoIbox = New System.Windows.Forms.TextBox
        Me.AoIbox = New System.Windows.Forms.TextBox
        Me.IoRbox = New System.Windows.Forms.TextBox
        Me.AoRbox = New System.Windows.Forms.TextBox
        Me.IoIl = New System.Windows.Forms.Label
        Me.AoIl = New System.Windows.Forms.Label
        Me.IoR2l = New System.Windows.Forms.Label
        Me.AoRl = New System.Windows.Forms.Label
        Me.mat1 = New System.Windows.Forms.Label
        Me.Mat2 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.clrall = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'IoIbox
        '
        Me.IoIbox.Location = New System.Drawing.Point(136, 48)
        Me.IoIbox.MaxLength = 10
        Me.IoIbox.Name = "IoIbox"
        Me.IoIbox.TabIndex = 0
        Me.IoIbox.Text = ""
        '
        'AoIbox
        '
        Me.AoIbox.Location = New System.Drawing.Point(136, 80)
        Me.AoIbox.MaxLength = 10
        Me.AoIbox.Name = "AoIbox"
        Me.AoIbox.TabIndex = 1
        Me.AoIbox.Text = ""
        '
        'IoRbox
        '
        Me.IoRbox.Location = New System.Drawing.Point(136, 168)
        Me.IoRbox.MaxLength = 10
        Me.IoRbox.Name = "IoRbox"
        Me.IoRbox.TabIndex = 2
        Me.IoRbox.Text = ""
        '
        'AoRbox
        '
        Me.AoRbox.Location = New System.Drawing.Point(136, 200)
        Me.AoRbox.MaxLength = 10
        Me.AoRbox.Name = "AoRbox"
        Me.AoRbox.TabIndex = 3
        Me.AoRbox.Text = ""
        '
        'IoIl
        '
        Me.IoIl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.IoIl.Location = New System.Drawing.Point(8, 48)
        Me.IoIl.Name = "IoIl"
        Me.IoIl.Size = New System.Drawing.Size(120, 23)
        Me.IoIl.TabIndex = 4
        Me.IoIl.Text = "Index of Incidence:"
        Me.IoIl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AoIl
        '
        Me.AoIl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.AoIl.Location = New System.Drawing.Point(8, 80)
        Me.AoIl.Name = "AoIl"
        Me.AoIl.Size = New System.Drawing.Size(120, 23)
        Me.AoIl.TabIndex = 5
        Me.AoIl.Text = "Angle of Incidence:"
        Me.AoIl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'IoR2l
        '
        Me.IoR2l.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.IoR2l.Location = New System.Drawing.Point(8, 168)
        Me.IoR2l.Name = "IoR2l"
        Me.IoR2l.Size = New System.Drawing.Size(120, 23)
        Me.IoR2l.TabIndex = 6
        Me.IoR2l.Text = "Index of Refraction:"
        Me.IoR2l.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AoRl
        '
        Me.AoRl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.AoRl.Location = New System.Drawing.Point(8, 200)
        Me.AoRl.Name = "AoRl"
        Me.AoRl.Size = New System.Drawing.Size(120, 23)
        Me.AoRl.TabIndex = 7
        Me.AoRl.Text = "Angle of Refraction:"
        Me.AoRl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'mat1
        '
        Me.mat1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.mat1.Location = New System.Drawing.Point(8, 8)
        Me.mat1.Name = "mat1"
        Me.mat1.TabIndex = 8
        Me.mat1.Text = "Medium 1"
        Me.mat1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Mat2
        '
        Me.Mat2.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Mat2.Location = New System.Drawing.Point(8, 128)
        Me.Mat2.Name = "Mat2"
        Me.Mat2.TabIndex = 9
        Me.Mat2.Text = "Medium 2"
        Me.Mat2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Button1
        '
        Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.Button1.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(160, 232)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 10
        Me.Button1.Text = "Calculate"
        '
        'clrall
        '
        Me.clrall.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.clrall.Location = New System.Drawing.Point(8, 232)
        Me.clrall.Name = "clrall"
        Me.clrall.TabIndex = 11
        Me.clrall.Text = "Clear All"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(248, 264)
        Me.Controls.Add(Me.clrall)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Mat2)
        Me.Controls.Add(Me.mat1)
        Me.Controls.Add(Me.AoRl)
        Me.Controls.Add(Me.IoR2l)
        Me.Controls.Add(Me.AoIl)
        Me.Controls.Add(Me.IoIl)
        Me.Controls.Add(Me.AoRbox)
        Me.Controls.Add(Me.IoRbox)
        Me.Controls.Add(Me.AoIbox)
        Me.Controls.Add(Me.IoIbox)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.KeyPreview = True
        Me.Name = "Form1"
        Me.Text = "Snell's Law"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clrbox()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If IoIbox.Text = Nothing Then
                AoI = AoIbox.Text
                AoR = AoRbox.Text
                IoR = IoRbox.Text
                calcIoI(AoI, AoR, IoR)
            ElseIf AoIbox.Text = Nothing Then
                AoR = AoRbox.Text
                IoI = IoIbox.Text
                IoR = IoRbox.Text
                calcAoI(AoR, IoI, IoR)
            ElseIf IoRbox.Text = Nothing Then
                AoI = AoIbox.Text
                AoR = AoRbox.Text
                IoI = IoIbox.Text
                calcIoR(AoI, AoR, IoI)
            ElseIf AoRbox.Text = Nothing Then
                AoI = AoIbox.Text
                IoI = IoIbox.Text
                IoR = IoRbox.Text
                calcAoR(AoI, IoI, IoR)
            ElseIf Not IoIbox.Text = Nothing And Not AoIbox.Text = Nothing _
            And Not IoRbox.Text = Nothing And Not AoRbox.Text = Nothing Then
                MsgBox("Enter exactly 3 variables", MsgBoxStyle.Exclamation)
            End If
        Catch
            MsgBox("Enter exactly 3 variables", MsgBoxStyle.Exclamation)
        End Try

    End Sub
    Private Sub clrall_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clrall.Click
        clrbox()
    End Sub
    Private Sub clrbox()
        IoIbox.Text = Nothing
        AoIbox.Text = Nothing
        IoRbox.Text = Nothing
        AoRbox.Text = Nothing
        IoIbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        AoIbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        IoRbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        AoRbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
    End Sub
    Private Sub allowedchars(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim IntegerOnly As Boolean
        Dim PositiveOnly As Boolean
        Dim KeyAscii As Integer
        KeyAscii = Asc(e.KeyChar)
        Select Case KeyAscii
            Case 48 To 57, 8, 13
            Case 45
                If PositiveOnly Then
                    KeyAscii = 0
                Else
                    If InStr(Me.Text, "-") <> 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case 46
                If IntegerOnly Then
                    KeyAscii = 0
                Else
                    If InStr(Me.Text, ".") <> 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case Else
                KeyAscii = 0
        End Select
        If KeyAscii = 0 Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End Sub
   
#End Region
#Region " Calculations "
    Private Sub calcIoI(ByVal AoI As Double, ByVal AoR As Double, ByVal IoR As Double)
        IoIbox.Font = New Font(IoIbox.Font, FontStyle.Bold)
        AoR = Sin((AoR * PI) / 180)
        AoI = Sin((AoI * PI) / 180)
        IoI = (IoR * AoR) / AoI
        IoIbox.Text = IoI.ToString.Substring(0, 10)
    End Sub
    Private Sub calcAoI(ByVal AoR As Double, ByVal IoI As Double, ByVal IoR As Double)
        AoIbox.Font = New Font(AoIbox.Font, FontStyle.Bold)
        AoR = AoR * (PI / 180)
        AoI = (IoR * Sin(AoR)) / IoI
        AoI = Asin(AoI)
        AoI = AoI * (180 / PI)

        AoIbox.Text = AoI.ToString.Substring(0, 10)
    End Sub
    Private Sub calcIoR(ByVal AoI As Double, ByVal AoR As Double, ByVal IoI As Double)
        IoRbox.Font = New Font(IoRbox.Font, FontStyle.Bold)
        AoI = Sin((AoI * PI) / 180)
        AoR = Sin((AoR * PI) / 180)
        IoR = (IoI * AoI) / AoR
        IoRbox.Text = IoR.ToString.Substring(0, 10)
    End Sub
    Private Sub calcAoR(ByVal AoI As Double, ByVal AoR As Double, ByVal IoR As Double)
        AoRbox.Font = New Font(AoRbox.Font, FontStyle.Bold)
        AoI = AoI * (PI / 180)
        AoR = (IoI * Sin(AoI)) / IoR
        AoR = Asin(AoR)
        AoR = AoR * (180 / PI)

        'AoRbox.Text = AoR.ToString.Substring(0, 10)
        'AoRbox.Text = AoR.ToString.Substring(AoR.ToString.LastIndexOf("." + 1))
    End Sub
#End Region

disregard the commented lines, I was trying some stuff out
0
 
Ander5Author Commented:
oops, it didn't get the keydown subs:

    Private Sub IoIbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IoIbox.KeyPress
        allowedchars(sender, e)
    End Sub
    Private Sub AoIbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles AoIbox.KeyPress
        allowedchars(sender, e)
    End Sub
    Private Sub IoRbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IoRbox.KeyPress
        allowedchars(sender, e)
    End Sub
    Private Sub Aorbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles AoRbox.KeyPress
        allowedchars(sender, e)
    End Sub

Before end of  " Code Region "
0
 
wd006451Commented:
Can you repaste the code for me?
0
 
S-TwilleyCommented:
Might I suggest one thing... if you have multiple textboxes that you want to filter keypresses... either have all of them using the same keypress event (almost like you are) or design an inherited control based on a textbox... as for your implementation, it can be shortened:

   Private Sub IoIbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IoIbox.KeyPress, AoIbox.KeyPress, IoRbox.KeyPress, AoRbox.KeyPress
        allowedchars(sender, e)
    End Sub



0
 
S-TwilleyCommented:
you might want to rename it though (forgot) :P

Private Sub FilteredTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IoIbox.KeyPress, AoIbox.KeyPress, IoRbox.KeyPress, AoRbox.KeyPress
        allowedchars(sender, e)
    End Sub
0
 
wd006451Commented:
Can you give me some samples?
0
 
wd006451Commented:
(that is...some sample numbers to put in...)
0
 
wd006451Commented:
The funny part, i don't think the issue is with the numbers or the textboxes right now, try to troubleshoot the calc functions....


But here is what you should have for now....

Imports System.Math
Public Class Form1
#Region " Code "

    Inherits System.Windows.Forms.Form
    Dim AoI, AoR, IoI, IoR As Double


#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 AoIbox As System.Windows.Forms.TextBox
    Friend WithEvents AoRbox As System.Windows.Forms.TextBox
    Friend WithEvents mat1 As System.Windows.Forms.Label
    Friend WithEvents Mat2 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents AoIl As System.Windows.Forms.Label
    Friend WithEvents IoR2l As System.Windows.Forms.Label
    Friend WithEvents AoRl As System.Windows.Forms.Label
    Friend WithEvents IoIbox As System.Windows.Forms.TextBox
    Friend WithEvents IoIl As System.Windows.Forms.Label
    Friend WithEvents IoRbox As System.Windows.Forms.TextBox
    Friend WithEvents clrall As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.IoIbox = New System.Windows.Forms.TextBox
        Me.AoIbox = New System.Windows.Forms.TextBox
        Me.IoRbox = New System.Windows.Forms.TextBox
        Me.AoRbox = New System.Windows.Forms.TextBox
        Me.IoIl = New System.Windows.Forms.Label
        Me.AoIl = New System.Windows.Forms.Label
        Me.IoR2l = New System.Windows.Forms.Label
        Me.AoRl = New System.Windows.Forms.Label
        Me.mat1 = New System.Windows.Forms.Label
        Me.Mat2 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.clrall = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'IoIbox
        '
        Me.IoIbox.Location = New System.Drawing.Point(136, 48)
        Me.IoIbox.MaxLength = 10
        Me.IoIbox.Name = "IoIbox"
        Me.IoIbox.TabIndex = 0
        Me.IoIbox.Text = ""
        '
        'AoIbox
        '
        Me.AoIbox.Location = New System.Drawing.Point(136, 80)
        Me.AoIbox.MaxLength = 10
        Me.AoIbox.Name = "AoIbox"
        Me.AoIbox.TabIndex = 1
        Me.AoIbox.Text = ""
        '
        'IoRbox
        '
        Me.IoRbox.Location = New System.Drawing.Point(136, 168)
        Me.IoRbox.MaxLength = 10
        Me.IoRbox.Name = "IoRbox"
        Me.IoRbox.TabIndex = 2
        Me.IoRbox.Text = ""
        '
        'AoRbox
        '
        Me.AoRbox.Location = New System.Drawing.Point(136, 200)
        Me.AoRbox.MaxLength = 10
        Me.AoRbox.Name = "AoRbox"
        Me.AoRbox.TabIndex = 3
        Me.AoRbox.Text = ""
        '
        'IoIl
        '
        Me.IoIl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.IoIl.Location = New System.Drawing.Point(8, 48)
        Me.IoIl.Name = "IoIl"
        Me.IoIl.Size = New System.Drawing.Size(120, 23)
        Me.IoIl.TabIndex = 4
        Me.IoIl.Text = "Index of Incidence:"
        Me.IoIl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AoIl
        '
        Me.AoIl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.AoIl.Location = New System.Drawing.Point(8, 80)
        Me.AoIl.Name = "AoIl"
        Me.AoIl.Size = New System.Drawing.Size(120, 23)
        Me.AoIl.TabIndex = 5
        Me.AoIl.Text = "Angle of Incidence:"
        Me.AoIl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'IoR2l
        '
        Me.IoR2l.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.IoR2l.Location = New System.Drawing.Point(8, 168)
        Me.IoR2l.Name = "IoR2l"
        Me.IoR2l.Size = New System.Drawing.Size(120, 23)
        Me.IoR2l.TabIndex = 6
        Me.IoR2l.Text = "Index of Refraction:"
        Me.IoR2l.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AoRl
        '
        Me.AoRl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.AoRl.Location = New System.Drawing.Point(8, 200)
        Me.AoRl.Name = "AoRl"
        Me.AoRl.Size = New System.Drawing.Size(120, 23)
        Me.AoRl.TabIndex = 7
        Me.AoRl.Text = "Angle of Refraction:"
        Me.AoRl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'mat1
        '
        Me.mat1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.mat1.Location = New System.Drawing.Point(8, 8)
        Me.mat1.Name = "mat1"
        Me.mat1.TabIndex = 8
        Me.mat1.Text = "Medium 1"
        Me.mat1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Mat2
        '
        Me.Mat2.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Mat2.Location = New System.Drawing.Point(8, 128)
        Me.Mat2.Name = "Mat2"
        Me.Mat2.TabIndex = 9
        Me.Mat2.Text = "Medium 2"
        Me.Mat2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Button1
        '
        Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.Button1.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(160, 232)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 10
        Me.Button1.Text = "Calculate"
        '
        'clrall
        '
        Me.clrall.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.clrall.Location = New System.Drawing.Point(8, 232)
        Me.clrall.Name = "clrall"
        Me.clrall.TabIndex = 11
        Me.clrall.Text = "Clear All"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(248, 264)
        Me.Controls.Add(Me.clrall)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Mat2)
        Me.Controls.Add(Me.mat1)
        Me.Controls.Add(Me.AoRl)
        Me.Controls.Add(Me.IoR2l)
        Me.Controls.Add(Me.AoIl)
        Me.Controls.Add(Me.IoIl)
        Me.Controls.Add(Me.AoRbox)
        Me.Controls.Add(Me.IoRbox)
        Me.Controls.Add(Me.AoIbox)
        Me.Controls.Add(Me.IoIbox)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.KeyPreview = True
        Me.Name = "Form1"
        Me.Text = "Snell's Law"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clrbox()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If IoIbox.Text = Nothing Then
                AoI = AoIbox.Text
                AoR = AoRbox.Text
                IoR = IoRbox.Text
                calcIoI(AoI, AoR, IoR)
            ElseIf AoIbox.Text = Nothing Then
                AoR = AoRbox.Text
                IoI = IoIbox.Text
                IoR = IoRbox.Text
                calcAoI(AoR, IoI, IoR)
            ElseIf IoRbox.Text = Nothing Then
                AoI = AoIbox.Text
                AoR = AoRbox.Text
                IoI = IoIbox.Text
                calcIoR(AoI, AoR, IoI)
            ElseIf AoRbox.Text = Nothing Then
                AoI = AoIbox.Text
                IoI = IoIbox.Text
                IoR = IoRbox.Text
                calcAoR(AoI, IoI, IoR)
            ElseIf Not IoIbox.Text = Nothing And Not AoIbox.Text = Nothing _
            And Not IoRbox.Text = Nothing And Not AoRbox.Text = Nothing Then
                MsgBox("Enter exactly 3 variables", MsgBoxStyle.Exclamation)
            End If
        Catch
            MsgBox("Enter exactly 3 variables", MsgBoxStyle.Exclamation)
        End Try

    End Sub
    Private Sub clrall_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clrall.Click
        clrbox()
    End Sub
    Private Sub clrbox()
        IoIbox.Text = Nothing
        AoIbox.Text = Nothing
        IoRbox.Text = Nothing
        AoRbox.Text = Nothing
        IoIbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        AoIbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        IoRbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        AoRbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
    End Sub
    Private Sub allowedchars(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IoIbox.KeyPress, AoIbox.KeyPress, IoRbox.KeyPress, AoRbox.KeyPress

        Dim IntegerOnly As Boolean
        Dim PositiveOnly As Boolean
        Dim KeyAscii As Integer
        KeyAscii = Asc(e.KeyChar)
        Select Case KeyAscii
            Case 48 To 57, 8, 13
            Case 45
                If PositiveOnly Then
                    KeyAscii = 0
                Else
                    If InStr(Me.Text, "-") <> 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case 46
                If IntegerOnly Then
                    KeyAscii = 0
                Else
                    If InStr(Me.Text, ".") <> 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case Else
                KeyAscii = 0
        End Select
        If KeyAscii = 0 Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End Sub

#End Region
#Region " Calculations "
    Private Sub calcIoI(ByVal AoI As Double, ByVal AoR As Double, ByVal IoR As Double)
        IoIbox.Font = New Font(IoIbox.Font, FontStyle.Bold)
        AoR = Sin((AoR * PI) / 180)
        AoI = Sin((AoI * PI) / 180)
        IoI = (IoR * AoR) / AoI
        IoIbox.Text = IoI.ToString.Substring(0, 10)
    End Sub
    Private Sub calcAoI(ByVal AoR As Double, ByVal IoI As Double, ByVal IoR As Double)
        AoIbox.Font = New Font(AoIbox.Font, FontStyle.Bold)
        AoR = AoR * (PI / 180)
        AoI = (IoR * Sin(AoR)) / IoI
        AoI = Asin(AoI)
        AoI = AoI * (180 / PI)

        AoIbox.Text = AoI.ToString.Substring(0, 10)
    End Sub
    Private Sub calcIoR(ByVal AoI As Double, ByVal AoR As Double, ByVal IoI As Double)
        IoRbox.Font = New Font(IoRbox.Font, FontStyle.Bold)
        AoI = Sin((AoI * PI) / 180)
        AoR = Sin((AoR * PI) / 180)
        IoR = (IoI * AoI) / AoR
        IoRbox.Text = IoR.ToString.Substring(0, 10)
    End Sub
    Private Sub calcAoR(ByVal AoI As Double, ByVal AoR As Double, ByVal IoR As Double)
        AoRbox.Font = New Font(AoRbox.Font, FontStyle.Bold)
        AoI = AoI * (PI / 180)
        AoR = (IoI * Sin(AoI)) / IoR
        AoR = Asin(AoR)
        AoR = AoR * (180 / PI)

        'AoRbox.Text = AoR.ToString.Substring(0, 10)
        'AoRbox.Text = AoR.ToString.Substring(AoR.ToString.LastIndexOf("." + 1))
    End Sub
#End Region

End Class
0
 
wd006451Commented:
Try to see if this is working, it works for me...
Imports System.Math
Public Class Form1
#Region " Code "

    Inherits System.Windows.Forms.Form
    Dim AoI, AoR, IoI, IoR As Double


#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 AoIbox As System.Windows.Forms.TextBox
    Friend WithEvents AoRbox As System.Windows.Forms.TextBox
    Friend WithEvents mat1 As System.Windows.Forms.Label
    Friend WithEvents Mat2 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents AoIl As System.Windows.Forms.Label
    Friend WithEvents IoR2l As System.Windows.Forms.Label
    Friend WithEvents AoRl As System.Windows.Forms.Label
    Friend WithEvents IoIbox As System.Windows.Forms.TextBox
    Friend WithEvents IoIl As System.Windows.Forms.Label
    Friend WithEvents IoRbox As System.Windows.Forms.TextBox
    Friend WithEvents clrall As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.IoIbox = New System.Windows.Forms.TextBox
        Me.AoIbox = New System.Windows.Forms.TextBox
        Me.IoRbox = New System.Windows.Forms.TextBox
        Me.AoRbox = New System.Windows.Forms.TextBox
        Me.IoIl = New System.Windows.Forms.Label
        Me.AoIl = New System.Windows.Forms.Label
        Me.IoR2l = New System.Windows.Forms.Label
        Me.AoRl = New System.Windows.Forms.Label
        Me.mat1 = New System.Windows.Forms.Label
        Me.Mat2 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.clrall = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'IoIbox
        '
        Me.IoIbox.Location = New System.Drawing.Point(136, 48)
        Me.IoIbox.MaxLength = 10
        Me.IoIbox.Name = "IoIbox"
        Me.IoIbox.TabIndex = 0
        Me.IoIbox.Text = ""
        '
        'AoIbox
        '
        Me.AoIbox.Location = New System.Drawing.Point(136, 80)
        Me.AoIbox.MaxLength = 10
        Me.AoIbox.Name = "AoIbox"
        Me.AoIbox.TabIndex = 1
        Me.AoIbox.Text = ""
        '
        'IoRbox
        '
        Me.IoRbox.Location = New System.Drawing.Point(136, 168)
        Me.IoRbox.MaxLength = 10
        Me.IoRbox.Name = "IoRbox"
        Me.IoRbox.TabIndex = 2
        Me.IoRbox.Text = ""
        '
        'AoRbox
        '
        Me.AoRbox.Location = New System.Drawing.Point(136, 200)
        Me.AoRbox.MaxLength = 10
        Me.AoRbox.Name = "AoRbox"
        Me.AoRbox.TabIndex = 3
        Me.AoRbox.Text = ""
        '
        'IoIl
        '
        Me.IoIl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.IoIl.Location = New System.Drawing.Point(8, 48)
        Me.IoIl.Name = "IoIl"
        Me.IoIl.Size = New System.Drawing.Size(120, 23)
        Me.IoIl.TabIndex = 4
        Me.IoIl.Text = "Index of Incidence:"
        Me.IoIl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AoIl
        '
        Me.AoIl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.AoIl.Location = New System.Drawing.Point(8, 80)
        Me.AoIl.Name = "AoIl"
        Me.AoIl.Size = New System.Drawing.Size(120, 23)
        Me.AoIl.TabIndex = 5
        Me.AoIl.Text = "Angle of Incidence:"
        Me.AoIl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'IoR2l
        '
        Me.IoR2l.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.IoR2l.Location = New System.Drawing.Point(8, 168)
        Me.IoR2l.Name = "IoR2l"
        Me.IoR2l.Size = New System.Drawing.Size(120, 23)
        Me.IoR2l.TabIndex = 6
        Me.IoR2l.Text = "Index of Refraction:"
        Me.IoR2l.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'AoRl
        '
        Me.AoRl.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.AoRl.Location = New System.Drawing.Point(8, 200)
        Me.AoRl.Name = "AoRl"
        Me.AoRl.Size = New System.Drawing.Size(120, 23)
        Me.AoRl.TabIndex = 7
        Me.AoRl.Text = "Angle of Refraction:"
        Me.AoRl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'mat1
        '
        Me.mat1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.mat1.Location = New System.Drawing.Point(8, 8)
        Me.mat1.Name = "mat1"
        Me.mat1.TabIndex = 8
        Me.mat1.Text = "Medium 1"
        Me.mat1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Mat2
        '
        Me.Mat2.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Mat2.Location = New System.Drawing.Point(8, 128)
        Me.Mat2.Name = "Mat2"
        Me.Mat2.TabIndex = 9
        Me.Mat2.Text = "Medium 2"
        Me.Mat2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Button1
        '
        Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.Button1.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(160, 232)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 10
        Me.Button1.Text = "Calculate"
        '
        'clrall
        '
        Me.clrall.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.clrall.Location = New System.Drawing.Point(8, 232)
        Me.clrall.Name = "clrall"
        Me.clrall.TabIndex = 11
        Me.clrall.Text = "Clear All"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(248, 264)
        Me.Controls.Add(Me.clrall)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Mat2)
        Me.Controls.Add(Me.mat1)
        Me.Controls.Add(Me.AoRl)
        Me.Controls.Add(Me.IoR2l)
        Me.Controls.Add(Me.AoIl)
        Me.Controls.Add(Me.IoIl)
        Me.Controls.Add(Me.AoRbox)
        Me.Controls.Add(Me.IoRbox)
        Me.Controls.Add(Me.AoIbox)
        Me.Controls.Add(Me.IoIbox)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.KeyPreview = True
        Me.Name = "Form1"
        Me.Text = "Snell's Law"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clrbox()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If IoIbox.Text = Nothing Then
                AoI = AoIbox.Text
                AoR = AoRbox.Text
                IoR = IoRbox.Text
                calcIoI(AoI, AoR, IoR)
            ElseIf AoIbox.Text = Nothing Then
                AoR = AoRbox.Text
                IoI = IoIbox.Text
                IoR = IoRbox.Text
                calcAoI(AoR, IoI, IoR)
            ElseIf IoRbox.Text = Nothing Then
                AoI = AoIbox.Text
                AoR = AoRbox.Text
                IoI = IoIbox.Text
                calcIoR(AoI, AoR, IoI)
            ElseIf AoRbox.Text = Nothing Then
                AoI = AoIbox.Text
                IoI = IoIbox.Text
                IoR = IoRbox.Text
                calcAoR(AoI, IoI, IoR)
                'ElseIf Not IoIbox.Text = Nothing And Not AoIbox.Text = Nothing _
                'And Not IoRbox.Text = Nothing And Not AoRbox.Text = Nothing Then
                'MsgBox("Enter exactly 3 variables", MsgBoxStyle.Exclamation)
            End If
        Catch
            MsgBox("Enter exactly 3 variables", MsgBoxStyle.Exclamation)
        End Try

    End Sub
    Private Sub clrall_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clrall.Click
        clrbox()
    End Sub
    Private Sub clrbox()
        IoIbox.Text = Nothing
        AoIbox.Text = Nothing
        IoRbox.Text = Nothing
        AoRbox.Text = Nothing
        IoIbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        AoIbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        IoRbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
        AoRbox.Font = New Font(IoIbox.Font, FontStyle.Regular)
    End Sub
    Private Sub allowedchars(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IoIbox.KeyPress, AoIbox.KeyPress, IoRbox.KeyPress, AoRbox.KeyPress

        Dim IntegerOnly As Boolean
        Dim PositiveOnly As Boolean
        Dim KeyAscii As Integer
        KeyAscii = Asc(e.KeyChar)
        Select Case KeyAscii
            Case 48 To 57, 8, 13
            Case 45
                If PositiveOnly Then
                    KeyAscii = 0
                Else
                    If InStr(Me.Text, "-") <> 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case 46
                If IntegerOnly Then
                    KeyAscii = 0
                Else
                    If InStr(Me.Text, ".") <> 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case Else
                KeyAscii = 0
        End Select
        If KeyAscii = 0 Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End Sub

#End Region
#Region " Calculations "
    Private Sub calcIoI(ByVal AoI As Double, ByVal AoR As Double, ByVal IoR As Double)
        IoIbox.Font = New Font(IoIbox.Font, FontStyle.Bold)
        AoR = Sin((AoR * PI) / 180)
        AoI = Sin((AoI * PI) / 180)
        IoI = (IoR * AoR) / AoI
        IoIbox.Text = IoI.ToString.Substring(0, 10)
    End Sub
    Private Sub calcAoI(ByVal AoR As Double, ByVal IoI As Double, ByVal IoR As Double)
        AoIbox.Font = New Font(AoIbox.Font, FontStyle.Bold)
        AoR = AoR * (PI / 180)
        AoI = (IoR * Sin(AoR)) / IoI
        AoI = Asin(AoI)
        AoI = AoI * (180 / PI)

        AoIbox.Text = AoI.ToString.Substring(0, 10)
    End Sub
    Private Sub calcIoR(ByVal AoI As Double, ByVal AoR As Double, ByVal IoI As Double)
        IoRbox.Font = New Font(IoRbox.Font, FontStyle.Bold)
        AoI = Sin((AoI * PI) / 180)
        AoR = Sin((AoR * PI) / 180)
        IoR = (IoI * AoI) / AoR
        IoRbox.Text = IoR.ToString.Substring(0, 10)
    End Sub
    Private Sub calcAoR(ByVal AoI As Double, ByVal AoR As Double, ByVal IoR As Double)
        AoRbox.Font = New Font(AoRbox.Font, FontStyle.Bold)
        AoI = AoI * (PI / 180)
        AoR = (IoI * Sin(AoI)) / IoR
        AoR = Asin(AoR)
        AoR = AoR * (180 / PI)

        'AoRbox.Text = AoR.ToString.Substring(0, 10)
        'AoRbox.Text = AoR.ToString.Substring(AoR.ToString.LastIndexOf("." + 1))
    End Sub
#End Region

End Class
0
 
Ander5Author Commented:
alright, I fixed the problem i was having (I feel dumb, the 1st commented line displays the result) thanks for your help.
0
 
wd006451Commented:
Welcome...
0
 
wd006451Commented:
BTW: Click on the add above to get 1000 extra points... (hint hint)
0
 
Ander5Author Commented:
?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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