Form クラス
アセンブリ: System.Web.Mobile (system.web.mobile.dll 内)
構文
解説
フォームは、ASP.NET モバイル Web ページ内で最も外側のコントロール グループを表現します。個々のモバイル Web ページには、最も外側のレベルで複数のフォームを格納できます。フォームを入れ子にすることはできません。コンテナを入れ子にしたい場合は、Panel コントロールを使用してください。詳細については、「Form コントロールの概要」を参照してください。特定のフォームを表示するには、現在のページ上で ActiveForm プロパティを目的のフォームに設定するか、Link コントロールの NavigateUrl プロパティを目的のフォームに設定します。Form コントロールのテキストには、マークアップ タグを伴うリテラル テキストを含めることができます。テンプレートを使用する場合は、Form コントロールがフォームの OnInit メソッドでテンプレートのインスタンスを作成することに注意してください。フォームの OnInit メソッドは、Page_Load および Page_Init より前に呼び出されます。また、この時点ではフォームがまだ作成されていないため、ページ コンストラクタでは実行タイミングが早すぎて OnInit メソッドでテンプレートを設定することはできません。この問題を修正するには、フォーム自体の OnInit メソッドをフックし、そこでテンプレートのインスタンスを作成します。詳細については、「テンプレート レンダリングの実装」を参照してください。
2 つのフォームがあり、そのフォーム間にリンクがあるページを作成する方法を次のコード例に示します。一方のフォームにはチェック ボックス リストがあります。項目を選択して [送信] ボタンをクリックすると、選択した項目とその値の一覧がフォームに表示されます。Activate イベント メソッドは、各フォームの表示を準備します。
メモ |
---|
次のコード例はシングルファイル コード モデルを使用しており、分離コード ファイルに直接コピーされた場合は正常に動作しない可能性があります。このコード例は、拡張子が .aspx の空のテキスト ファイルにコピーする必要があります。詳細については、「ASP.NET Web ページのコード モデル」を参照してください。 |
<%@ Page Language="VB" Inherits="System.Web.UI.MobileControls.MobilePage" %> <%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %> <%@ Import Namespace="System.Web.Mobile" %> <%@ Import Namespace="System.Web.UI.MobileControls" %> <%@ Import Namespace="System.Drawing" %> <script runat="server"> ' When Form1 is activated Private Sub Form1_Activate(ByVal sender As Object, _ ByVal e As EventArgs) Dim viewText As String = "You have viewed this Form {0} times." ' First viewing If (count = 0) Then message2.Text = "Welcome to the Form Sample" Else ' subsequent viewings message2.Text = String.Format(viewText, _ (count + 1).ToString()) End If ' Format the form Form1.Alignment = Alignment.Center Form1.Wrapping = Wrapping.NoWrap Form1.BackColor = Color.LightBlue Form1.ForeColor = Color.Blue Form1.Paginate = True ' Create an array and add the tasks to it. Dim arr As ArrayList = New ArrayList() arr.Add(New Task("Verify transactions", "Done")) arr.Add(New Task("Check balance sheet", "Scheduled")) arr.Add(New Task("Send report", "Pending")) ' Bind the SelectionList to the array. SelectionList1.DataValueField = "Status" SelectionList1.DataTextField = "TaskName" SelectionList1.DataSource = arr SelectionList1.DataBind() End Sub ' When Form1 is deactivated Private Sub Form1_Deactivate(ByVal sender As Object, _ ByVal e As EventArgs) count += 1 End Sub ' When Form2 is activated Private Sub Form2_Activate(ByVal sender As Object, _ ByVal e As EventArgs) Form2.BackColor = Color.DarkGray Form2.ForeColor = Color.White Form2.Font.Bold = BooleanOption.True End Sub ' The the Submit button is clicked Protected Sub Command1_OnSubmit(ByVal sender As Object, _ ByVal e As EventArgs) Dim i As Integer message2.Text = "FORM RESULTS:" message2.Font.Bold = BooleanOption.True ' Create a string and a TextView control Dim txtView As TextView = New TextView() Dim txt As String = "" Dim spec As String = "{0} is {1}<br />" ' Display a list of selected items with values For i = 0 To SelectionList1.Items.Count - 1 ' Get the ListItem Dim itm As MobileListItem = SelectionList1.Items(i) ' List the selected items and values If itm.Selected Then txt &= String.Format(spec, itm.Text, itm.Value) End If Next ' Put the text into the TextView txtView.Text = txt ' Add the TextView to the form Form1.Controls.Add(txtView) ' Hide unnecessary controls SelectionList1.Visible = False link1.Visible = False Command1.Visible = False End Sub ' Property to persist the count between postbacks Private Property count() As Integer Get Dim o As Object = ViewState("FormCount") If IsNothing(o) Then Return 0 Else Return CType(o, Integer) End If End Get Set(ByVal value As Integer) ViewState("FormCount") = value End Set End Property ' A custom class for the task array Private Class Task Private _TaskName As String Private _Status As String Public Sub New(ByVal TaskName As String, ByVal Status As String) _TaskName = TaskName _Status = Status End Sub Public ReadOnly Property TaskName() As String Get Return _TaskName End Get End Property Public ReadOnly Property Status() As String Get Return _Status End Get End Property End Class </script> <html xmlns="http:'www.w3.org/1999/xhtml" > <body> <!-- The first form: Form1 --> <mobile:Form ID="Form1" Runat="server" OnDeactivate="Form1_Deactivate" OnActivate="Form1_Activate"> <mobile:Label ID="message1" Runat="server"> Welcome to ASP.NET </mobile:Label> <mobile:Label ID="message2" Runat="server" /> <mobile:SelectionList Runat="server" ID="SelectionList1" ForeColor="red" SelectType="CheckBox" /> <mobile:Link ID="link1" Runat="server" NavigateUrl="#Form2" Text="Next Form" /><br /> <mobile:Command ID="Command1" Runat="server" Text="Submit" OnClick="Command1_OnSubmit" /> </mobile:Form> <!-- The second form: Form2 --> <mobile:Form ID="Form2" Runat="server" OnActivate="Form2_Activate"> <mobile:Label ID="message4" Runat="server"> Welcome to ASP.NET </mobile:Label> <mobile:Link ID="Link2" Runat="server" NavigateUrl="#Form1" Text="Back" /> </mobile:Form> </body> </html>
<%@ Page Language="C#" Inherits="System.Web.UI.MobileControls.MobilePage" %> <%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %> <%@ Import Namespace="System.Web.Mobile" %> <%@ Import Namespace="System.Web.UI.MobileControls" %> <%@ Import Namespace="System.Drawing" %> <script runat="server"> // When Form1 is activated private void Form1_Activate(object sender, EventArgs e) { string viewText = "You have viewed this Form {0} times."; if (count == 0) // First viewing message2.Text = "Welcome to the Form Sample"; else // subsequent viewings message2.Text = String.Format(viewText, (count + 1).ToString()); // Format the form Form1.Alignment = Alignment.Center; Form1.Wrapping = Wrapping.NoWrap; Form1.BackColor = Color.LightBlue; Form1.ForeColor = Color.Blue; Form1.Paginate = true; // Create an array and add the tasks to it. ArrayList arr = new ArrayList(); arr.Add(new Task("Verify transactions", "Done")); arr.Add(new Task("Check balance sheet", "Scheduled")); arr.Add(new Task("Send report", "Pending")); // Bind the SelectionList to the array. SelectionList1.DataValueField = "Status"; SelectionList1.DataTextField = "TaskName"; SelectionList1.DataSource = arr; SelectionList1.DataBind(); } // When Form1 is deactivated private void Form1_Deactivate(object sender, EventArgs e) { count++; } // When Form2 is activated private void Form2_Activate(object sender, EventArgs e) { Form2.BackColor = Color.DarkGray; Form2.ForeColor = Color.White; Form2.Font.Bold = BooleanOption.True; } // The the Submit button is clicked protected void Command1_OnSubmit(object sender, EventArgs e) { message2.Text = "FORM RESULTS:"; message2.Font.Bold = BooleanOption.True; // Display a list of selected items with values for (int i = 0; i < SelectionList1.Items.Count; i++) { // Create a string and a TextView control TextView txtView = new TextView(); string txt = ""; string spec = "{0} is {1}<br />"; // Display a list of selected items with values // Get the list item MobileListItem itm = SelectionList1.Items[i]; // List the selected items and values if (itm.Selected) { txt += String.Format(spec, itm.Text, itm.Value); } // Put the text into the TextView txtView.Text = txt; // Add txtView to the form Form1.Controls.Add(txtView); } // Hide unnecessary controls SelectionList1.Visible = false; link1.Visible = false; Command1.Visible = false; } // Property to persist the count between postbacks private int count { get { object o = ViewState["FormCount"]; return o == null ? 0 : (int)o; } set { ViewState["FormCount"] = value; } } // A custom class for the task array private class Task { private String _TaskName; private String _Status; public Task(String TaskName, String Status) { _TaskName = TaskName; _Status = Status; } public String TaskName { get { return _TaskName; } } public String Status { get { return _Status; } } } </script> <html > <body> <!-- The first form: Form1 --> <mobile:Form ID="Form1" Runat="server" OnDeactivate="Form1_Deactivate" OnActivate="Form1_Activate"> <mobile:Label ID="message1" Runat="server"> Welcome to ASP.NET </mobile:Label> <mobile:Label ID="message2" Runat="server" /> <mobile:SelectionList Runat="server" ID="SelectionList1" ForeColor="red" SelectType="CheckBox" /> <mobile:Link ID="link1" Runat="server" NavigateUrl="#Form2" Text="Next Form" /><br /> <mobile:Command ID="Command1" Runat="server" Text="Submit" OnClick="Command1_OnSubmit" /> </mobile:Form> <!-- The second form: Form2 --> <mobile:Form ID="Form2" Runat="server" OnActivate="Form2_Activate"> <mobile:Label ID="message4" Runat="server"> Welcome to ASP.NET </mobile:Label> <mobile:Link ID="Link2" Runat="server" NavigateUrl="#Form1" Text="Back" /> </mobile:Form> </body> </html>
System.Web.UI.Control
System.Web.UI.MobileControls.MobileControl
System.Web.UI.MobileControls.Panel
System.Web.UI.MobileControls.Form
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Form クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
<ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ Public Class Form Inherits ContainerControl
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] public class Form : ContainerControl
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] public ref class Form : public ContainerControl
Form は、アプリケーションで表示されるあらゆるウィンドウを表します。Form クラスを使用すると、ツールを格納する標準的な境界線のないフローティング ウィンドウを作成できます。また Form クラスは、ダイアログ ボックスなどのモーダル ウィンドウを作成する目的でも使用できます。特殊なフォームであるマルチ ドキュメント インターフェイス (MDI: Multiple Document Interface) フォームには、MDI 子フォームと呼ばれる別のフォームを格納できます。MDI フォームは、IsMdiContainer プロパティを true に設定することによって作成します。MDI 子フォームは、MdiParent プロパティに、その子フォームを格納する MDI 親フォームを設定することによって作成します。
Form クラスで用意されているプロパティを使用すると、作成するウィンドウやダイアログ ボックスの外観、サイズ、色、およびウィンドウ管理機能を決定できます。Text プロパティでは、タイトル バーに表示されるウィンドウのキャプションを指定できます。Size プロパティと DesktopLocation プロパティでは、ウィンドウが表示されたときのサイズと位置を定義できます。ForeColor 色プロパティを使用すると、フォーム上に配置されるすべてのコントロールの既定の前景色を変更できます。FormBorderStyle、MinimizeBox、および MaximizeBox の各プロパティでは、実行時にフォームの最小化、最大化、またはサイズ変更を許可するかどうかを制御できます。
プロパティの他に、このクラスのメソッドを使用してフォームを操作することもできます。たとえば、ShowDialog メソッドを使用すると、フォームをモーダル ダイアログ ボックスとして表示できます。また SetDesktopLocation メソッドを使用すると、デスクトップ上のフォームの位置を設定できます。
Form クラスのイベントを使用すると、フォーム対して実行されるアクションに応答できます。Activated イベントを使用すると、フォームがアクティブになったときに、フォームのコントロール内に表示されるデータを更新するなどの操作を実行できます。
このクラスに Main と呼ばれるメソッドを配置することにより、フォームをアプリケーションの起動時のクラスとして使用できます。この Main メソッド内に、フォームを作成して表示するコードを追加します。フォームを実行するには、Main メソッドに STAThread 属性も追加する必要があります。起動フォームが閉じられると、アプリケーションも閉じられます。
Form の新しいインスタンスを作成し、ShowDialog メソッドを呼び出して、フォームをダイアログ ボックスとして表示するコード例を次に示します。この例では、FormBorderStyle、AcceptButton、CancelButton、MinimizeBox、MaximizeBox、StartPosition の各プロパティを設定してフォームの外観と機能を変更し、ダイアログ ボックスとして表示します。この例では、フォームの Controls コレクションの Add メソッドを使用して、2 つの Button コントロールも追加します。また、HelpButton プロパティを使用して、ダイアログ ボックスのキャプション バーにヘルプ ボタンを表示します。
Public Sub CreateMyForm() ' Create a new instance of the form. Dim form1 As New Form() ' 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. form1.Text = "My Dialog Box" ' Display a help button on the form. form1.HelpButton = True ' Define the border style of the form to a dialog box. form1.FormBorderStyle = FormBorderStyle.FixedDialog ' Set the MaximizeBox to false to remove the maximize box. form1.MaximizeBox = False ' Set the MinimizeBox to false to remove the minimize box. form1.MinimizeBox = False ' Set the accept button of the form to button1. form1.AcceptButton = button1 ' Set the cancel button of the form to button2. form1.CancelButton = button2 ' Set the start position of the form to the center of the screen. form1.StartPosition = FormStartPosition.CenterScreen ' Add button1 to the form. form1.Controls.Add(button1) ' Add button2 to the form. form1.Controls.Add(button2) ' Display the form as a modal dialog box. form1.ShowDialog() End Sub
public void CreateMyForm() { // Create a new instance of the form. Form form1 = new Form(); // Create two buttons to use as the accept and cancel buttons. Button button1 = new Button (); Button button2 = 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. form1.Text = "My Dialog Box"; // Display a help button on the form. form1.HelpButton = true; // Define the border style of the form to a dialog box. form1.FormBorderStyle = FormBorderStyle.FixedDialog; // Set the MaximizeBox to false to remove the maximize box. form1.MaximizeBox = false; // Set the MinimizeBox to false to remove the minimize box. form1.MinimizeBox = false; // Set the accept button of the form to button1. form1.AcceptButton = button1; // Set the cancel button of the form to button2. form1.CancelButton = button2; // Set the start position of the form to the center of the screen. form1.StartPosition = FormStartPosition.CenterScreen; // Add button1 to the form. form1.Controls.Add(button1); // Add button2 to the form. form1.Controls.Add(button2); // Display the form as a modal dialog box. form1.ShowDialog(); }
public: void CreateMyForm() { // Create a new instance of the form. Form^ form1 = gcnew Form; // Create two buttons to use as the accept and cancel buttons. Button^ button1 = gcnew Button; Button^ button2 = gcnew Button; // Set the text of button1 to "OK". button1->Text = "OK"; // Set the position of the button on the form. button1->Location = 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 = Point( button1->Left, button1->Height + button1->Top + 10 ); // Set the caption bar text of the form. form1->Text = "My Dialog Box"; // Display a help button on the form. form1->HelpButton = true; // Define the border style of the form to a dialog box. form1->FormBorderStyle = ::FormBorderStyle::FixedDialog; // Set the MaximizeBox to false to remove the maximize box. form1->MaximizeBox = false; // Set the MinimizeBox to false to remove the minimize box. form1->MinimizeBox = false; // Set the accept button of the form to button1. form1->AcceptButton = button1; // Set the cancel button of the form to button2. form1->CancelButton = button2; // Set the start position of the form to the center of the screen. form1->StartPosition = FormStartPosition::CenterScreen; // Add button1 to the form. form1->Controls->Add( button1 ); // Add button2 to the form. form1->Controls->Add( button2 ); // Display the form as a modal dialog box. form1->ShowDialog(); }
public void CreateMyForm() { // Create a new instance of the form. Form form1 = new Form(); // Create two buttons to use as the accept and cancel buttons. Button button1 = new Button(); Button button2 = new Button(); // Set the text of button1 to "OK". button1.set_Text("OK"); // Set the position of the button on the form. button1.set_Location(new Point(10, 10)); // Set the text of button2 to "Cancel". button2.set_Text("Cancel"); // Set the position of the button based on the location of button1. button2.set_Location(new Point(button1.get_Left(), button1.get_Height() + button1.get_Top() + 10)); // Set the caption bar text of the form. form1.set_Text("My Dialog Box"); // Display a help button on the form. form1.set_HelpButton(true); // Define the border style of the form to a dialog box. form1.set_FormBorderStyle(get_FormBorderStyle().FixedDialog); // Set the MaximizeBox to false to remove the maximize box. form1.set_MaximizeBox(false); // Set the MinimizeBox to false to remove the minimize box. form1.set_MinimizeBox(false); // Set the accept button of the form to button1. form1.set_AcceptButton(button1); // Set the cancel button of the form to button2. form1.set_CancelButton(button2); // Set the start position of the form to the center of the screen. form1.set_StartPosition(FormStartPosition.CenterScreen); // Add button1 to the form. form1.get_Controls().Add(button1); // Add button2 to the form. form1.get_Controls().Add(button2); // Display the form as a modal dialog box. form1.ShowDialog(); } //CreateMyForm
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ContainerControl
System.Windows.Forms.Form
派生クラス
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- Form クラスのページへのリンク