VB添削
このプログラムは
例えば3 3 4とテキストボックスに数字が打ち込まれると
3×3行列が4個分 のテキストボックスがでてきます。
ここに数字を打ち込んでいき、ボタン2を押すと3×3のテキスト
トボックスが出てくると同時に足し算した結果が出てくるようにしたいです。
以下のプログラムはできたところまで作成しています。
どこを直せばよいのでしょうか。
Public Class Form1
Private number As Integer
Private rows As Integer
Private columns As Integer
Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 1 To 3
AddHandler Me.Controls("TextBox" & i).TextChanged, AddressOf TextBox_TextChanged
Next
End Sub
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If System.Text.RegularExpressions.Regex.IsMatch(CType(sender, TextBox).Text, "[^0-9]") Then
MessageBox.Show("数字で入力してください", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
With CType(sender, TextBox)
.Text = .Text.Substring(0, .Text.Length - 1)
.SelectionStart = .Text.Length
End With
End If
Dim cnt1 As Integer
Dim cnt2 As Integer
Dim cnt3 As Integer
If Integer.TryParse(TextBox1.Text, cnt1) And Integer.TryParse(TextBox2.Text, cnt2) And Integer.TryParse(TextBox3.Text, cnt3) Then
For k = 1 To cnt3
For i = 1 To cnt1
For j = 1 To cnt2
Dim tb As TextBox = New TextBox()
tb.Name = "tb" + i.ToString()
Me.Controls.Add(tb)
tb.Top = (i - 1) * 28 + 55
tb.Left = (j - 1) * 30 + 40 * (cnt2 * (k - 1)) + 10
tb.Width = 25
Next
Next
Next
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Bounds = New Rectangle(10, 10, 1350, 800)
Me.AutoScroll = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sum As Double
Dim cnt As Integer = 0
For i As Integer = 1 To rows
For j As Integer = 1 To columns
Dim tb As TextBox = New TextBox()
cnt += 1 : If cnt > rows * columns Then cnt = 1
tb.Name = "tb" + cnt.ToString
Me.Controls.Add(tb)
tb.Top = (i - 1) * 30 + (80 + 40 * rows)
tb.Left = (j - 1) * 60 + 10
tb.Width = 40
sum = 0
For k As Integer = 1 To number
sum += Double.Parse(CType(Me.Controls("tb" + (cnt + (rows * columns * (k - 1))).ToString()), TextBox).Text)
Next
tb.Text = sum.ToString()
Next
Next
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
End Sub
End Class
お礼
返事が遅くなってしまい申し訳ありません。この度は回答頂きありがとうございました。カウントアップの方法について調べてみます。