演習問題解答
練習1.1コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Label1.Text = Val(Label1.Text) + 2
EndSub
練習1.2
コントロールの追加 プロパティの変更
プログラムリスト
PrivateSub Button2_Click(…省略…) Handles Button2.Click Label1.Text = Val(Label1.Text) - 2
EndSub
練習2.1
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click
EndSub 練習2.2
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click
TextBox4.Text = (Val(TextBox1.Text) + Val(TextBox2.Text)) * Val(TextBox3.Text) / 2 EndSub
練習2.3
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim r AsDouble
r = Val(TextBox1.Text)
TextBox2.Text = 4 * Math.PI * r ^ 2 TextBox3.Text = 4 * Math.PI * r ^ 3 / 3 EndSub
練習3.1 コントロールの配置 プロパティの変更 プログラムリスト Dim a, b AsInteger a = Val(TextBox1.Text) b = a Mod 2 If b = 0 Then TextBox2.Text = "偶数" Else TextBox2.Text = "奇数" EndIf EndSub 練習3.2 コントロールの配置 プロパティの変更 プログラムリスト
PrivateSub TextBox1_TextChanged(…省略…) Handles TextBox1.TextChanged, TextBox2.TextChanged Dim a, b, c AsSingle a = Val(TextBox1.Text) b = Val(TextBox2.Text) c = (a - 100) * 0.9 If b > c * 1.2 Then TextBox3.Text = "ふとりすぎです" ElseIf b < c * 0.8 Then TextBox3.Text = "やせすぎです" Else TextBox3.Text = "ちょうどいい体重です" EndIf EndSub
練習3.3
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub RadioButton1_CheckedChanged(…省略…) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged
If (RadioButton1.Checked = True) Then TextBox1.Text = 5000
ElseIf (RadioButton2.Checked = True) Then TextBox1.Text = 8000
ElseIf (RadioButton3.Checked = True) Then TextBox1.Text = 10000 EndIf EndSub 練習4.1 コントロールの配置 プロパティの変更 プログラムリスト
PrivateSub TextBox1_TextChanged(…省略…) Handles TextBox1.TextChanged Dim i, n, s AsInteger n = Val(TextBox1.Text) s = 1 For i = 1 To n s = s * i Next TextBox2.Text = s EndSub
練習4.2
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub TextBox1_TextChanged(…省略…) Handles TextBox1.TextChanged Dim i, s, limit AsInteger
limit = Val(TextBox1.Text) Do
i = i + 1
s = i * (i + 1) * (i + 2) LoopWhile s <= limit TextBox2.Text = i TextBox3.Text = i + 1 TextBox4.Text = i + 2 TextBox5.Text = s EndSub 練習4.3 コントロールの配置 プロパティの変更 プログラムリスト
PrivateSub TextBox1_TextChanged(…省略…) Handles TextBox1.TextChanged, TextBox2.TextChanged Dim i, j, s, n, m AsInteger n = Val(TextBox1.Text) m = Val(TextBox2.Text) s = 0 For i = n To m For j = 1 To 9 s = s + i * j Next j Next i TextBox3.Text = s EndSub
練習5.1
コントロールの追加 プロパティの変更
プログラムリスト
PrivateSub Button2_Click(…省略…) Handles Button2.Click Dim i AsInteger
Dim j AsInteger Dim n AsInteger Dim work AsInteger n = Val(TextBox3.Text) Dim a(n - 1) AsInteger For i = 0 To n - 1
a(i) = Val(TextBox1.Lines(i)) Next i
For i = 0 To n - 2 For j = i + 1 To n - 1 If a(i) < a(j) Then work = a(i) a(i) = a(j) a(j) = work EndIf Next j Next i TextBox2.Text = "" For i = 0 To n - 1
TextBox2.Text = TextBox2.Text & a(i) & vbCrLf Next i
練習5.2
コントロールの追加 プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim i, j, k, data(2, 5), work(2), heikinn(2) AsInteger Dim simei(5), worksimei AsString
For i = 0 To 5 simei(i) = TextBox1.Lines(i) data(0, i) = Val(TextBox2.Lines(i)) data(1, i) = Val(TextBox3.Lines(i)) Next i For i = 0 To 5
data(2, i) = data(0, i) + data(1, i) Next i
For i = 0 To 2 For j = 0 To 5
heikinn(i) = heikinn(i) + data(i, j) Next heikinn(i) = heikinn(i) / 6 Next TextBox5.Text = heikinn(0) TextBox6.Text = heikinn(1) TextBox7.Text = heikinn(2) For i = 0 To 4 For j = i + 1 To 5
If data(2, i) < data(2, j) Then worksimei = simei(i) simei(i) = simei(j) simei(j) = worksimei For k = 0 To 2 work(k) = data(k, i) data(k, i) = data(k, j) data(k, j) = work(k) Next EndIf Next
Next TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" For i = 0 To 5
TextBox1.Text = TextBox1.Text & simei(i) & vbCrLf TextBox2.Text = TextBox2.Text & data(0, i) & vbCrLf TextBox3.Text = TextBox3.Text & data(1, i) & vbCrLf TextBox4.Text = TextBox4.Text & data(2, i) & vbCrLf Next i EndSub 練習5.3 コントロールの配置 プロパティの変更 プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim i, j, k AsInteger
Dim a(3, 3), b(3, 3), c(3, 3) AsDouble a(1, 1) = Val(TextBox1.Text) a(1, 2) = Val(TextBox2.Text) a(1, 3) = Val(TextBox3.Text) a(2, 1) = Val(TextBox4.Text) a(2, 2) = Val(TextBox5.Text) a(2, 3) = Val(TextBox6.Text) a(3, 1) = Val(TextBox7.Text) a(3, 2) = Val(TextBox8.Text) a(3, 3) = Val(TextBox9.Text) b(1, 1) = Val(TextBox10.Text) b(1, 2) = Val(TextBox11.Text) b(1, 3) = Val(TextBox12.Text) b(2, 1) = Val(TextBox13.Text) b(2, 2) = Val(TextBox14.Text)
b(2, 3) = Val(TextBox15.Text) b(3, 1) = Val(TextBox16.Text) b(3, 2) = Val(TextBox17.Text) b(3, 3) = Val(TextBox18.Text) For i = 1 To 3 For j = 1 To 3 c(i, j) = 0 For k = 1 To 3
c(i, j) = c(i, j) + a(i, k) * b(k, j) Next Next Next TextBox19.Text = c(1, 1) TextBox20.Text = c(1, 2) TextBox21.Text = c(1, 3) TextBox22.Text = c(2, 1) TextBox23.Text = c(2, 2) TextBox24.Text = c(2, 3) TextBox25.Text = c(3, 1) TextBox26.Text = c(3, 2) TextBox27.Text = c(3, 3) EndSub 練習6.1 コントロールの配置 プロパティの変更 プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim n, s, a AsInteger
FileOpen(1, "data.txt", OpenMode.Input) n = 0 s = 0 DoUntil EOF(1) Input(1, a) n = n + 1 s = s + a Loop TextBox1.Text = n TextBox2.Text = s / n EndSub
練習6.2
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim a, maxa AsInteger
Dim namae, maxnamae AsString FileOpen(1, "data.txt", OpenMode.Input) maxnamae = "" maxa = 0 DoUntil EOF(1) Input(1, namae) Input(1, a) If (a > maxa) Then maxnamae = namae maxa = a EndIf Loop TextBox1.Text = maxnamae TextBox2.Text = maxa FileClose() EndSub 練習6.3 コントロールの配置 プロパティの変更
プログラムリスト
PublicStructure seiseki
<VBFixedString(12)> Public simei AsString Public kokugo AsInteger
Public suugaku AsInteger Public eigo AsInteger EndStructure
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim i AsInteger
Dim kojinseiseki As seiseki Dim fname AsString
SaveFileDialog1.ShowDialog() fname = SaveFileDialog1.FileName
FileOpen(1, fname, OpenMode.Random, , , Len(kojinseiseki)) For i = 1 To 6 kojinseiseki.simei = TextBox1.Lines(i - 1) kojinseiseki.kokugo = Val(TextBox2.Lines(i - 1)) kojinseiseki.suugaku = Val(TextBox3.Lines(i - 1)) kojinseiseki.eigo = Val(TextBox4.Lines(i - 1)) FilePut(1, kojinseiseki, i) Next FileClose(1) EndSub
PrivateSub Button2_Click(…省略…) Handles Button2.Click Dim i AsInteger
Dim kojinseiseki As seiseki Dim fname AsString
OpenFileDialog1.ShowDialog() fname = OpenFileDialog1.FileName
FileOpen(1, fname, OpenMode.Random, , , Len(kojinseiseki)) TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" For i = 1 To 6 FileGet(1, kojinseiseki, i)
TextBox1.Text = TextBox1.Text & kojinseiseki.simei & vbCrLf TextBox2.Text = TextBox2.Text & kojinseiseki.kokugo & vbCrLf TextBox3.Text = TextBox3.Text & kojinseiseki.suugaku & vbCrLf TextBox4.Text = TextBox4.Text & kojinseiseki.eigo & vbCrLf Next i
FileClose(1) EndSub
練習7.1
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim simei(5) AsString
Dim data(5, 2) AsInteger nyuuryoku(simei, data) keisan(data)
narabikae(simei, data) hyouji(simei, data) EndSub
PrivateSub nyuuryoku(ByRef simei() AsString, ByRef data(,) AsInteger) Dim i AsInteger For i = 0 To 5 simei(i) = TextBox1.Lines(i) data(i, 0) = Val(TextBox2.Lines(i)) data(i, 1) = Val(TextBox3.Lines(i)) Next i EndSub
PrivateSub keisan(ByRef data(,) AsInteger) Dim i AsInteger
For i = 0 To 5
data(i, 2) = data(i, 0) + data(i, 1) Next i
EndSub
PrivateSub narabikae(ByRef simei() AsString, ByRef data(,) AsInteger) Dim i, j, k, work AsInteger
Dim worksimei AsString For i = 0 To 4
For j = i + 1 To 5
If data(i, 2) < data(j, 2) Then worksimei = simei(i) simei(i) = simei(j) simei(j) = worksimei For k = 0 To 2
work = data(i, k) data(i, k) = data(j, k) data(j, k) = work Next k EndIf Next j Next i EndSub
PrivateSub hyouji(ByRef simei() AsString, ByRef data(,) AsInteger) Dim i AsInteger TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" For i = 0 To 5
TextBox1.Text = TextBox1.Text & simei(i) & vbCrLf TextBox2.Text = TextBox2.Text & data(i, 0) & vbCrLf TextBox3.Text = TextBox3.Text & data(i, 1) & vbCrLf TextBox4.Text = TextBox4.Text & data(i, 2) & vbCrLf Next i
EndSub
練習7.2
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub TextBox1_TextChanged(…省略…) Handles TextBox1.TextChanged Dim a AsInteger
a = Val(TextBox1.Text) TextBox2.Text = hantei(a) EndSub
Function hantei(ByVal a AsInteger) AsString If a >= 80 Then hantei = "A" ElseIf a >= 70 Then hantei = "B" ElseIf a >= 60 Then hantei = "C" Else
hantei = "D" EndIf EndFunction 練習7.3 コントロールの配置 プロパティの変更 プログラムリスト
PrivateSub TextBox1_TextChanged(…省略…) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged Dim k, a AsDouble Dim n AsInteger a = Val(TextBox1.Text) k = Val(TextBox2.Text) n = Val(TextBox3.Text) TextBox4.Text = fukuri(a, k, n) EndSub
Function fukuri(ByVal a AsDouble, ByVal k AsDouble, ByVal n AsInteger) AsInteger Dim i AsInteger For i = 1 To n a = a * (1 + k / 100) Next i fukuri = a EndFunction
練習8.1
コントロールの配置 プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim a As Graphics = PictureBox1.CreateGraphics() Dim i AsInteger For i = 0 To 330 Step 30 a.DrawPie(Pens.Red, 0, 0, 100, 100, i, 30) Next i EndSub 練習8.2 コントロールの配置 プロパティの変更 プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim a As Graphics = PictureBox1.CreateGraphics() Dim i, xs, xc AsInteger
Dim s AsDouble
PictureBox1
PictureBox1
For i = 0 To 720
s = i / 360 * 2 * Math.PI
xs = -50 * (Math.Sin(s) + 1 / 3 * Math.Sin(3 * s) + 1 / 5 * Math.Sin(5 * s)) + 60 a.FillRectangle(Brushes.Red, i, xs, 2, 2) Next i EndSub 練習8.3 コントロールの配置 プロパティの変更 プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim fname AsString
OpenFileDialog1.Filter = "Bitmap Image|*.bmp|JPeg Image|*.jpg|Gif Image|*.gif" OpenFileDialog1.ShowDialog()
fname = OpenFileDialog1.FileName
Dim bmap As Bitmap = New Bitmap(fname) PictureBox1.Image = bmap
PictureBox2.Size = PictureBox1.Size EndSub
PrivateSub Button2_Click(…省略…) Handles Button2.Click Dim fname AsString
OpenFileDialog1.Filter = "Bitmap Image|*.bmp|JPeg Image|*.jpg|Gif Image|*.gif" OpenFileDialog1.ShowDialog()
fname = OpenFileDialog1.FileName
Dim bmap As Bitmap = New Bitmap(fname) PictureBox2.Image = bmap
EndSub
PrivateSub Button3_Click(…省略…) Handles Button3.Click Dim bmpa As Bitmap = PictureBox1.Image
Dim bmpb As Bitmap = PictureBox2.Image
Dim iroa As Color Dim irob As Color Dim heikin AsInteger Dim i AsInteger Dim j AsInteger
For i = 0 To PictureBox1.Image.Width - 1 For j = 0 To PictureBox1.Image.Height - 1 iroa = bmpa.GetPixel(i, j)
heikin = Int((Val(iroa.R) + Val(iroa.G) + Val(iroa.B)) / 3) If (heikin < 255) Then bmpb.SetPixel(i, j, iroa) EndIf Next Next PictureBox2.Image = bmpb EndSub
PrivateSub Button4_Click(…省略…) Handles Button4.Click Dim fname AsString
SaveFileDialog1.Filter = "Bitmap Image|*.bmp" SaveFileDialog1.ShowDialog()
fname = SaveFileDialog1.FileName Dim bmap As Bitmap
bmap = PictureBox2.Image bmap.Save(fname) EndSub
練習9.1
サイコロの絵を描く
コントロールの配置
プロパティの変更
プログラムリスト
PrivateSub Button1_Click(…省略…) Handles Button1.Click Dim a AsInteger PictureBox1.Visible = False PictureBox2.Visible = False PictureBox3.Visible = False PictureBox4.Visible = False PictureBox5.Visible = False
PictureBox の BorderStyle を FixedSingle に変更 PictureBox1.Image にサイコロの1の絵 PictureBox2.Image にサイコロの2の絵 PictureBox3.Image にサイコロの3の絵 PictureBox4.Image にサイコロの4の絵 PictureBox5.Image にサイコロの5の絵 PictureBox6.Image にサイコロの6の絵 を設定(Image というプロパティ名をクリック するとボタンが表示されますので、そのボタンを クリックして画像ファイルを指定する) 全てのPictureBox を重ねる
PictureBox6.Visible = False a = Int(Rnd() * 6) + 1
If a = 1 Then PictureBox1.Visible = True If a = 2 Then PictureBox2.Visible = True If a = 3 Then PictureBox3.Visible = True If a = 4 Then PictureBox4.Visible = True If a = 5 Then PictureBox5.Visible = True If a = 6 Then PictureBox6.Visible = True EndSub
練習9.2
モグラの穴とモグラの絵を描く
コントロールの配置
プロパティの変更
PictureBox1 PictureBox3 PictureBox5
PictureBox2 PictureBox4 PictureBox6
PictureBox7 PictureBox9 PictureBox11
PictureBox8 PictureBox10 PictureBox12
Enabled を True Interval を 700 に変更
プログラムリスト Dim a AsInteger Dim pic(6) As PictureBox
PrivateSub Form1_Load(…省略…) HandlesMyBase.Load Dim i AsInteger pic(1) = PictureBox7 pic(2) = PictureBox8 pic(3) = PictureBox9 pic(4) = PictureBox10 pic(5) = PictureBox11 pic(6) = PictureBox12 For i = 1 To 6 pic(i).Visible = False
AddHandler pic(i).Click, AddressOf PictureBox_Click Next i
Randomize()
a = Int(Rnd() * 6) + 1 pic(a).Visible = True EndSub
PrivateSub PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Label1.Text = Val(Label1.Text) + 1
EndSub
PrivateSub Timer1_Tick(…省略…) Handles Timer1.Tick pic(a).Visible = False a = Int(Rnd() * 6 + 1) pic(a).Visible = True EndSub PuctureBox7 ~ PictureBox12 を PuctureBox1~PictureBox6 に重ねる
練習9.3 コントロールの配置 プロパティの変更 プログラムリスト Dim f1 AsInteger Dim f2 AsInteger Dim f3 AsInteger
PrivateSub Form1_Load(…省略…) HandlesMyBase.Load Randomize()
EndSub
PrivateSub Button4_Click(…省略…) Handles Button4.Click Label1.Text = Val(Label1.Text) - 1
f1 = 1 f2 = 1 f3 = 1 EndSub
PrivateSub Button1_Click(…省略…) Handles Button1.Click If (f1 = 1) Then
f1 = 0 hantei() EndIf EndSub
PrivateSub Button2_Click(…省略…) Handles Button2.Click If (f2 = 1) Then f2 = 0 hantei() EndIf EndSub Enabled を True Interval を 700 に変更
PrivateSub Button3_Click(…省略…) Handles Button3.Click If (f3 = 1) Then f3 = 0 hantei() EndIf EndSub
PrivateSub hantei()
If (f1 = 0 And f2 = 0 And f3 = 0) Then
If (TextBox1.Text = TextBox2.Text And TextBox1.Text = TextBox3.Text) Then Label1.Text = Label1.Text + 50
If (TextBox1.Text = 7) Then Label1.Text = Label1.Text + 50 EndIf
EndIf
If (TextBox4.Text = TextBox5.Text And TextBox4.Text = TextBox6.Text) Then Label1.Text = Label1.Text + 50
If (TextBox4.Text = 7) Then Label1.Text = Label1.Text + 50 EndIf
EndIf
If (TextBox7.Text = TextBox8.Text And TextBox7.Text = TextBox9.Text) Then Label1.Text = Label1.Text + 50
If (TextBox7.Text = 7) Then Label1.Text = Label1.Text + 50 EndIf
EndIf
If (TextBox1.Text = TextBox5.Text And TextBox1.Text = TextBox9.Text) Then Label1.Text = Label1.Text + 50
If (TextBox1.Text = 7) Then Label1.Text = Label1.Text + 50 EndIf
EndIf
If (TextBox3.Text = TextBox5.Text And TextBox3.Text = TextBox7.Text) Then Label1.Text = Label1.Text + 50 If (TextBox3.Text = 7) Then Label1.Text = Label1.Text + 50 EndIf EndIf EndIf EndSub
PrivateSub Timer1_Tick(…省略…) Handles Timer1.Tick If (f1 = 1) Then
TextBox1.Text = Int(Rnd() * 9) TextBox4.Text = Int(Rnd() * 9) TextBox7.Text = Int(Rnd() * 9) EndIf
If (f2 = 1) Then TextBox2.Text = Int(Rnd() * 9) TextBox5.Text = Int(Rnd() * 9) TextBox8.Text = Int(Rnd() * 9) EndIf If (f3 = 1) Then TextBox3.Text = Int(Rnd() * 9) TextBox6.Text = Int(Rnd() * 9) TextBox9.Text = Int(Rnd() * 9) EndIf EndSub