Vb.net-forms

提供:Dev Guides
移動先:案内検索

VB.Net-フォーム

Microsoft Visual Studioで次の手順を実行して、ウィンドウフォームアプリケーションの作成から始めましょう-ファイル→新規プロジェクト→Windowsフォームアプリケーション

最後に、[OK]を選択すると、Microsoft Visual Studioがプロジェクトを作成し、 Form1 という名前の次のウィンドウフォームを表示します。

VB.Net Form

Visual Basicフォームは、ユーザーインターフェイスを構成するすべてのコントロールのコンテナーです。 実行中のVisual Basicアプリケーションで表示されるすべてのウィンドウはフォームであるため、フォームとウィンドウという用語は同じエンティティを表します。 Visual Studioは、* Windowsフォームアプリケーション*を作成すると、デフォルトのフォームを作成します。

すべてのフォームにはタイトルバーがあり、その上にフォームのキャプションが表示されます。次に示すフォームを閉じたり、最大化したり、最小化したりするためのボタンがあります-

VB.Netフォームのタイトルバー

左上隅のアイコンをクリックすると、コントロールメニューが開きます。コントロールメニューには、フォームを制御するためのさまざまなコマンドが含まれています。

フォームのプロパティ

次の表に、フォームに関連するさまざまな重要なプロパティを示します。 これらのプロパティは、アプリケーションの実行中に設定または読み取ることができます。 フォームコントロールに関連付けられているプロパティの完全なリストについては、Microsoftのドキュメントを参照してください-

S.N Properties Description
1 AcceptButton The button that’s automatically activated when you press Enter, no matter which control has the focus at the time. Usually the OK button on a form is set as AcceptButton for a form.
2 CancelButton

The button that’s automatically activated when you hit the Esc key.

通常、フォームの[キャンセル]ボタンは、フォームの[キャンセル]ボタンとして設定されます。

3 AutoScale This Boolean property determines whether the controls you place on the form are automatically scaled to the height of the current font. The default value of this property is True. This is a property of the form, but it affects the controls on the form.
4 AutoScroll This Boolean property indicates whether scroll bars will be automatically attached to the form if it is resized to a point that not all its controls are visible.
5 AutoScrollMinSize This property lets you specify the minimum size of the form, before the scroll bars are attached.
6 AutoScrollPosition The AutoScrollPosition is the number of pixels by which the two scroll bars were displaced from their initial locations.
7 BackColor Sets the form background color.
8 BorderStyle

The BorderStyle property determines the style of the form’s border and the appearance of the form −

  • なし-サイズ変更できないボーダーレスウィンドウ。
  • Sizable -これはデフォルト値であり、通常のフォームの表示に使用されるサイズ変更可能なウィンドウに使用されます。
  • Fixed3D -メイン領域に対して「盛り上がった」可視境界線を持つウィンドウ。 この場合、ウィンドウのサイズを変更することはできません。
  • FixedDialog -ダイアログボックスの作成に使用される固定ウィンドウ。
  • FixedSingle -単一行の境界線を持つ固定ウィンドウ。
  • FixedToolWindow -閉じるボタンのみの固定ウィンドウ。 描画およびイメージングアプリケーションによって表示されるツールバーのように見えます。
  • SizableToolWindow -FixedToolWindowと同じですが、サイズ変更可能です。 また、そのキャプションフォントは通常よりも小さくなっています。
9 ControlBox By default, this property is True and you can set it to False to hide the icon and disable the Control menu.
10 Enabled If True, allows the form to respond to mouse and keyboard events; if False, disables form.
11 Font This property specify font type, style, size
12 HelpButton Determines whether a Help button should be displayed in the caption box of the form.
13 Height This is the height of the Form in pixels.
14 MinimizeBox By default, this property is True and you can set it to False to hide the Minimize button on the title bar.
15 MaximizeBox By default, this property is True and you can set it to False to hide the Maximize button on the title bar.
16 MinimumSize This specifies the minimum height and width of the window you can minimize.
17 MaximumSize This specifies the maximum height and width of the window you maximize.
18 Name This is the actual name of the form.
19 StartPosition

This property determines the initial position of the form when it’s first displayed. It will have any of the following values −

  • CenterParent -フォームは親フォームの領域の中央に配置されます。
  • CenterScreen -フォームはモニターの中央に配置されます。
  • 手動-フォームの場所とサイズによって開始位置が決まります。
  • WindowsDefaultBounds -フォームは、Windowsによって決定されたデフォルトの場所とサイズに配置されます。
  • WindowsDefaultLocation -フォームはWindowsのデフォルトの場所に配置され、設計時に設定した寸法を持ちます。
20 Text The text, which will appear at the title bar of the form.
21 Top, Left These two properties set or return the coordinates of the form’s top-left corner in pixels.
22 TopMost This property is a True/False value that lets you specify whether the form will remain on top of all other forms in your application. Its default property is False.
23 *Width * This is the width of the form in pixel.

フォームメソッド

以下は、Formクラスの一般的に使用されるメソッドの一部です。 フォームコントロールに関連するメソッドの完全なリストについては、Microsoftのドキュメントを参照してください-

Sr.No. Method Name & Description
1
  • Activate*

フォームをアクティブにし、フォーカスを与えます。

2

ActivateMdiChild

フォームのMDI子をアクティブ化します。

3

AddOwnedForm

