Public Class Form1
    Const READ_ME As String = "Readme.txt"  '説明書のファイル
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.KeyPreview = True

    End Sub

    Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
            e.Handled = True
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try

        If TextBox1.Text = "" Then
            MsgBox("呼び径および直径を入力してください。")
        End If

        If TextBox1.Text = "" Then
            MsgBox("ｘの値を入力してください。")
        End If
        Dim kei As Decimal 'TextBox1にだいにゅうされる直径
        Dim x As Decimal   'TextBox2にだいにゅうされるxの値
        Dim R As Decimal   '求めるRの値
        Dim re As Decimal   '四捨五入前の値
        Dim z As Decimal
            
        kei = TextBox1.Text
            x = TextBox2.Text
            If kei = 0 Then
                MsgBox("呼び径に「0」は代入できません。")
                Return
            ElseIf x = 0 Then
                MsgBox("Ｘに「0」は代入できません。")
                Return
            End If

            If 2 * x > kei Then
                MsgBox("呼び径はＸの２倍より大きくなければなりません。")
                Return
            End If
        re = x / 2 + kei * kei / x / 8
        R = Math.Round(re, 3, MidpointRounding.AwayFromZero)
        
        Label4.Font = New Font(Label4.Font.Name, 18, Label4.Font.Style Or FontStyle.Bold)

        Label4.Text = R
        Label5.Visible = True
        Label5.Text = "先端のＲは"
        Catch ex As InvalidCastException
            MsgBox("数値を入力してください。", MsgBoxStyle.Exclamation)
        Catch ex As Exception
            MsgBox("その他のエラーが発生しました。", MsgBoxStyle.Critical)

        End Try
    End Sub

    Private Sub バージョンToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles バージョンToolStripMenuItem.Click
        MsgBox(Application.ProductName & vbNewLine & "バージョン   " & Application.ProductVersion)
    End Sub

    Private Sub 制作著作ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 制作著作ToolStripMenuItem.Click
        MsgBox("ライセンス　田中裕之")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Clear()
        TextBox2.Clear()
        Label5.Text = ""
        Label4.Font = New Font(Label4.Font.Name, 12)
        Label4.Text = "先端Ｒ表示"
        TextBox1.Focus()
    End Sub

    Private Sub 説明書ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 説明書ToolStripMenuItem.Click
        Dim reader As New IO.StreamReader(Application.StartupPath & "\" & READ_ME, System.Text.Encoding.GetEncoding("shift-jis"))
        Dim f As New Form2
        f.Owner = Me
        f.TextBox1.WordWrap = False
        f.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
        f.TextBox1.Text = reader.ReadToEnd
        reader.Close()
        f.Show()
        f.TextBox1.DeselectAll()
    End Sub
End Class
