Skip to content

Visual Basic.NET いろいろなテクニック⑥

Posted in Visual Basic .NET, プログラム関係, and 技術メモ


業務アプリで使用できそうなものを五月雨に記録しておく。
まとまるまで待っていると忘れてしまうために・・・。ww

なお、このブログ内に記載しているソースの転載・転用については何も縛りはありません。
ご使用の状況の如何に問わず、作者たる当方は結果についてその責任を一切負いません。ご使用は自己責任の範囲でお願いいたします。

■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□

【フォームの型(ベース)を使う】

業務アプリケーションでは同じような機能を持った同じような構成画面が複数作ることになるかと思います。
特にマスタ等の編集画面は機能や構成はほとんど同じで操作するマスタが違う程度で機能を使いまわして効率よく作成することが肝要です。

メリットとしては

1、同じ機能を何度も実装しなくて済む。
2、1つの実装機能を使いまわすことにより、保守性に優れる。
3、構成画面が同じな為、使用方法等を統一的にすることができる。
等です。

デメリットとしては…

1、使いまわす為にこの共通機能に間違いがあると全部に波及する。
2、画面個々に共通機能に対するオリジナルな機能の実装が現実的ではない。(反共通化な行動な為おススメはしませんがあえて言うなら…)

一様にメリット・デメリットはありますが、メリットの方が圧倒的に多いので世間では当然の考え方になります。
そこで、ベースフォームを作成して使いまわす方法を記載いたします。
尚、当方の環境で行っている為、多少の操作・設定に抜けがある可能性があるのはご了承ください。

ベースフォームを用意

まず、画面のようにメインプロジェクト(Sample1)とベースフォームプロジェクト(BaseForm)を含むソリューションを用意してください。
既存のソリューションにベースフォームプロジェクト(BaseForm)を追加しても可能です。

20161130_1

今回のベースフォームはいろいろなテクニック⑤で作成したファンクションキーを含むものを使用します。

Visual Basic.NET いろいろなテクニック⑤

画面デザインがこんな感じです。

20161130_2