所有フォームをこのフォームに追加します。

4

BringToFront

コントロールをzオーダーの前面に移動します。

5

CenterToParent

親フォームの境界内でフォームの位置を中央揃えにします。

6

CenterToScreen

フォームを現在の画面の中央に配置します。

7

Close

フォームを閉じます。

8

Contains

指定したコントロールがコントロールの子かどうかを示す値を取得します。

9

Focus

コントロールに入力フォーカスを設定します。

10

Hide

コントロールをユーザーから隠します。

11

Refresh

コントロールがクライアント領域を無効にし、それ自体と子コントロールをすぐに再描画するように強制します。

12

Scale(SizeF)

指定された倍率でコントロールとすべての子コントロールを拡大縮小します。

13

ScaleControl

コントロールの位置、サイズ、パディング、マージンを拡大縮小します。

14

ScaleCore

フォームのスケーリングを実行します。

15

Select

コントロールをアクティブにします。

16

SendToBack

コントロールをzオーダーの後ろに送ります。

17

SetAutoScrollMargin

自動スクロールの余白のサイズを設定します。

18

SetDesktopBounds

デスクトップ座標でフォームの境界を設定します。

19

SetDesktopLocation

フォームの場所をデスクトップ座標で設定します。

20

SetDisplayRectLocation

表示ウィンドウを指定された値に配置します。

21

Show

コントロールをユーザーに表示します。

22

ShowDialog

フォームをモーダルダイアログボックスとして表示します。

フォームイベント

次の表に、フォームに関連するさまざまな重要なイベントを示します。 フォームコントロールに関連するイベントの完全なリストについては、Microsoftのドキュメントを参照してください-

Sr.No. Event Description
1 Activated Occurs when the form is activated in code or by the user.
2 Click Occurs when the form is clicked.
3 Closed Occurs before the form is closed.
4 Closing Occurs when the form is closing.
5 DoubleClick Occurs when the form control is double-clicked.
6 DragDrop Occurs when a drag-and-drop operation is completed.
7 Enter Occurs when the form is entered.
8 GotFocus Occurs when the form control receives focus.
9 *HelpButtonClicked * Occurs when the* Help* button is clicked.
10 KeyDown Occurs when a key is pressed while the form has focus.
11 KeyPress Occurs when a key is pressed while the form has focus.
12 KeyUp Occurs when a key is released while the form has focus.
13 Load Occurs before a form is displayed for the first time.
14 LostFocus Occurs when the form loses focus.
15 MouseDown Occurs when the mouse pointer is over the form and a mouse button is pressed.
16 MouseEnter Occurs when the mouse pointer enters the form.
17 MouseHover Occurs when the mouse pointer rests on the form.
18 MouseLeave Occurs when the mouse pointer leaves the form.
19 MouseMove Occurs when the mouse pointer is moved over the form.
20 MouseUp Occurs when the mouse pointer is over the form and a mouse button is released.
21 MouseWheel Occurs when the mouse wheel moves while the control has focus.
22 Move Occurs when the form is moved.
23 Resize Occurs when the control is resized.
24 Scroll Occurs when the user or code scrolls through the client area.
25 Shown Occurs whenever the form is first displayed.
26 VisibleChanged Occurs when the Visible property value changes.

以下に例を示します。フォームの読み込みイベント時に2つのボタンを作成し、異なるプロパティを同時に設定する方法を示しています。

*Form1* は独自のイベントハンドラー内で参照されているため、名前を使用する代わりに *Me* として記述されますが、他のコントロールのイベントハンドラー内で同じフォームにアクセスすると、その名前を使用してアクセスされます *Form1* 。

フォームをダブルクリックして、開いたウィンドウに次のコードを配置しましょう。

Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Create two buttons to use as the accept and cancel buttons.
      Dim button1 As New Button()
      Dim button2 As New Button()
      ' Set the text of button1 to "OK".

      button1.Text = "OK"
      ' Set the position of the button on the form.
      button1.Location = New Point(10, 10)
      ' Set the text of button2 to "Cancel".

      button2.Text = "Cancel"
      ' Set the position of the button based on the location of button1.
      button2.Location = _
         New Point(button1.Left, button1.Height + button1.Top + 10)
      ' Set the caption bar text of the form.

      Me.Text = "finddevguides.com"
      ' Display a help button on the form.
      Me.HelpButton = True
      ' Define the border style of the form to a dialog box.

      Me.FormBorderStyle = FormBorderStyle.FixedDialog
      ' Set the MaximizeBox to false to remove the maximize box.
      Me.MaximizeBox = False
      ' Set the MinimizeBox to false to remove the minimize box.

      Me.MinimizeBox = False
      ' Set the accept button of the form to button1.
      Me.AcceptButton = button1
      ' Set the cancel button of the form to button2.

      Me.CancelButton = button2
      ' Set the start position of the form to the center of the screen.
      Me.StartPosition = FormStartPosition.CenterScreen
      ' Set window width and height

      Me.Height = 300
      Me.Width = 560
      ' Add button1 to the form.

      Me.Controls.Add(button1)
      ' Add button2 to the form.
      Me.Controls.Add(button2)
   End Sub
End Class

上記のコードが実行され、Microsoft Visual Studioツールバーで使用可能な Start ボタンを使用して実行されると、次のウィンドウが表示されます-

結果のフォーム