Postupy: Vytvoření ohraničení kolem ovládacího prvku Windows Forms pomocí výplně
Následující příklad kódu ukazuje, jak vytvořit ohraničení nebo obrys kolem RichTextBox ovládacího prvku. Příklad nastaví hodnotu vlastnosti Padding ovládacího prvku Panel na hodnotu 5 a nastaví Dock vlastnost podřízeného ovládacího prvku RichTextBox na Fill. BackColor ovládacího prvku Panel je nastaven na Blue, čímž se kolem ovládacího prvku RichTextBox vytvoří modré ohraničení.
Příklad
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace MarginAndPadding
{
public class Form1 : Form
{
private Panel panel1;
private RichTextBox richTextBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
// This code example demonstrates using the Padding property to
// create a border around a RichTextBox control.
public Form1()
{
InitializeComponent();
this.panel1.BackColor = System.Drawing.Color.Blue;
this.panel1.Padding = new System.Windows.Forms.Padding(5);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.richTextBox1);
this.panel1.Location = new System.Drawing.Point(20, 20);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(491, 313);
this.panel1.TabIndex = 0;
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(5, 5);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(481, 303);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(531, 353);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Padding = new System.Windows.Forms.Padding(20);
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private panel1 As Panel
Private richTextBox1 As RichTextBox
'/ <summary>
'/ Required designer variable.
'/ </summary>
Private components As System.ComponentModel.IContainer = Nothing
' This code example demonstrates using the Padding property to
' create a border around a RichTextBox control.
Public Sub New()
InitializeComponent()
Me.panel1.BackColor = System.Drawing.Color.Blue
Me.panel1.Padding = New System.Windows.Forms.Padding(5)
Me.panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill
End Sub
'/ <summary>
'/ Clean up any resources being used.
'/ </summary>
'/ <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
Protected Overrides Sub Dispose(disposing As Boolean)
If disposing AndAlso (components IsNot Nothing) Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
'/ <summary>
'/ Required method for Designer support - do not modify
'/ the contents of this method with the code editor.
'/ </summary>
Private Sub InitializeComponent()
Me.panel1 = New System.Windows.Forms.Panel()
Me.richTextBox1 = New System.Windows.Forms.RichTextBox()
Me.panel1.SuspendLayout()
Me.SuspendLayout()
'
' panel1
'
Me.panel1.Controls.Add(Me.richTextBox1)
Me.panel1.Location = New System.Drawing.Point(20, 20)
Me.panel1.Name = "panel1"
Me.panel1.Size = New System.Drawing.Size(491, 313)
Me.panel1.TabIndex = 0
'
' richTextBox1
'
Me.richTextBox1.Location = New System.Drawing.Point(5, 5)
Me.richTextBox1.Name = "richTextBox1"
Me.richTextBox1.Size = New System.Drawing.Size(481, 303)
Me.richTextBox1.TabIndex = 0
Me.richTextBox1.Text = ""
'
' Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(531, 353)
Me.Controls.Add(panel1)
Me.Name = "Form1"
Me.Padding = New System.Windows.Forms.Padding(20)
Me.Text = "Form1"
Me.panel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
End Class
Public Class Program
'/ <summary>
'/ The main entry point for the application.
'/ </summary>
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
End Class
Viz také
Spolupracujte s námi na GitHubu
Zdroj tohoto obsahu najdete na GitHubu, kde můžete také vytvářet a kontrolovat problémy a žádosti o přijetí změn. Další informace najdete v našem průvodci pro přispěvatele.
.NET Desktop feedback