방법: 안쪽 여백을 사용하여 Windows Forms 컨트롤 주위에 테두리 만들기

다음 코드 예제에서는 RichTextBox 컨트롤 주위에 테두리 또는 외곽선을 만드는 방법을 보여 줍니다. 예제에서는 Panel 컨트롤의 Padding 속성 값을 5로 설정하고 자식 RichTextBox 컨트롤의 Dock 속성을 Fill로 설정합니다. Panel 컨트롤의 BackColorBlue로 설정하면 RichTextBox 컨트롤 주위에 파란색 테두리가 만들어집니다.

예제

Imports System
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
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());
        }
    }


}

참고 항목

참조

Padding

개념

Windows Forms 컨트롤의 여백 및 안쪽 여백