方法 : DataRepeater コントロールのレイアウトを変更する (Visual Studio)
DataRepeater コントロールは、垂直方向 (項目を縦にスクロールする) または水平方向 (項目を横にスクロールする) に表示できます。この方向は、デザイン時にも実行時にも、LayoutStyle プロパティを変更することによって変更できます。実行時に LayoutStyle プロパティを変更する場合は、ItemTemplate のサイズと子コントロールの位置も変更することが必要になる場合があります。
[!メモ]
実行時に ItemTemplate 上のコントロールの位置を変更する場合、コントロールの位置を変更するコード ブロックの先頭と末尾で、BeginResetItemTemplate メソッドおよび EndResetItemTemplate メソッドを呼び出す必要があります。
デザイン時にレイアウトを変更するには
Windows フォーム デザイナーで、DataRepeater コントロールを選択します。
[!メモ]
DataRepeater コントロールの上部にある ItemTemplate 領域ではなく、下部の領域でコントロールの外枠をクリックして選択する必要があります。
[プロパティ] ウィンドウで、LayoutStyle プロパティを Vertical または Horizontal に設定します。
実行時にレイアウトを変更するには
ボタンまたはメニューの Click イベント ハンドラーに次のコードを追加します。
' Switch the orientation. If DataRepeater1.LayoutStyle = PowerPacks.DataRepeaterLayoutStyles.Vertical Then DataRepeater1.LayoutStyle = PowerPacks.DataRepeaterLayoutStyles.Horizontal Else DataRepeater1.LayoutStyle = PowerPacks.DataRepeaterLayoutStyles.Vertical End If
// Switch the orientation. if (dataRepeater1.LayoutStyle == DataRepeaterLayoutStyles.Vertical) { dataRepeater1.LayoutStyle = DataRepeaterLayoutStyles.Horizontal; } else { dataRepeater1.LayoutStyle = DataRepeaterLayoutStyles.Vertical; }
多くの場合、例のセクションに示されているようなコードを追加して、ItemTemplate のサイズおよびコントロールの位置を新しい方向に合わせて変更する必要があります。
使用例
イベント ハンドラー内で LayoutStyleChanged イベントに応答する方法を次の例に示します。この例では、フォームに DataRepeater1 という名前の DataRepeater コントロールが配置され、そのコントロールの ItemTemplate に TextBox1 および TextBox2 という名前の 2 つの TextBox コントロールが配置されている必要があります。
Private Sub DataRepeater1_LayoutStyleChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DataRepeater1.LayoutStyleChanged
' Call a method to re-initialize the template.
DataRepeater1.BeginResetItemTemplate()
If DataRepeater1.LayoutStyle =
PowerPacks.DataRepeaterLayoutStyles.Vertical Then
' Change the height of the template and rearrange the controls.
DataRepeater1.ItemTemplate.Height = 150
DataRepeater1.ItemTemplate.Controls(TextBox1.Name).Location =
New Point(20, 40)
DataRepeater1.ItemTemplate.Controls(TextBox2.Name).Location =
New Point(150, 40)
Else
' Change the width of the template and rearrange the controls.
DataRepeater1.ItemTemplate.Width = 150
DataRepeater1.ItemTemplate.Controls(TextBox1.Name).Location =
New Point(40, 20)
DataRepeater1.ItemTemplate.Controls(TextBox2.Name).Location =
New Point(40, 150)
End If
' Apply the changes to the template.
DataRepeater1.EndResetItemTemplate()
End Sub
private void dataRepeater1_LayoutStyleChanged_1(object sender, EventArgs e)
{
// Call a method to re-initialize the template.
dataRepeater1.BeginResetItemTemplate();
if (dataRepeater1.LayoutStyle == DataRepeaterLayoutStyles.Vertical)
// Change the height of the template and rearrange the controls.
{
dataRepeater1.ItemTemplate.Height = 150;
dataRepeater1.ItemTemplate.Controls["TextBox1"].Location = new Point(20, 40);
dataRepeater1.ItemTemplate.Controls["TextBox2"].Location = new Point(150, 40);
}
else
{
// Change the width of the template and rearrange the controls.
dataRepeater1.ItemTemplate.Width = 150;
dataRepeater1.ItemTemplate.Controls["TextBox1"].Location = new Point(40, 20);
dataRepeater1.ItemTemplate.Controls["TextBox2"].Location = new Point(40, 150);
}
// Apply the changes to the template.
dataRepeater1.EndResetItemTemplate();
}
参照
処理手順
DataRepeater コントロールのトラブルシューティング (Visual Studio)
方法 : DataRepeater コントロールの外観を変更する (Visual Studio)