昔の自分を執拗以上に貶してみた (VB.NETの糞コード集)

エディター作成

クラス・メソッドの分割 (閲覧注意)

まずは外観。
旧 ↓

新 ↓

まあここはあんまり違いないですね。
今回の趣旨はVB.NETで作成したプログラムをC#に移植する事なので、この辺りはほぼ同じです。

次にメインフォームのコード。
旧 ↓

' Form1.vb
Public Class Form1
    Private Property Square1 As Short = 0
    Private Property OverField As Boolean = False
    Private BasePoint As Point
    Private PaPen As Pen = Pens.DarkGray
    Private ResetRectangle As Rectangle

    ''' メインフォームを読み込み際の初期化処理を行います。    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim mar = New MapArrayEdit1()
        Form1_Initialize()
        MapField_Add_NewTable()
        mar.MapArray_AddToEnd(1)
        mar.Dispose()
    End Sub

    Private Sub Form1_Initialize()
        KeyPreview = True
        Text = "Freeformation Action Map Editor - 無題のワークドキュメント  "
        ファイル名へ上書きエクスポートOToolStripMenuItem.Text = "無題へ上書きしてエクスポート"
        PageNo.AutoSize = False
        PageNo.Height = 22
        PageNo.Text = 1
        FAMESharedClasses1.CurrentPosition = FAMESharedClasses1.Map_Array.GetUpperBound(1)
        FAMESharedClasses1.WorkFileName = ""
        FAMESharedClasses1.ChipVolume.ChipFileName = ""
        FAMESharedClasses1.ChipVolume.X = 0
        FAMESharedClasses1.ChipVolume.Y = 0
        PictureBox1.BackgroundImage = Nothing
        PictureBox1.Tag = -1
        ResetRectangle = New Rectangle(0, 0, 1, 1)
    End Sub

    ''' クライアントクラス内に TableLayoutPanel を15行、16列の固定オブジェクトとして生成します。
    Private Sub MapField_Add_NewTable()
        Const MAP_SIZE As Integer = 32
        Const MAP_COL As Integer = 16
        Const MAP_ROW As Integer = 15

        TableLayoutPanel2.ColumnCount = MAP_COL
        TableLayoutPanel2.ColumnStyles.Clear()
        For i As Integer = 1 To MAP_COL
            TableLayoutPanel2.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, MAP_SIZE))
        Next
        TableLayoutPanel2.RowCount = MAP_ROW
        TableLayoutPanel2.RowStyles.Clear()
        For i As Integer = 1 To MAP_ROW
            TableLayoutPanel2.RowStyles.Add(New RowStyle(SizeType.Absolute, MAP_SIZE))
        Next
        TableLayoutPanel2.Size = New Size((MAP_COL * MAP_SIZE) + MAP_COL + 1, (MAP_ROW * MAP_SIZE) + MAP_ROW + 1)

        For r As Integer = 0 To MAP_ROW - 1
            For c As Integer = 0 To MAP_COL - 1
                Dim iPx = New PictureBox() With {
                    .Name = "PictureBox_X" & c & "Y" & r,
                    .Size = New Size(MAP_SIZE, MAP_SIZE),
                    .Margin = New Padding(0),
                    .BackgroundImage = PictureBox1.BackgroundImage
                }
                AddHandler iPx.MouseDown, AddressOf MouseDown_MapTable
                AddHandler iPx.MouseMove, AddressOf MouseMove_MapTable
                AddHandler iPx.MouseUp, AddressOf MouseUp_MapTable
                TableLayoutPanel2.Controls.Add(iPx, c, r)
            Next
        Next
    End Sub

    ''' フォームを閉じます。    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Dim saving As Boolean = FAMESharedClasses1.UpdateFlag
        Dim commit As Boolean = False

        'How do you handle content being edited?
        If saving = True Then
            Dim a = New FileIO1
            commit = a.Ask_Saving()
            If commit = False Then
                a.Dispose()
                Exit Sub
            Else
                FAMESharedClasses1.UpdateFlag_Judgement(False)
            End If
            a.Dispose()
        End If
    End Sub
End Class

原文ままです。
新 ↓

// MainForm.cs
namespace MapEditor.src.main
{
    ///  Application power window.
    public partial class MainForm : Form
    {
        private readonly FileCommands fileCommands = new();

        ///  Constructor for MainForm.
        public MainForm()
        {
            InitializeComponent();
        }

        private void バイナリデータを開くBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fileCommands.OpenBinaryMapFile(ref mapFieldTable);
        }

        private void バイナリデータを閉じるBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fileCommands.CloseBinaryMapFile(ref mapFieldTable);
        }

        private void グラフィックチップリストを開くGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fileCommands.OpenGraphicChipList(ref graphicChipPanel);
        }

        private void アプリケーションを終了XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FileCommands.ExitApplication();
        }

        private void MainForm_Activated(object sender, EventArgs e)
        {
            this.ActiveControl = null;
        }

        private void MainForm_Click(object sender, EventArgs e)
        {
            this.ActiveControl = null;
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                e.SuppressKeyPress = true;
                this.ActiveControl = null;
                return;
            }
        }
    }
}

こちらも原文まま。

まず新のほうはこのままだと説明もクソもできないんですけど、言いたい事は伝わるかと思います。

まずは全体通して実装とイベントハンドラを分けておくようにしました。

プログラムの可読性ですね。

それで気が付く事があると思うんですが、旧のコードにはイベントハンドラないじゃん・・・ってなりますね。
本当だ・・・ないね・・・f^^;

と思ったらありました。
別ファイルになってました ↓

' Form1_EventHandler.vb
Partial Class Form1
    ' ----- This place exists to gather eventhandlers in one place and manage it.    ''' 二重左マークボタンをクリックした際のイベント処理。マップフィールドの操作を行います。    Private Sub Button_FRw_Click(sender As Object, e As EventArgs) Handles Button_FRw.Click
        Dim mfr As New MapFieldRefresh1()
        mfr.Map_FastRewind()
        mfr.Dispose()
        FAMESharedClasses1.UpdateFlag_Judgement(True)
    End Sub

    ''' 左マークボタンをクリックした際のイベント処理。マップフィールドの操作を行います。    Private Sub Button_Rw_Click(sender As Object, e As EventArgs) Handles Button_Rw.Click
        Dim mfr As New MapFieldRefresh1()
        mfr.Map_Rewind()
        mfr.Dispose()
        FAMESharedClasses1.UpdateFlag_Judgement(True)
    End Sub

    ''' 右マークボタンをクリックした際のイベント処理。マップフィールドの操作を行います。    Private Sub Button_Fw_Click(sender As Object, e As EventArgs) Handles Button_Fw.Click
        Dim mfr As New MapFieldRefresh1()
        mfr.Map_Forward()
        mfr.Dispose()
        FAMESharedClasses1.UpdateFlag_Judgement(True)
    End Sub

    ''' 二重右マークボタンをクリックした際のイベント処理。マップフィールドの操作を行います。    Private Sub Button_FFw_Click(sender As Object, e As EventArgs) Handles Button_FFw.Click
        Dim mfr As New MapFieldRefresh1()
        mfr.Map_FastForward()
        mfr.Dispose()
        FAMESharedClasses1.UpdateFlag_Judgement(True)
    End Sub

    ''' 上画面スクロールボタンをクリックした際のイベント処理を行います。    Private Sub UpScrollButton_Click(sender As Object, e As EventArgs) Handles UpScrollButton.Click
        Dim a As Integer
        If Not Integer.TryParse(PageNo.Text, a) Then
            Exit Sub
        Else
            a = a - 1
        End If
        Dim b = FAMESharedClasses1.Map_Array.GetValue(0, a * 16 + 6)
        If b < 1 Or b > FAMESharedClasses1.Map_Array.GetLength(1) / 16 Then
            Exit Sub
        Else
            FAMESharedClasses1.CurrentPosition = (b - 1) * 16
            Dim iMFR = New MapFieldRefresh1
            iMFR.MapFieldView_Refresh()
            iMFR.Dispose()
        End If
    End Sub

    ''' 下画面スクロールボタンをクリックした際のイベント処理を行います。    Private Sub DownScrollButton_Click(sender As Object, e As EventArgs) Handles DownScrollButton.Click
        Dim a As Integer
        If Not Integer.TryParse(PageNo.Text, a) Then
            Exit Sub
        Else
            a = a - 1
        End If
        Dim b = FAMESharedClasses1.Map_Array.GetValue(0, a * 16 + 7)
        If b < 1 Or b > FAMESharedClasses1.Map_Array.GetLength(1) / 16 Then
            Exit Sub
        Else
            FAMESharedClasses1.CurrentPosition = (b - 1) * 16
            Dim iMFR = New MapFieldRefresh1
            iMFR.MapFieldView_Refresh()
            iMFR.Dispose()
        End If
    End Sub

    ''' 左画面スクロールボタンをクリックした際のイベント処理を行います。    Private Sub LeftScrollButton_Click(sender As Object, e As EventArgs) Handles LeftScrollButton.Click
        Dim a As Integer
        If Not Integer.TryParse(PageNo.Text, a) Then
            Exit Sub
        Else
            a = a - 1
        End If
        Dim b = FAMESharedClasses1.Map_Array.GetValue(0, a * 16 + 8)
        If b < 1 Or b > FAMESharedClasses1.Map_Array.GetLength(1) / 16 Then
            Exit Sub
        Else
            FAMESharedClasses1.CurrentPosition = (b - 1) * 16
            Dim iMFR = New MapFieldRefresh1
            iMFR.MapFieldView_Refresh()
            iMFR.Dispose()
        End If
    End Sub

    ''' 右画面スクロールボタンをクリックした際のイベント処理を行います。    Private Sub RightScrollButton_Click(sender As Object, e As EventArgs) Handles RightScrollButton.Click
        Dim a As Integer
        If Not Integer.TryParse(PageNo.Text, a) Then
            Exit Sub
        Else
            a = a - 1
        End If
        Dim b = FAMESharedClasses1.Map_Array.GetValue(0, a * 16 + 9)
        If b < 1 Or b > FAMESharedClasses1.Map_Array.GetLength(1) / 16 Then
            Exit Sub
        Else
            FAMESharedClasses1.CurrentPosition = (b - 1) * 16
            Dim iMFR = New MapFieldRefresh1
            iMFR.MapFieldView_Refresh()
            iMFR.Dispose()
        End If
    End Sub

    ''' ページ数入力テキストボックスへカーソルが入った場合の処理を行います。    Private Sub PageNo_Enter(sender As Object, e As EventArgs) Handles PageNo.Enter
        PageNo.Tag = PageNo.Text
    End Sub

    ''' ページ数入力テキストボックスでのキー入力制御処理を行います。    Private Sub PageNo_KeyPress(sender As Object, e As KeyPressEventArgs) Handles PageNo.KeyPress
        Dim iMFR = New MapFieldRefresh1
        Dim ret As Boolean
        If e.KeyChar = Chr(13) Then
            ActiveControl = Nothing
            ret = iMFR.Map_PageJump()
            iMFR.Dispose()

            If ret Then
                Dim iMFR2 = New MapFieldRefresh1
                iMFR2.MapFieldView_Refresh()
                iMFR2.Dispose()
            Else
                Exit Sub
            End If
        ElseIf e.KeyChar = Chr(27) Then
            ActiveControl = Nothing
            iMFR.Dispose()
            PageNo.Text = PageNo.Tag
        End If
    End Sub

    ''' ChipEnterButton にマウスが被さった際のイベント処理。    Private Sub ChipEnterButton_MouseEnter(sender As Object, e As EventArgs) Handles ChipEnterButton.MouseEnter
        Cursor = Cursors.Hand
    End Sub

    ''' ChipEnterButton からマウスカーソルが離脱した際のイベント処理。    Private Sub ChipEnterButton_MouseLeave(sender As Object, e As EventArgs) Handles ChipEnterButton.MouseLeave
        Cursor = Cursors.Default
    End Sub

    ''' メインフォームのコントロール以外をクリックした時の処理を行います。    Private Sub Form1_Click(sender As Object, e As EventArgs) Handles MyBase.Click
        ActiveControl = Nothing
    End Sub

    ''' Write processing from here When clicking On the created map field.
    ''' PHASE 1. Mousedown in Mapfield.
    Private Sub MouseDown_MapTable(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel2.MouseDown
        Dim iMousePoint As Point = System.Windows.Forms.Cursor.Position
        BasePoint = TableLayoutPanel2.PointToClient(iMousePoint)
        Dim temp1 As Integer = Math.Floor(BasePoint.X / 33)
        Dim temp2 As Integer = Math.Floor(BasePoint.Y / 33)
        temp1 = temp1 * 33
        temp2 = temp2 * 33
        BasePoint = New Point(temp1, temp2)
        Select Case e.Button
            Case MouseButtons.Left
                Square1 = 1
            Case MouseButtons.Right
                Square1 = 3
        End Select
    End Sub

    ''' Write processing from here When clicking On the created map field.
    ''' PHASE 2. Move the MouseOver Mapfield.    Private Sub MouseMove_MapTable(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel2.MouseMove
        Dim cOverX, cOverY As Boolean
        Dim rBeginPoint As Point, rEndPoint As Point
        Dim rMousePoint As Point = System.Windows.Forms.Cursor.Position
        Dim graph As Graphics
        rMousePoint = TableLayoutPanel2.PointToClient(rMousePoint)
        'Checking the overfield.
        If rMousePoint.X < 0 Then
            rMousePoint.X = 0 : cOverX = True
        ElseIf rMousePoint.X > 529 Then
            rMousePoint.X = 528 : cOverX = True
        Else
            cOverX = False
        End If
        If rMousePoint.Y < 0 Then
            rMousePoint.Y = 0 : cOverY = True
        ElseIf rMousePoint.Y > 496 Then
            rMousePoint.Y = 495 : cOverY = True
        Else
            cOverY = False
        End If
        If cOverX = False And cOverY = False Then
            PaPen = Pens.Red
            OverField = False
        Else
            PaPen = Pens.DarkGray
            OverField = True
        End If
        If Square1 >= 1 And Square1 <= 4 Then
            graph = TableLayoutPanel2.CreateGraphics()
            graph.DrawRectangle(Pens.DarkGray, ResetRectangle)
            Dim temp1 As Integer, temp2 As Integer
            If rMousePoint.X > BasePoint.X And rMousePoint.Y > BasePoint.Y Then
                rBeginPoint = New Point(BasePoint.X, BasePoint.Y)
                temp1 = Math.Ceiling(rMousePoint.X / 33)
                temp2 = Math.Ceiling(rMousePoint.Y / 33)
                temp1 = temp1 * 33
                temp2 = temp2 * 33
                rEndPoint = New Point(temp1, temp2)
                graph.DrawRectangle(PaPen, rBeginPoint.X, rBeginPoint.Y, rEndPoint.X - rBeginPoint.X, rEndPoint.Y - rBeginPoint.Y)
                ResetRectangle = New Rectangle(rBeginPoint.X, rBeginPoint.Y, rEndPoint.X - rBeginPoint.X, rEndPoint.Y - rBeginPoint.Y)
            ElseIf rMousePoint.X > BasePoint.X And rMousePoint.Y < BasePoint.Y Then
                rBeginPoint = New Point(BasePoint.X, BasePoint.Y + 33)
                temp1 = Math.Ceiling(rMousePoint.X / 33)
                temp2 = Math.Floor(rMousePoint.Y / 33)
                temp1 = temp1 * 33
                temp2 = temp2 * 33
                rEndPoint = New Point(temp1, temp2)
                graph.DrawRectangle(PaPen, rBeginPoint.X, rEndPoint.Y, rEndPoint.X - rBeginPoint.X, rBeginPoint.Y - rEndPoint.Y)
                ResetRectangle = New Rectangle(rBeginPoint.X, rEndPoint.Y, rEndPoint.X - rBeginPoint.X, rBeginPoint.Y - rEndPoint.Y)
            ElseIf rMousePoint.X < BasePoint.X And rMousePoint.Y > BasePoint.Y Then
                rBeginPoint = New Point(BasePoint.X + 33, BasePoint.Y)
                temp1 = Math.Floor(rMousePoint.X / 33)
                temp2 = Math.Ceiling(rMousePoint.Y / 33)
                temp1 = temp1 * 33
                temp2 = temp2 * 33
                rEndPoint = New Point(temp1, temp2)
                graph.DrawRectangle(PaPen, rEndPoint.X, rBeginPoint.Y, rBeginPoint.X - rEndPoint.X, rEndPoint.Y - rBeginPoint.Y)
                ResetRectangle = New Rectangle(rEndPoint.X, rBeginPoint.Y, rBeginPoint.X - rEndPoint.X, rEndPoint.Y - rBeginPoint.Y)
            ElseIf rMousePoint.X < BasePoint.X And rMousePoint.Y < BasePoint.Y Then
                rBeginPoint = New Point(BasePoint.X + 33, BasePoint.Y + 33)
                temp1 = Math.Floor(rMousePoint.X / 33)
                temp2 = Math.Floor(rMousePoint.Y / 33)
                temp1 = temp1 * 33
                temp2 = temp2 * 33
                rEndPoint = New Point(temp1, temp2)
                graph.DrawRectangle(PaPen, rEndPoint.X, rEndPoint.Y, rBeginPoint.X - rEndPoint.X, rBeginPoint.Y - rEndPoint.Y)
                ResetRectangle = New Rectangle(rEndPoint.X, rEndPoint.Y, rBeginPoint.X - rEndPoint.X, rBeginPoint.Y - rEndPoint.Y)
            Else ' MousePoint.X = BasePoint.X Or MousePoint.Y = BasePoint.Y
                ' Processing undefined.
            End If
            If Square1 = 1 OrElse Square1 = 2 Then
                Square1 = 2
            ElseIf Square1 = 3 OrElse Square1 = 4 Then
                Square1 = 4
            End If
        Else ' Square1 = 0
            Square1 = 0
        End If
    End Sub

    ''' Write processing from here When clicking On the created map field.
    ''' PHASE 3. Determining the selection range and placing the map chip.    Private Sub MouseUp_MapTable(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel2.MouseUp
        If OverField = False Then
            If Square1 = 2 OrElse Square1 = 4 Then
                Dim temp1 As Integer = ResetRectangle.X \ 33
                Dim temp2 As Integer = ResetRectangle.Y \ 33
                Dim temp3 As Integer = (ResetRectangle.Right - 1) \ 33
                Dim temp4 As Integer = (ResetRectangle.Bottom - 1) \ 33
                Dim pic As PictureBox
                Cursor.Current = Cursors.WaitCursor
                For ii As Integer = temp2 To temp4
                    For jj As Integer = temp1 To temp3
                        pic = TableLayoutPanel2.GetControlFromPosition(jj, ii)
                        If Square1 = 2 Then
                            pic.Tag = PictureBox1.Tag
                            pic.BackgroundImage = PictureBox1.BackgroundImage
                            FAMESharedClasses1.Map_Array(ii + 1, jj + FAMESharedClasses1.CurrentPosition) = pic.Tag
                        Else 'Square1 = 4
                            pic.Tag = -1
                            pic.BackgroundImage = Nothing
                            FAMESharedClasses1.Map_Array(ii + 1, jj + FAMESharedClasses1.CurrentPosition) = pic.Tag
                        End If
                    Next
                Next
                Cursor.Current = Cursors.Default
                Sub_ComponentsReset()
            ElseIf Square1 = 1 OrElse Square1 = 3 Then
                Dim temp1 As Integer = BasePoint.X / 33 : Dim temp2 As Integer = BasePoint.Y / 33
                Dim hand As PictureBox = TableLayoutPanel2.GetControlFromPosition(temp1, temp2)
                Dim iCellPoint As TableLayoutPanelCellPosition = TableLayoutPanel2.GetCellPosition(hand)
                Dim iCellColumn As Integer = iCellPoint.Column : Dim iCellRow As Integer = iCellPoint.Row
                If Square1 = 1 Then
                    hand.Tag = PictureBox1.Tag
                    hand.BackgroundImage = PictureBox1.BackgroundImage
                    FAMESharedClasses1.Map_Array(iCellRow + 1, iCellColumn + FAMESharedClasses1.CurrentPosition) = hand.Tag
                Else 'Square1 = 3
                    hand.Tag = -1
                    hand.BackgroundImage = Nothing
                    FAMESharedClasses1.Map_Array(iCellRow + 1, iCellColumn + FAMESharedClasses1.CurrentPosition) = hand.Tag
                End If
                Sub_ComponentsReset()
            Else 'Square = 0
                'Processing undefined.
            End If
            FAMESharedClasses1.UpdateFlag_Judgement(True)
        Else
            Sub_ComponentsReset()
            OverField = False
        End If
    End Sub

    ''' Deletes an existing rectangular graphic.    Private Sub Sub_ComponentsReset()
        Dim graph As Graphics
        graph = TableLayoutPanel2.CreateGraphics()
        graph.DrawRectangle(Pens.DarkGray, ResetRectangle)
        Square1 = 0
    End Sub


    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '
    ' - - - - - - - The event procedure when item of the Menustrip selected is described from here. - - - - - - - '

    ''' DAT形式の作業ファイルの読み込みを試みます。    Private Sub 作業データを開くToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 作業データを開くToolStripMenuItem.Click
        Open_WorkFile()
    End Sub

    ''' 指定されたBin形式ファイルの読み込みを試みます。    Private Sub バイナリファイルを開くToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles バイナリファイルを開くToolStripMenuItem.Click
        Dim ofd As OpenFileDialog = BinFileOpenDialog
        Dim iFileIO As FileIO1 = New FileIO1
        Dim commit As Boolean
        Dim ret As DialogResult
        If Not FAMESharedClasses1.ChiData_Exists Then
            MessageBox.Show("チップデータが読み込まれていないためマップデータを展開出来ません。" & vbCrLf & "処理を続行する場合はグラフィックチップを読み込んで下さい。", "処理を中止しました")
            Exit Sub
        End If

        With ofd
            .FileName = ""
            .Filter = "バイナリデータファイル(*.bin)|*.bin"
            .Title = "マップデータファイルを開く"
            .InitialDirectory = iFileIO.GetCurrentDirectoryPath(FAMESharedClasses1.GetBinaryFilePath)
            .RestoreDirectory = True
        End With

        ret = ofd.ShowDialog()
        If ret = DialogResult.OK Then
            commit = iFileIO.LoadBinaryFile(ofd.FileName)
            iFileIO.Dispose()
        End If
        If commit Then
            FAMESharedClasses1.SetBinaryFile(ofd.FileName)

            PageNo.Text = "1"
            FAMESharedClasses1.CurrentPosition = 0
            Dim iMaps = New MapFieldRefresh1
            iMaps.MapFieldView_Refresh()
            iMaps.Dispose()

            Dim temp_num As Integer = FAMESharedClasses1.Map_Array.GetLength(1) / 16
            Dim iMAE = New MapArrayEdit1
            iMAE.Update_LastPageNum_Label(0, temp_num)
            iMAE.Dispose()

            FAMESharedClasses1.UpdateFlag_Judgement(True)
            MessageBox.Show("バイナリデータの読み取りが正常に終了しました。")
            ToolStripStatusLabel1.Text = "バイナリファイルが正常に読み込まれました。 - " & FAMESharedClasses1.GetBinaryFilePath
        End If
    End Sub

    ''' ファイル名を指定して作業ファイルを保存します。    Private Sub 名前を付けて保存AToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 名前を付けて保存AToolStripMenuItem.Click
        Dim filenametext As String = FAMESharedClasses1.WorkFileName
        Dim io_except = New FileIO1
        Dim acc As Boolean

        acc = io_except.File_Saved()
        io_except.Dispose()

        If acc = True Then
            FAMESharedClasses1.UpdateFlag_Judgement(False)
            ToolStripStatusLabel1.Text = "次のファイルへの保存が完了しました。 - " & FAMESharedClasses1.WorkFileName
        Else
            Exit Sub
        End If
    End Sub

    ''' 現在開いている作業ファイルにデータの保存を行います。    Private Sub 上書き保存SToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 上書き保存SToolStripMenuItem.Click
        Dim filenametext As String = FAMESharedClasses1.WorkFileName
        Dim io_except = New FileIO1
        Dim acc As Boolean

        If filenametext = "" Then
            acc = io_except.File_Saved()
        Else
            acc = io_except.File_Updated()
        End If
        io_except.Dispose()

        If acc = True Then
            FAMESharedClasses1.UpdateFlag_Judgement(False)
            ToolStripStatusLabel1.Text = "次のファイルへの保存が完了しました。 - " & FAMESharedClasses1.WorkFileName
        Else
            Exit Sub
        End If
    End Sub

    ''' 作業ドキュメントの新規作成を行います。    Private Sub 無題のワークドキュメントToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 無題のワークドキュメントToolStripMenuItem.Click
        Dim saving As Boolean = FAMESharedClasses1.UpdateFlag
        Dim commit As Boolean = False

        'How do you handle content being edited?
        If saving = True Then
            Dim a = New FileIO1
            commit = a.Ask_Saving()
            If commit = False Then
                Exit Sub
            Else
                ToolStripStatusLabel1.Text = "次のファイルへの保存が完了しました。 - " & FAMESharedClasses1.WorkFileName
            End If
        End If

        'Reset the MapArray and MapField.
        ReDim FAMESharedClasses1.Map_Array(15, 0)
        Dim mar = New MapArrayEdit1()
        FAMESharedClasses1.CurrentPosition = FAMESharedClasses1.Map_Array.GetUpperBound(1)
        mar.Update_LastPageNum_Label(0, 0)
        mar.MapArray_AddToEnd(1)
        mar.Dispose()

        'Adjust the display position of the map.
        Dim mfr = New MapFieldRefresh1()
        mfr.MapFieldView_Refresh()
        mfr.Dispose()
        PageNo.Text = 1


        Dim graphC = New GraphicChip1
        graphC.Clear_ChipList()

        FAMESharedClasses1.WorkFileName = ""
        FAMESharedClasses1.UpdateFlag_Judgement(False)
        Text = "Freeformation Action Map Editor - 新規ワークドキュメント  "
        ファイル名へ上書きエクスポートOToolStripMenuItem.Text = "無題へ上書きしてエクスポート"
    End Sub

    ''' マップフィールドの内容をリセットして新規バイナリファイルを配置します。    Private Sub 空のマップデータToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 空のマップデータToolStripMenuItem.Click
        Dim saving As Boolean = FAMESharedClasses1.UpdateFlag
        Dim commit As Boolean = False

        'How do you handle content being edited?
        If saving = True Then
            Dim a = New FileIO1
            commit = a.Ask_Saving()
            If commit = False Then
                Exit Sub
            Else
                ToolStripStatusLabel1.Text = "次のファイルへの保存が完了しました。 - " & FAMESharedClasses1.WorkFileName
            End If
        End If

        'Reset the MapArray and MapField.
        ReDim FAMESharedClasses1.Map_Array(15, 0)
        Dim mar = New MapArrayEdit1()
        FAMESharedClasses1.CurrentPosition = FAMESharedClasses1.Map_Array.GetUpperBound(1)
        mar.Update_LastPageNum_Label(0, 0)
        mar.MapArray_AddToEnd(1)
        mar.Dispose()

        'Adjust the display position of the map.
        Dim mfr = New MapFieldRefresh1()
        mfr.MapFieldView_Refresh()
        mfr.Dispose()
        PageNo.Text = "1"

        FAMESharedClasses1.SetBinaryFile("")
        FAMESharedClasses1.UpdateFlag_Judgement(False)
    End Sub

    ''' 現在開いている作業ファイルを再度開き直します。    Private Sub 作業データを開きなおすRToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 作業データを開きなおすRToolStripMenuItem.Click
        If FAMESharedClasses1.WorkFileName = "" Then
            Exit Sub
        Else
            Open_WorkFile(True)
        End If
    End Sub

    ''' バイナリデータの対象ファイルへデータを上書き保存します。    Private Sub ファイル名へ上書きエクスポートOToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ファイル名へ上書きエクスポートOToolStripMenuItem.Click
        Dim iFileIO As FileIO1 = New FileIO1
        Dim commit As Boolean
        Dim ret As DialogResult
        Dim filename As String
        If FAMESharedClasses1.GetBinaryFilePath = "" Then
            Dim sfd As SaveFileDialog = BinFileSaveDialog
            With sfd
                .FileName = FAMESharedClasses1.GetBinaryFilePath
                .Filter = "バイナリデータファイル(*.bin)|*.bin"
                .Title = "ファイル名を指定して保存"
                .InitialDirectory = iFileIO.GetCurrentDirectoryPath(FAMESharedClasses1.GetBinaryFilePath)
                .RestoreDirectory = True
            End With
            ret = sfd.ShowDialog()
            If ret = DialogResult.Cancel Then
                Exit Sub
            Else
                filename = sfd.FileName
            End If
        Else
            filename = FAMESharedClasses1.GetBinaryFilePath
        End If

        commit = iFileIO.CreateBinaryDatas(filename)
        iFileIO.Dispose()
        If commit Then
            FAMESharedClasses1.SetBinaryFile(filename)
        Else
            FAMESharedClasses1.SetBinaryFile("")
        End If

    End Sub

    ''' 現在のマップデータをバイト列としてファイルへ保存します。    Private Sub 名前を指定してエクスポートNToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 名前を指定してエクスポートNToolStripMenuItem.Click
        Dim sfd As SaveFileDialog = BinFileSaveDialog
        Dim iFileIO As FileIO1 = New FileIO1
        Dim commit As Boolean
        Dim ret As DialogResult
        With sfd
            .FileName = FAMESharedClasses1.GetBinaryFilePath
            .Filter = "バイナリデータファイル(*.bin)|*.bin"
            .Title = "ファイル名を指定して保存"
            .InitialDirectory = iFileIO.GetCurrentDirectoryPath(FAMESharedClasses1.GetBinaryFilePath)
            .RestoreDirectory = True
        End With

        ret = sfd.ShowDialog()
        If ret = DialogResult.OK Then
            commit = iFileIO.CreateBinaryDatas(sfd.FileName)
            iFileIO.Dispose()
        Else
            Exit Sub
        End If
        If commit Then
            FAMESharedClasses1.SetBinaryFile(sfd.FileName)
        Else
            FAMESharedClasses1.SetBinaryFile("")
        End If
    End Sub

    ''' マップフィールドへマップページを追加します。    Private Sub 新規ページの追加AToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 新規ページの追加AToolStripMenuItem.Click
        Dim DialogBinder As New AddMapPageDialog(FAMESharedClasses1.Map_Array)
        DialogBinder.ShowDialog()

        Select Case DialogBinder.DialogResult
            Case DialogResult.OK
                Dim add_in, locate As Integer
                Dim mar = New MapArrayEdit1
                add_in = CInt(DialogBinder.AddPageNumTextBox.Text)
                If DialogBinder.RadioButton1.Checked = True Then
                    locate = Nothing
                    FAMESharedClasses1.CurrentPosition = FAMESharedClasses1.Map_Array.GetLength(1)
                ElseIf DialogBinder.RadioButton2.Checked = True Then
                    locate = 1
                    FAMESharedClasses1.CurrentPosition = 0
                Else
                    locate = CInt(DialogBinder.EditTextBox.Text)
                    FAMESharedClasses1.CurrentPosition = (locate - 1) * 16
                End If

                Cursor.Current = Cursors.WaitCursor

                mar.MapArray_AddToEnd(add_in)
                If DialogBinder.RadioButton1.Checked <> True Then
                    mar.MapArray_BetweenIn(locate, add_in)
                End If
                mar.Dispose()

                Dim mfr = New MapFieldRefresh1
                mfr.MapFieldView_Refresh()
                mfr.Dispose()

                Cursor.Current = Cursors.Default

                FAMESharedClasses1.UpdateFlag_Judgement(True)

                MessageBox.Show("マップが追加されました。")
                ToolStripStatusLabel1.Text = "マップページの追加が正常に行われました。"

            Case DialogResult.Cancel
                'Processing undefined.
        End Select
    End Sub

    ''' マップデータのページの移動・コピーを行います。    Private Sub ページのコピー移動CToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ページのコピー移動CToolStripMenuItem.Click
        Dim iMovDialog = New MovCopyMapPageDialog
        Dim ret As DialogResult

        ret = iMovDialog.ShowDialog()
        If ret = DialogResult.Cancel Then
            iMovDialog.Dispose()
            Exit Sub
        Else
            FAMESharedClasses1.UpdateFlag_Judgement(True)
            iMovDialog.Dispose()
            ToolStripStatusLabel1.Text = "マップページの移動が正常に行われました。"
        End If
    End Sub

    ''' マップデータのページ削除を行います。    Private Sub ページの削除DToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ページの削除DToolStripMenuItem.Click
        Dim iMAE As MapArrayEdit1 = New MapArrayEdit1
        Dim page As Integer = (FAMESharedClasses1.CurrentPosition \ 16) + 1
        Dim ret As DialogResult
        If FAMESharedClasses1.Map_Array.Length <= (16 * 16) Then
            MessageBox.Show("単数ページのマップは取り消し出来ません!", "フールプルーフ機能が作動しました")
            Exit Sub
        End If

        ret = MessageBox.Show("現在表示中のページ(" & page & "ページ目)の削除します。削除は取り消せません。本当に削除しますか?", "マップページの削除", MessageBoxButtons.YesNo)
        If ret = DialogResult.No Then
            Exit Sub
        Else
            iMAE.MapArray_Remove(page)
            iMAE.Dispose()
            Dim iMFR = New MapFieldRefresh1
            iMFR.MapFieldView_Refresh()
            iMFR.Dispose()
            FAMESharedClasses1.UpdateFlag_Judgement(True)
        End If
        MessageBox.Show("マップが削除されました。")
        ToolStripStatusLabel1.Text = "マップページの削除が正常に行われました。"
    End Sub

    ''' マップヘッダーの編集画面を開きます。    Private Sub マップヘッダーの編集WToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles マップヘッダーの編集WToolStripMenuItem.Click
        Dim iHEDlog = New HeaderEditDialog1(FAMESharedClasses1.Map_Array, FAMESharedClasses1.CurrentPosition)
        iHEDlog.ShowDialog()
        iHEDlog.Dispose()
        ToolStripStatusLabel1.Text = "マップヘッダーの値が更新されました。"
    End Sub

    ''' ページ数を指定するテキストボックスにフォーカスを合わせます。    Private Sub ページ数を指定してジャンプRToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ページ数を指定してジャンプRToolStripMenuItem.Click
        ActiveControl = PageNo
    End Sub

    ''' マップページの先頭ページを表示します。    Private Sub 最初のページTToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 最初のページTToolStripMenuItem.Click
        PageNo.Text = "1"
        PageNo.Tag = PageNo.Text
        Dim iMFR = New MapFieldRefresh1
        Dim ret = iMFR.Map_PageJump()
        iMFR.Dispose()

        If ret Then
            Dim iMFR2 = New MapFieldRefresh1
            iMFR2.MapFieldView_Refresh()
            iMFR2.Dispose()
        Else
            Exit Sub
        End If
    End Sub

    ''' マップページの末尾ページを表示します。    Private Sub 最後のページEToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 最後のページEToolStripMenuItem.Click
        Dim maxpage As Integer = FAMESharedClasses1.Map_Array.GetLength(1) / 16
        PageNo.Text = CStr(maxpage)
        PageNo.Tag = PageNo.Text
        Dim iMFR = New MapFieldRefresh1
        Dim ret = iMFR.Map_PageJump()
        iMFR.Dispose()

        If ret Then
            Dim iMFR2 = New MapFieldRefresh1
            iMFR2.MapFieldView_Refresh()
            iMFR2.Dispose()
        Else
            Exit Sub
        End If
    End Sub

    ''' Processing when button1 is clicked.    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ChipEnterButton.Click, マップチップを読み込むToolStripMenuItem.Click
        Dim ret As DialogResult
        Dim iDialog1 As ChipLoadDialog
        Dim iGraphic As GraphicChip1 = New GraphicChip1
        Dim iFileIOs As FileIO1 = New FileIO1

        If ChipEnterButton.Text = "グラフィック取込..." Then
            ret = iFileIOs.Set_ListOption()
            If ret = Windows.Forms.DialogResult.OK Then
                Dim img As Image = Image.FromFile(FAMESharedClasses1.ChipVolume.ChipFileName)
                iDialog1 = New ChipLoadDialog(img)
                iGraphic.Load_ChipList(FAMESharedClasses1.ChipVolume.ChipFileName, FAMESharedClasses1.ChipVolume.X, FAMESharedClasses1.ChipVolume.Y, iDialog1.Get_DialogParameter(2))
            Else  ' DialogResult.Cancel
                Exit Sub
            End If
        Else  ' Button1.Text = "グラフィック削除"
            iGraphic.Clear_ChipList()
        End If
    End Sub

    ''' 現在開いているデータを保存してアプリケーションを終了します。    Private Sub 保存して閉じるEToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 保存して閉じるEToolStripMenuItem.Click
        Dim save_key As Boolean = FAMESharedClasses1.UpdateFlag
        Dim filenametext As String = FAMESharedClasses1.WorkFileName
        Dim io_except = New FileIO1
        Dim commit As Boolean

        If save_key = True Then
            If filenametext = "" Then
                commit = io_except.File_Saved()
            Else
                commit = io_except.File_Updated()
            End If
            io_except.Dispose()
        End If

        If commit = True Then
            FAMESharedClasses1.UpdateFlag_Judgement(False)
            Close()  ' This application is end.
        Else
            Exit Sub
        End If
    End Sub

    ''' アプリケーションを終了します。    Private Sub 閉じるXToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 閉じるXToolStripMenuItem.Click
        Close()  ' This application is end.
    End Sub


    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '
    ' - - - - Exclusion processing : There are events occurring from the operation of the user from here. - - - - '

    ''' 作業ファイルを開く処理を開始します。    ''' ファイル選択をするかどうかのブランチング。
    Private Sub Open_WorkFile(Optional ByVal branch As Boolean = False)
        Dim commit As Boolean = True
        Dim fileIO As FileIO1 = New FileIO1
        Dim saving As Boolean = FAMESharedClasses1.UpdateFlag

        'How do you handle content being edited?
        If saving = True Then
            commit = fileIO.Ask_Saving()
            If commit = False Then
                Exit Sub
            Else
                FAMESharedClasses1.UpdateFlag_Judgement(False)
            End If
        End If

        If branch Then
            commit = fileIO.Try_DATFileOpen(FAMESharedClasses1.WorkFileName, branch)
            fileIO.Dispose()
        Else
            commit = fileIO.Try_DATFileOpen(FAMESharedClasses1.WorkFileName)
            If commit = False Then
                fileIO.Dispose()
                Exit Sub
            End If
        End If

        Dim graphC = New GraphicChip1
        graphC.Clear_ChipList()
        graphC.Load_ChipList(FAMESharedClasses1.ChipVolume.ChipFileName, FAMESharedClasses1.ChipVolume.X, FAMESharedClasses1.ChipVolume.Y)

        Dim iMaps = New MapFieldRefresh1
        iMaps.MapFieldView_Refresh()
        iMaps.Dispose()

        Dim temp_num As Integer = FAMESharedClasses1.Map_Array.GetLength(1) / 16
        Dim iMAE = New MapArrayEdit1
        iMAE.Update_LastPageNum_Label(0, temp_num)
        iMAE.Dispose()

        FAMESharedClasses1.UpdateFlag_Judgement(False)
        MessageBox.Show("作業ファイルの読み込みが完了しました。")
        ToolStripStatusLabel1.Text = "作業ファイルが正常に読み込まれました。 - " & FAMESharedClasses1.WorkFileName
        Dim form_str = FAMESharedClasses1.FilePath_To_FileName(FAMESharedClasses1.WorkFileName)
        Text = "Freeformation Action Map Editor - " & form_str
    End Sub

End Class

長いわ!!!💢💢😤

あとなんかね、最初のほうに書いてあるButton_FRw_Clickイベントハンドラメソッド、これ同じ処理が何回も書かれてて難解・・・(笑)

これは流石にひどいですね。
初心者でもこんな事しないと思うんだけど・・・

そして何よりもブラウザのスクロールバーのつまみの短さを見て笑ってしまいます。
ソースファイル一つに950行のコードが書かれています。

もう面倒だからその点については指摘しないですけど、行数が云々ではなくて実処理がつらつら書いてあるソースコードが950行も続くという事にあると思います
これじゃメンテナンスできなくてバグ直すのそりゃ諦めるやろ・・・(´・ω・`)

後はですね、これは作っている時に気づいてるはずなんですが、FAMESharedClasses1とかいう変数の塊クラスを使っているようです。
変数が多すぎるのもいけませんね。

いずれにしても変数とか処理自体をメソッドに書き出さないからこんな事になっている。

実際にはVB.NETの全機能を新コード側に全部入れてるわけではないですが、今回はVB.NETの糞すぎるコードを批評するコーナーですのでコメント入れさせていただきます😇

こりゃもう改ページしないと埒が明かないわ。

コメント

タイトルとURLをコピーしました