1. 程式人生 > 實用技巧 >C# 新增panel,在panel上新增控制元件,刪除panel.設定label字型型別

C# 新增panel,在panel上新增控制元件,刪除panel.設定label字型型別

VS2012

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Panel panel1 = new Panel();

        
public Form1() { InitializeComponent(); //新增panel控制元件 this.Controls.Add(panel1); panel1.Size = new Size(100, 100);//設定控制元件大小 panel1.Location = new Point(20, 20);//設定控制元件位置 panel1.BackColor = Color.Blue;//設定控制元件背景顏色 //在panel控制元件上新增label控制元件
Label label1 = new Label(); panel1.Controls.Add(label1); label1.Text = "這是按鈕";//設定控制元件內容 label1.Location = new Point(0, 10); //設定字型型別 FontFamily myFonFamily = new FontFamily("宋體"); Font myFont = new Font(myFonFamily, 14, FontStyle.Bold); label1.Font
= myFont; //設定字型顏色 label1.ForeColor = Color.Red; } private void button1_Click(object sender, EventArgs e) { //刪除panel控制元件 this.Controls.Remove(panel1); } } } Caesar盧尚宇 2020年8月18日