VisalBasicで一対比較アンケートを作ってみた。(紹介編)
後輩の依頼があったということもあり、今後も自分が使う機会は大いにあるしということもありで、VisualBasicで一対比較アンケートを作ってみました。項目を変えるたびにプログラムを書き換えるのは面倒ですから、外部のtxtファイルを書きかるだけで十分な仕様にしてみました。
とりあえず、フォームの見た目はこんな感じ:
中身はこんな感じです。(下の「続きを読む」をクリックするとコードが表示されます。)
コードの解説はまた記事を改めて書きたいと思います。
→続き:VisalBasicで一対比較アンケートを作ってみた。(解説編)
Form1.vb
Imports System.IO Imports System.Text Public Class Form1 Public items(0) As String Public Pairs(0, 0) As String Public Num As Integer Public Matrix(0, 0) As Double Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sr As New StreamReader("items.txt", Encoding.GetEncoding("Shift_JIS")) While (sr.Peek() >= 0) Dim buf As String = sr.ReadLine If items(items.Length - 1) = Nothing Then items(items.Length - 1) = buf Else ReDim Preserve items(items.Length) items(items.Length - 1) = buf End If End While sr.Close() Num = items.Length Dim sl As New System.Collections.SortedList Randomize() For i = 0 To Num * Num - 1 If i Mod Num <> System.Math.Floor(i / Num) Then sl.Add(Rnd(), (i Mod Num) + System.Math.Floor(i / Num) * 10) End If Next ReDim Pairs(Num * (Num - 1) - 1, 2 - 1) For i = 0 To sl.Count - 1 Pairs(i, 0) = items(System.Math.Floor(sl.GetByIndex(i) / 10)) Pairs(i, 1) = items(sl.GetByIndex(i) Mod 10) Next ReDim Matrix(Num - 1, Num - 1) For i = 0 To Num - 1 Matrix(i, i) = 0 Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Button1.Text = "実験開始" Then If NameTextBox.Text = "" Then MsgBox("お名前を入力してください。") NameTextBox.Focus() Exit Sub End If LeftLabel.Text = Pairs(0, 0) RightLabel.Text = Pairs(0, 1) CountMaxLabel.Text = "/" & Format(Num * (Num - 1), "000") Button1.Text = "次へ" Exit Sub End If Dim m As Integer, n As Integer For i = 0 To Num - 1 If LeftLabel.Text = items(i) Then m = i End If If RightLabel.Text = items(i) Then n = i End If Next Matrix(m, n) = TrackBar1.Value / 10 If Pairs.GetLength(0) = CountTextBox.Text Then MsgBox("評価終了") saveMatrix() Exit Sub End If CountTextBox.Text = CountTextBox.Text + 1 LeftLabel.Text = Pairs(CountTextBox.Text - 1, 0) RightLabel.Text = Pairs(CountTextBox.Text - 1, 1) TrackBar1.Value = 0 End Sub Private Sub saveMatrix() Dim sfd As New SaveFileDialog() sfd.FileName = NameTextBox.Text & ".csv" sfd.Filter = "CSVファイル(*.csv)|*.csv|すべてのファイル(*.*)|*.*" sfd.FilterIndex = 1 sfd.Title = "保存先のファイルを選択して下さい" sfd.RestoreDirectory = True If sfd.ShowDialog() = DialogResult.OK Then Dim sw As New StreamWriter(sfd.FileName, False, Encoding.GetEncoding("Shift_JIS")) sw.WriteLine("CSV保存年月日," & Format(Now, "yyyy/m/d") & ",CSV保存時刻," & Format(Now, "hh:MM:ss") & ",被験者氏名," & NameTextBox.Text) sw.Write("↓と比べて→は?,") For i = 0 To Num - 2 sw.Write(items(i) & ",") Next sw.WriteLine(items(Num - 1)) For i = 0 To Num - 1 sw.Write(items(i) & ",") For j = 0 To Num - 2 sw.Write(Matrix(i, j) & ",") Next sw.WriteLine(Matrix(i, Num - 1)) Next sw.Close() End If End Sub End Class
Form1.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class Form1 Inherits System.Windows.Forms.Form 'フォームがコンポーネントの一覧をクリーンアップするために dispose をオーバーライドします。 <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try If disposing AndAlso components IsNot Nothing Then components.Dispose() End If Finally MyBase.Dispose(disposing) End Try End Sub 'Windows フォーム デザイナーで必要です。 Private components As System.ComponentModel.IContainer 'メモ: 以下のプロシージャは Windows フォーム デザイナーで必要です。 'Windows フォーム デザイナーを使用して変更できます。 'コード エディターを使って変更しないでください。 <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.LeftLabel = New System.Windows.Forms.Label() Me.RightLabel = New System.Windows.Forms.Label() Me.TrackBar1 = New System.Windows.Forms.TrackBar() Me.Button1 = New System.Windows.Forms.Button() Me.CountTextBox = New System.Windows.Forms.TextBox() Me.NameTextBox = New System.Windows.Forms.TextBox() Me.NameLabel = New System.Windows.Forms.Label() Me.CountLabel = New System.Windows.Forms.Label() Me.CountMaxLabel = New System.Windows.Forms.Label() CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'LeftLabel ' Me.LeftLabel.AutoSize = True Me.LeftLabel.Font = New System.Drawing.Font("MS UI Gothic", 20.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.LeftLabel.Location = New System.Drawing.Point(12, 122) Me.LeftLabel.Name = "LeftLabel" Me.LeftLabel.Size = New System.Drawing.Size(105, 27) Me.LeftLabel.TabIndex = 0 Me.LeftLabel.Text = "Example" ' 'RightLabel ' Me.RightLabel.AutoSize = True Me.RightLabel.Font = New System.Drawing.Font("MS UI Gothic", 20.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.RightLabel.Location = New System.Drawing.Point(363, 122) Me.RightLabel.Name = "RightLabel" Me.RightLabel.Size = New System.Drawing.Size(105, 27) Me.RightLabel.TabIndex = 1 Me.RightLabel.Text = "Example" ' 'TrackBar1 ' Me.TrackBar1.Location = New System.Drawing.Point(122, 122) Me.TrackBar1.Maximum = 30 Me.TrackBar1.Minimum = -30 Me.TrackBar1.Name = "TrackBar1" Me.TrackBar1.Size = New System.Drawing.Size(235, 45) Me.TrackBar1.TabIndex = 2 Me.TrackBar1.TickFrequency = 30 Me.TrackBar1.TickStyle = System.Windows.Forms.TickStyle.Both ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(178, 173) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(125, 48) Me.Button1.TabIndex = 3 Me.Button1.Text = "実験開始" Me.Button1.UseVisualStyleBackColor = True ' 'CountTextBox ' Me.CountTextBox.Font = New System.Drawing.Font("MS UI Gothic", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.CountTextBox.Location = New System.Drawing.Point(374, 12) Me.CountTextBox.Name = "CountTextBox" Me.CountTextBox.Size = New System.Drawing.Size(43, 28) Me.CountTextBox.TabIndex = 4 Me.CountTextBox.Text = "1" Me.CountTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right ' 'NameTextBox ' Me.NameTextBox.Font = New System.Drawing.Font("MS UI Gothic", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.NameTextBox.Location = New System.Drawing.Point(82, 12) Me.NameTextBox.Name = "NameTextBox" Me.NameTextBox.Size = New System.Drawing.Size(159, 28) Me.NameTextBox.TabIndex = 5 ' 'NameLabel ' Me.NameLabel.AutoSize = True Me.NameLabel.Font = New System.Drawing.Font("MS UI Gothic", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.NameLabel.Location = New System.Drawing.Point(13, 17) Me.NameLabel.Name = "NameLabel" Me.NameLabel.Size = New System.Drawing.Size(63, 19) Me.NameLabel.TabIndex = 6 Me.NameLabel.Text = "お名前" ' 'CountLabel ' Me.CountLabel.AutoSize = True Me.CountLabel.Font = New System.Drawing.Font("MS UI Gothic", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.CountLabel.Location = New System.Drawing.Point(283, 17) Me.CountLabel.Name = "CountLabel" Me.CountLabel.Size = New System.Drawing.Size(85, 19) Me.CountLabel.TabIndex = 7 Me.CountLabel.Text = "実験回数" ' 'CountMaxLabel ' Me.CountMaxLabel.AutoSize = True Me.CountMaxLabel.Font = New System.Drawing.Font("MS UI Gothic", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.CountMaxLabel.Location = New System.Drawing.Point(423, 17) Me.CountMaxLabel.Name = "CountMaxLabel" Me.CountMaxLabel.Size = New System.Drawing.Size(49, 19) Me.CountMaxLabel.TabIndex = 8 Me.CountMaxLabel.Text = "/000" ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(484, 262) Me.Controls.Add(Me.CountMaxLabel) Me.Controls.Add(Me.CountLabel) Me.Controls.Add(Me.NameLabel) Me.Controls.Add(Me.NameTextBox) Me.Controls.Add(Me.CountTextBox) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TrackBar1) Me.Controls.Add(Me.RightLabel) Me.Controls.Add(Me.LeftLabel) Me.Name = "Form1" Me.Text = "Form1" CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents LeftLabel As System.Windows.Forms.Label Friend WithEvents RightLabel As System.Windows.Forms.Label Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents CountTextBox As System.Windows.Forms.TextBox Friend WithEvents NameTextBox As System.Windows.Forms.TextBox Friend WithEvents NameLabel As System.Windows.Forms.Label Friend WithEvents CountLabel As System.Windows.Forms.Label Friend WithEvents CountMaxLabel As System.Windows.Forms.Label End Class
items.txt(bin→Degub内)
りんご みかん バナナ なし パイナップル