機能としては
①機能タイトルの表示
②ログインユーザーID・ユーザー名の表示
③F1~F12までのフルファンクションキー
④ステータスバーのモード表示・コメント表示・時刻表示
⑤いろいろなテクニック⑤で紹介した「【ファンクションキーの有効・無効を制御する】」の機能を利用可能。
等です。
デザインソースは下記になります。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class BaseForm
    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.components = New System.ComponentModel.Container()
        Me.StatusStrip = New System.Windows.Forms.StatusStrip()
        Me.Button_F12 = New System.Windows.Forms.Button()
        Me.Button_F11 = New System.Windows.Forms.Button()
        Me.Button_F10 = New System.Windows.Forms.Button()
        Me.Button_F9 = New System.Windows.Forms.Button()
        Me.Button_F8 = New System.Windows.Forms.Button()
        Me.Button_F7 = New System.Windows.Forms.Button()
        Me.Button_F6 = New System.Windows.Forms.Button()
        Me.Button_F5 = New System.Windows.Forms.Button()
        Me.Button_F4 = New System.Windows.Forms.Button()
        Me.Button_F3 = New System.Windows.Forms.Button()
        Me.Button_F2 = New System.Windows.Forms.Button()
        Me.Button_F1 = New System.Windows.Forms.Button()
        Me.Label_UserID = New System.Windows.Forms.Label()
        Me.Label_Title = New System.Windows.Forms.Label()
        Me.Timer = New System.Windows.Forms.Timer(Me.components)
        Me.ToolStripStatusLabel_NowDayTime = New System.Windows.Forms.ToolStripStatusLabel()
        Me.ToolStripStatusLabel_Comment = New System.Windows.Forms.ToolStripStatusLabel()
        Me.ToolStripStatusLabel_StatusInfo = New System.Windows.Forms.ToolStripStatusLabel()
        Me.StatusStrip.SuspendLayout()
        Me.SuspendLayout()
        '
        'StatusStrip
        '
        Me.StatusStrip.AutoSize = False
        Me.StatusStrip.GripMargin = New System.Windows.Forms.Padding(0)
        Me.StatusStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripStatusLabel_StatusInfo, Me.ToolStripStatusLabel_Comment, Me.ToolStripStatusLabel_NowDayTime})
        Me.StatusStrip.Location = New System.Drawing.Point(0, 350)
        Me.StatusStrip.Name = "StatusStrip"
        Me.StatusStrip.Size = New System.Drawing.Size(1014, 25)
        Me.StatusStrip.SizingGrip = False
        Me.StatusStrip.TabIndex = 0
        Me.StatusStrip.Text = "StatusStrip1"
        '
        'Button_F12
        '
        Me.Button_F12.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F12.Location = New System.Drawing.Point(930, 315)
        Me.Button_F12.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F12.Name = "Button_F12"
        Me.Button_F12.Size = New System.Drawing.Size(82, 27)
        Me.Button_F12.TabIndex = 29
        Me.Button_F12.Text = "F12:"
        Me.Button_F12.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F12.UseVisualStyleBackColor = True
        '
        'Button_F11
        '
        Me.Button_F11.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F11.Location = New System.Drawing.Point(848, 315)
        Me.Button_F11.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F11.Name = "Button_F11"
        Me.Button_F11.Size = New System.Drawing.Size(82, 27)
        Me.Button_F11.TabIndex = 28
        Me.Button_F11.Text = "F11:"
        Me.Button_F11.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F11.UseVisualStyleBackColor = True
        '
        'Button_F10
        '
        Me.Button_F10.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F10.Location = New System.Drawing.Point(766, 315)
        Me.Button_F10.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F10.Name = "Button_F10"
        Me.Button_F10.Size = New System.Drawing.Size(82, 27)
        Me.Button_F10.TabIndex = 27
        Me.Button_F10.Text = "F10:"
        Me.Button_F10.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F10.UseVisualStyleBackColor = True
        '
        'Button_F9
        '
        Me.Button_F9.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F9.Location = New System.Drawing.Point(684, 315)
        Me.Button_F9.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F9.Name = "Button_F9"
        Me.Button_F9.Size = New System.Drawing.Size(82, 27)
        Me.Button_F9.TabIndex = 26
        Me.Button_F9.Text = "F9:"
        Me.Button_F9.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F9.UseVisualStyleBackColor = True
        '
        'Button_F8
        '
        Me.Button_F8.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F8.Location = New System.Drawing.Point(592, 315)
        Me.Button_F8.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F8.Name = "Button_F8"
        Me.Button_F8.Size = New System.Drawing.Size(82, 27)
        Me.Button_F8.TabIndex = 25
        Me.Button_F8.Text = "F8:"
        Me.Button_F8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F8.UseVisualStyleBackColor = True
        '
        'Button_F7
        '
        Me.Button_F7.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F7.Location = New System.Drawing.Point(510, 315)
        Me.Button_F7.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F7.Name = "Button_F7"
        Me.Button_F7.Size = New System.Drawing.Size(82, 27)
        Me.Button_F7.TabIndex = 24
        Me.Button_F7.Text = "F7:"
        Me.Button_F7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F7.UseVisualStyleBackColor = True
        '
        'Button_F6
        '
        Me.Button_F6.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F6.Location = New System.Drawing.Point(428, 315)
        Me.Button_F6.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F6.Name = "Button_F6"
        Me.Button_F6.Size = New System.Drawing.Size(82, 27)
        Me.Button_F6.TabIndex = 23
        Me.Button_F6.Text = "F6:"
        Me.Button_F6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F6.UseVisualStyleBackColor = True
        '
        'Button_F5
        '
        Me.Button_F5.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F5.Location = New System.Drawing.Point(346, 315)
        Me.Button_F5.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F5.Name = "Button_F5"
        Me.Button_F5.Size = New System.Drawing.Size(82, 27)
        Me.Button_F5.TabIndex = 22
        Me.Button_F5.Text = "F5:"
        Me.Button_F5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F5.UseVisualStyleBackColor = True
        '
        'Button_F4
        '
        Me.Button_F4.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F4.Location = New System.Drawing.Point(255, 315)
        Me.Button_F4.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F4.Name = "Button_F4"
        Me.Button_F4.Size = New System.Drawing.Size(82, 27)
        Me.Button_F4.TabIndex = 21
        Me.Button_F4.Text = "F4:"
        Me.Button_F4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F4.UseVisualStyleBackColor = True
        '
        'Button_F3
        '
        Me.Button_F3.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F3.Location = New System.Drawing.Point(173, 315)
        Me.Button_F3.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F3.Name = "Button_F3"
        Me.Button_F3.Size = New System.Drawing.Size(82, 27)
        Me.Button_F3.TabIndex = 20
        Me.Button_F3.Text = "F3:"
        Me.Button_F3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F3.UseVisualStyleBackColor = True
        '
        'Button_F2
        '
        Me.Button_F2.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F2.Location = New System.Drawing.Point(91, 315)
        Me.Button_F2.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F2.Name = "Button_F2"
        Me.Button_F2.Size = New System.Drawing.Size(82, 27)
        Me.Button_F2.TabIndex = 19
        Me.Button_F2.Text = "F2:"
        Me.Button_F2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F2.UseVisualStyleBackColor = True
        '
        'Button_F1
        '
        Me.Button_F1.Font = New System.Drawing.Font("MS ゴシック", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Button_F1.Location = New System.Drawing.Point(9, 315)
        Me.Button_F1.Margin = New System.Windows.Forms.Padding(0)
        Me.Button_F1.Name = "Button_F1"
        Me.Button_F1.Size = New System.Drawing.Size(82, 27)
        Me.Button_F1.TabIndex = 18
        Me.Button_F1.Text = "F1:"
        Me.Button_F1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button_F1.UseVisualStyleBackColor = True
        '
        'Label_UserID
        '
        Me.Label_UserID.BackColor = System.Drawing.Color.Transparent
        Me.Label_UserID.Font = New System.Drawing.Font("メイリオ", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label_UserID.ForeColor = System.Drawing.Color.Blue
        Me.Label_UserID.Location = New System.Drawing.Point(682, 10)
        Me.Label_UserID.Name = "Label_UserID"
        Me.Label_UserID.Size = New System.Drawing.Size(323, 30)
        Me.Label_UserID.TabIndex = 55
        Me.Label_UserID.Text = "Label_UserID"
        Me.Label_UserID.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'Label_Title
        '
        Me.Label_Title.BackColor = System.Drawing.Color.DarkBlue
        Me.Label_Title.Font = New System.Drawing.Font("MS ゴシック", 15.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label_Title.ForeColor = System.Drawing.Color.Cyan
        Me.Label_Title.Location = New System.Drawing.Point(9, 9)
        Me.Label_Title.Name = "Label_Title"
        Me.Label_Title.Size = New System.Drawing.Size(328, 30)
        Me.Label_Title.TabIndex = 54
        Me.Label_Title.Text = "タイトル"
        Me.Label_Title.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Timer
        '
        Me.Timer.Enabled = True
        '
        'ToolStripStatusLabel_NowDayTime
        '
        Me.ToolStripStatusLabel_NowDayTime.AutoSize = False
        Me.ToolStripStatusLabel_NowDayTime.BorderSides = CType((((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Top) _
            Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Right) _
            Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Bottom), System.Windows.Forms.ToolStripStatusLabelBorderSides)
        Me.ToolStripStatusLabel_NowDayTime.BorderStyle = System.Windows.Forms.Border3DStyle.RaisedInner
        Me.ToolStripStatusLabel_NowDayTime.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
        Me.ToolStripStatusLabel_NowDayTime.Font = New System.Drawing.Font("MS ゴシック", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.ToolStripStatusLabel_NowDayTime.Margin = New System.Windows.Forms.Padding(0, 3, 0, 0)
        Me.ToolStripStatusLabel_NowDayTime.Name = "ToolStripStatusLabel_NowDayTime"
        Me.ToolStripStatusLabel_NowDayTime.Size = New System.Drawing.Size(200, 22)
        Me.ToolStripStatusLabel_NowDayTime.Text = "ToolStripStatusLabel1"
        '
        'ToolStripStatusLabel_Comment
        '
        Me.ToolStripStatusLabel_Comment.AutoSize = False
        Me.ToolStripStatusLabel_Comment.BorderSides = CType((((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Top) _
            Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Right) _
            Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Bottom), System.Windows.Forms.ToolStripStatusLabelBorderSides)
        Me.ToolStripStatusLabel_Comment.BorderStyle = System.Windows.Forms.Border3DStyle.RaisedInner
        Me.ToolStripStatusLabel_Comment.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
        Me.ToolStripStatusLabel_Comment.Font = New System.Drawing.Font("MS ゴシック", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.ToolStripStatusLabel_Comment.Margin = New System.Windows.Forms.Padding(0, 3, 0, 0)
        Me.ToolStripStatusLabel_Comment.Name = "ToolStripStatusLabel_Comment"
        Me.ToolStripStatusLabel_Comment.Size = New System.Drawing.Size(600, 22)
        Me.ToolStripStatusLabel_Comment.Text = "ToolStripStatusLabel2"
        '
        'ToolStripStatusLabel_StatusInfo
        '
        Me.ToolStripStatusLabel_StatusInfo.AutoSize = False
        Me.ToolStripStatusLabel_StatusInfo.BorderSides = CType((((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Top) _
            Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Right) _
            Or System.Windows.Forms.ToolStripStatusLabelBorderSides.Bottom), System.Windows.Forms.ToolStripStatusLabelBorderSides)
        Me.ToolStripStatusLabel_StatusInfo.BorderStyle = System.Windows.Forms.Border3DStyle.RaisedInner
        Me.ToolStripStatusLabel_StatusInfo.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text
        Me.ToolStripStatusLabel_StatusInfo.Font = New System.Drawing.Font("MS ゴシック", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.ToolStripStatusLabel_StatusInfo.Margin = New System.Windows.Forms.Padding(0, 3, 0, 0)
        Me.ToolStripStatusLabel_StatusInfo.Name = "ToolStripStatusLabel_StatusInfo"
        Me.ToolStripStatusLabel_StatusInfo.Size = New System.Drawing.Size(200, 22)
        Me.ToolStripStatusLabel_StatusInfo.Text = "ToolStripStatusLabel3"
        '
        'BaseForm
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(1014, 375)
        Me.Controls.Add(Me.Label_UserID)
        Me.Controls.Add(Me.Label_Title)
        Me.Controls.Add(Me.Button_F12)
        Me.Controls.Add(Me.Button_F11)
        Me.Controls.Add(Me.Button_F10)
        Me.Controls.Add(Me.Button_F9)
        Me.Controls.Add(Me.Button_F8)
        Me.Controls.Add(Me.Button_F7)
        Me.Controls.Add(Me.Button_F6)
        Me.Controls.Add(Me.Button_F5)
        Me.Controls.Add(Me.Button_F4)
        Me.Controls.Add(Me.Button_F3)
        Me.Controls.Add(Me.Button_F2)
        Me.Controls.Add(Me.Button_F1)
        Me.Controls.Add(Me.StatusStrip)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Name = "BaseForm"
        Me.Text = "BaseForm"
        Me.StatusStrip.ResumeLayout(False)
        Me.StatusStrip.PerformLayout()
        Me.ResumeLayout(False)
 
    End Sub
    Public WithEvents StatusStrip As System.Windows.Forms.StatusStrip
    Friend WithEvents ToolStripStatusLabel_StatusInfo As System.Windows.Forms.ToolStripStatusLabel
    Friend WithEvents ToolStripStatusLabel_Comment As System.Windows.Forms.ToolStripStatusLabel
    Public WithEvents ToolStripStatusLabel_NowDayTime As System.Windows.Forms.ToolStripStatusLabel
    Public WithEvents Button_F12 As System.Windows.Forms.Button
    Public WithEvents Button_F11 As System.Windows.Forms.Button
    Public WithEvents Button_F10 As System.Windows.Forms.Button
    Public WithEvents Button_F9 As System.Windows.Forms.Button
    Public WithEvents Button_F8 As System.Windows.Forms.Button
    Public WithEvents Button_F7 As System.Windows.Forms.Button
    Public WithEvents Button_F6 As System.Windows.Forms.Button
    Public WithEvents Button_F5 As System.Windows.Forms.Button
    Public WithEvents Button_F4 As System.Windows.Forms.Button
    Public WithEvents Button_F3 As System.Windows.Forms.Button
    Public WithEvents Button_F2 As System.Windows.Forms.Button
    Public WithEvents Button_F1 As System.Windows.Forms.Button
    Public WithEvents Label_UserID As System.Windows.Forms.Label
    Public WithEvents Label_Title As System.Windows.Forms.Label
    Friend WithEvents Timer As System.Windows.Forms.Timer
 
End Class

ベースフォームのコードソースです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Public Class BaseForm
    Inherits System.Windows.Forms.Form
    'ボタンコントロール配列のフィールドを作成
    Private FuncButtons() As System.Windows.Forms.Button
    'ステータスバーの情報(モード)用変数
    Public strInfoMode As String = String.Empty
    'ステータスバーの情報(モードの文字カラー)用変数
    Public strInfoModeForeColor As Drawing.Color = Color.Black
    'ステータスバーの情報(モードのバックカラー)用変数
    Public strInfoModeBackColor As Drawing.Color = Color.White
    'ステータスバーの情報(項目コメント)用変数
    Public strInfoComment As String = String.Empty
 
    Private Sub BaseForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
        'ボタンコントロール配列の作成
        Me.FuncButtons = New System.Windows.Forms.Button(11) {}
        'ボタンコントロールの配列にすでに作成されているインスタンスを代入
        Me.FuncButtons(0) = Me.Button_F1
        Me.FuncButtons(1) = Me.Button_F2
        Me.FuncButtons(2) = Me.Button_F3
        Me.FuncButtons(3) = Me.Button_F4
        Me.FuncButtons(4) = Me.Button_F5
        Me.FuncButtons(5) = Me.Button_F6
        Me.FuncButtons(6) = Me.Button_F7
        Me.FuncButtons(7) = Me.Button_F8
        Me.FuncButtons(8) = Me.Button_F9
        Me.FuncButtons(9) = Me.Button_F10
        Me.FuncButtons(10) = Me.Button_F11
        Me.FuncButtons(11) = Me.Button_F12
 
        'コントロールの初期化
        With Me
            'ユーザー
            .Label_UserID.Text = String.Empty
            .ToolStripStatusLabel_Comment.Width = .StatusStrip.Width - .ToolStripStatusLabel_StatusInfo.Width - ToolStripStatusLabel_NowDayTime.Width
 
            .ToolStripStatusLabel_StatusInfo.Text = String.Empty
            .ToolStripStatusLabel_StatusInfo.ForeColor = Color.Black
            .ToolStripStatusLabel_StatusInfo.BackColor = Color.WhiteSmoke
 
            .ToolStripStatusLabel_NowDayTime.Text = String.Empty
            .Label_Title.Text = String.Empty
            'ステータスバーの表示(モード)
            .ToolStripStatusLabel_StatusInfo.Text = String.Empty
            .ToolStripStatusLabel_Comment.Text = String.Empty
 
        End With
 
    End Sub
 
    '--------------------------------------------------------------------------------
    'ステータスバーの表示
    '--------------------------------------------------------------------------------
    Private Sub Timer_Tick(sender As System.Object, e As System.EventArgs) Handles Timer.Tick
 
        'ステータスバーの表示(時刻)
        ToolStripStatusLabel_NowDayTime.Text = Format(Now, "yyyy/MM/dd HH:mm")
 
        'モード名を出力
        ToolStripStatusLabel_StatusInfo.Text = strInfoMode
        ToolStripStatusLabel_StatusInfo.ForeColor = strInfoModeForeColor
        ToolStripStatusLabel_StatusInfo.BackColor = strInfoModeBackColor
        'コメントを出力
        ToolStripStatusLabel_Comment.Text = strInfoComment
 
 
    End Sub
 
    ''' <summary>
    ''' ファンクションキーの有効無効制御イベント
    ''' </summary>
    ''' <param name="strFCode">ファンクション文字列(12桁)</param>
    ''' <remarks></remarks>
    Public Sub BaseForm12_Function(ByVal strFCode As String)
 
        Dim intI As Integer = 0
        Dim intA As Integer = 0
 
        If Len(strFCode) <> 14 Then
            MessageBox.Show("ファンクション文字列が不正です。" & Chr(13) & "終了いたします。", _
                            "BaseForm_Functionエラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End
        End If
 
        For intI = 1 To 14
            '「-」を除外
            Select Case intI
                Case 1 To 4
                    intA = 1
                Case 5 To 9
                    intA = 2
                Case 10 To 14
                    intA = 3
            End Select
            'チェック
            If Mid(strFCode, intI, 1) = "0" Then
                Me.FuncButtons(intI - intA).Enabled = False
 
            ElseIf Mid(strFCode, intI, 1) = "1" Then
                Me.FuncButtons(intI - intA).Enabled = True
 
            ElseIf Mid(strFCode, intI, 1) = "-" Then
                '「-」は区切り記号の為に何もしない
 
            Else
                Me.FuncButtons(intI - intA).Enabled = False
 
            End If
        Next
    End Sub
 
End Class

メインフォームにベースフォームを関連付ける

メインフォーム(機能を張り付ける側)の構成です。
一応、メインフォームはベースフォームと同じ大きさで構成すると考え方は楽です。
作成画面はこんな感じ。何とも殺風景。

20161130_3

デザインソースは

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class MainForm
    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.SuspendLayout()
        '
        'MainForm
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(1014, 375)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MinimizeBox = False
        Me.Name = "MainForm"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "MainForm"
        Me.ResumeLayout(False)
 
    End Sub
 
End Class

メインフォームのソース

1
2
3
4
5
Public Class MainForm
    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
    End Sub
End Class

ここでBaseFormをコンパイルしておきます。
MainFormプロジェクトを開いて参照タブを開きます。

20161130_4

参照の追加を行います。「追加」ボタンをクリックして「プロジェクト」のタブをクリックします。

20161130_5

そうすると画面のように同じソリューションのBaseFormが選択できます。これを選択します。

20161130_6

MainFormのコードを開き設定をいれます。

1
2
3
4
5
6
7
Public Class MainForm
    Inherits BaseForm.BaseForm
 
    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
    End Sub
End Class

そうするとこんな感じでエラーが出ます。

20161130_7

20161130_8

エラーにマウスカーソルを合わせるとこんなことを言っています。

20161130_9

上段の「BaseForm.BaseFormから継承するためにクラス’MainForm’を変更します。」をクリックします。
すると、MainForm画面のデザインに戻ればベースの内容が張り付いてりようできるようになります。

20161130_10

これで機能の使い回しが可能です。他のフォームでも同様にベースフォームを使いまわせます。
いろいろ応用可能です。

Be First to Comment

    コメントを残す

    メールアドレスが公開されることはありません。 が付いている欄は必須項目です

    CAPTCHA